Textscan loop to store multiple groups of data
Show older comments
I would like to plot data from a file. My 'results.dat' file contains multiple groups of data as below -
51109, 753250
51365, 675792
-
51877, 897964
60716, 149582
60341, 123422
-
58586, 180476
56504, 235721
-
33023, 191901
30632, 193063
-
18428, 192009
12029, 235773
Groups are separated using '-'. I would like to do something like this:
while 1
%textscan until '-', i.e., loop when '-' is found
s(i) =textscan(fid,'%d, %d'); %I know this is wrong
i++;
end
So that s{0} stores the first group, s{1} the second group and so on. Then I will be able to plot as -
plot(s{0}{1},s{0}{2}); % x values: 51109, 51365; y values: 753250, 675792
plot(s{0}{1},s{0}{2}); % x values: 51877, 60716, 60341; y values: 897964, 149582, 123422
Answers (1)
opt = {'Delimiter',','};
fmt = '%f%f';
out = {};
[fid,msg] = fopen('test.txt','rt');
assert(fid>=3,msg)
while ~feof(fid)
out(:,end+1) = textscan(fid,fmt,opt{:}); %#ok<SAGROW>
end
fclose(fid);
plot(out{:})
generates this plot:

And was tested on this file:
2 Comments
Md Mamunur Rashid Akand
on 11 Apr 2017
Stephen23
on 11 Apr 2017
@Md Mamunur Rashid Akand: are you running my code exactly as it is above, or did you make changes? How are you running it? Are you using test.txt from my answer, or another file that you defined?
Categories
Find more on Large Files and Big Data in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!