How do I plot a cell array where every cell contains an (x,2) matrix?

2 views (last 30 days)
I am collecting large amounts of data that are output as .dat files. I filter through my entire directory to choose all the .dat files and then reassign them into a cell array as I go. The data imports as a structure when imported using importdata().
My code:
dr = dir('my directory');
c = 1;
for i = 1 : length(dr) % Sort through directory
fln = dr(lc).name; % get filename
[pathstr, name, ext] = fileparts(fln); % get filename parts
if strcmpi(ext,'.dat') % If file is .mat
x = importdata(fln); % Load file - currently a struct
RawData{c,1} = name; % Move file name into Col 1, Row 'c'
RawData{c,2} = x.data; % Move data (n by 2) into Col 2, Row 'c'
c = c + 1; % Increase counter
end
end
I have a cell array:
RawData =
'a14' [1603x2 double]
'a15' [1574x2 double]
'a16' [1632x2 double]
'a21' [1602x2 double]
'a22' [1635x2 double]
'a23' [1603x2 double]
'a24' [1640x2 double]
'a31' [1631x2 double]
'a32' [1605x2 double]
'a33' [1607x2 double]
'a34' [1605x2 double]
'a35' [1569x2 double]
How do I plot each of these (column 1 vs. column 2) all on the same plot without a loop? I want them to be different colors so that I can label them individually in the legend. I am also able to separate the data so that all 'x' data is in column 3 and all 'y' data is in column four so I would have an (n x 3) cell array.
Thanks.

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 23 Jan 2014
The better way to do it is to use the for loop
  3 Comments
Azzi Abdelmalek
Azzi Abdelmalek on 23 Jan 2014
You can add the legend at the end of your plot. Look at this example
RawData ={'a14' [(1:20)' sin(1:20)']
'a15' [(1:30)' 10*cos(1:30)']
'a16' [(1:40)' 20*sin(1:40)']}
couleur='krgbm'
for k=1:size(RawData,1)
xy=RawData{k,2}
plot(xy(:,1),xy(:,2),couleur(k))
hold on
end
legend(RawData(:,1))
hold off

Sign in to comment.

More Answers (1)

Khalid
Khalid on 24 Apr 2014
grades_imp = importdata('filename.dat');
A=grades_imp.data; B=grades_imp.textdata; C=cell2mat(B(2:end, 1)); time_hours = (mod(datenum(C), 1)) * 24; % transform hh:mm:ss to hours rel1 = A(:,1); rel2 = A(:,2); rel3 = A(:,3); rel4 = A(:,4); rel5 = A(:,5); rel6 = A(:,6); plot (time_hours,rel1,'b' ,time_hours,rel2,'r', time_hours,rel3,'g',time_hours,rel4,'y' ,time_hours,rel5,'w', time_hours,rel6,'m')

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!