function r = readCSVfileIntoStruct(fileName,colStart) % function reads in a comma-deliminated file where the first row are % variable names and returns a struct with the variable names as fields % arguments: fileName % OPTIONAL: colStart: how many leading columns are skipped % (e.g.: if colStart = 2, first two columns are skipped) % returns: struct with data (fields are variable names) if nargin < 2 colStart = 0; end fid = fopen(fileName); tline = [',' fgetl(fid) ',']; fclose(fid); indexComma = find(tline == ','); % get data x = csvread(fileName,1,colStart); for i = 1+colStart:max(size(indexComma))-1 eval(['r.' tline(indexComma(i)+1:indexComma(i+1)-1) ' = x(:,i-colStart);']); end