0

I have a file where each line is a list of CSV doubles, i.e:

80,81,179,180,181,182
114,115,27,31,34
16,17,18,25
63,64,35,58,73,75,76,94,95
67,68

I need to read in each line, temporarily store it as a 1 x n double array for some calculations, then move onto the next line.

The idea I had was:

fid = fopen('fileName.txt');
tline = fgets(fid);
while ischar(tline)
    % Update with solution I came up with
    values = cellfun(@str2double,regexp(tline,',', 'split'));
    tline = fgets(fid);
end

1 Answers1

1

You can search for the commas contained in each line and the either use the indexes of their location in the string or their amount to loop till the end of the line.

Community
  • 1
  • 1
Federico
  • 1,092
  • 3
  • 16
  • 36
  • The reply below it seems useful, it creates a cell array of each value, is there an easy way to convert that to a 1 x n vector? `cell2mat` combines all to one long value. EDIT: `cellfun(@str2double,cellArray);` – user2680312 May 22 '15 at 16:53
  • @user2680312 the command `tline == ','` will already provide a 1 x n vector. – Federico May 22 '15 at 16:55