Skip to content

Instantly share code, notes, and snippets.

@ahebrank
Created October 3, 2014 16:48
Show Gist options
  • Save ahebrank/dce675350df4b968cf9c to your computer and use it in GitHub Desktop.
Save ahebrank/dce675350df4b968cf9c to your computer and use it in GitHub Desktop.
read csv with text and numeric data
function outputStuff = mfcsvread(fileName)
%mfcsvread reads a CSV file containing both text & numeric data. MATLAB's
%csvread function will work with all numeric, or all text data, but not
%both. It's common to have a file with a single line of comma separated
%text headers followed by many rows of numeric data. xlsread is limited in
%the number of rows & colums (actually, Excel is the limitation) it can
%read.
%
% The CSV file should look like:
% comma, separated, text, ...
% 1,2,3,4,5,...
% 6,7,8,9,10,...
% etc...
%
% The output is a structure with the column headers as fields in a
% structure each with a vector of data.
% Open the file
if ~exist(fileName, 'file')
error('File not found.');
end
fid=fopen(fileName);
% Start reading the data
tline = fgetl(fid); % Read in the first line only (text headers)
tline = tline(tline~='"'); % kill "
tline = tline(tline~=' '); %Get rid of any spaces
commaLocs=findstr(',',tline); % find the commas
fieldNames=cell(1,(length(commaLocs)+1));
start=1;
for colIdx=1:length(commaLocs)
fieldNames{colIdx}=tline(start:commaLocs(colIdx)-1);
start=commaLocs(colIdx)+1;
end
fieldNames{colIdx+1}=tline(start:end);
%Read in the rest of the data (should be numeric)
fieldData=csvread(fileName,1,0);
%Convert the data into a single structure.
[cellW cellH]=size(fieldNames);
numFields=max([cellW cellH]);
%Needs to be a 1xN or Nx1 cell-array.
if ~iscell(fieldNames)
disp('Needs to be a cell-array');
return;
elseif cellW ~=numFields && cellH ~=numFields
disp('Needs to be 1xN or Nx1');
return;
end
dataSize=size(fieldData);
arrayDim=find(dataSize==numFields);
if isempty(arrayDim)
disp('Dimensions are wrong');
end
outputStuff=[];
% if arrayDim==2
% for idx=1:numFields
% outputStuff.(fieldNames{idx})=fieldData(:,idx);
% end
% else
% for idx=1:numFields
% outputStuff.(fieldNames{idx})=fieldData(idx,:);
% end
% end
outputStuff = struct(...
'header', {fieldNames},...
'data', fieldData);
fclose(fid);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment