how to plot different range of files on one graph using hold on

i have 63 .mat files that contain cells of data. i am dividing the .mat files into three parts so that each file is plotted correspondingly on the same graph from another set. that is, file 1 in the first, second third set are plotted on the same graph using hold on and the same for file 2 and so on. Although, i have not included the plot command, i am still trying to see how the code looks like by dividing the files into two sets.
startIndex = 1;
endIndex = 3;
startIndex1 = 4;
endIndex1 = 6;
count1 = 0;
count2 = 0;
filelist = dir('C:\Users\shedr\Downloads\tec data\2017\DOBUM\ttr\ttr1\tt4\*.mat');
% first set of files
for fileidx = startIndex:endIndex
count1 = count1+1;
spectrum = load(filelist(fileidx).name);
C = spectrum.B
% second set of files
for fileidx1 = startIndex1:endIndex1
count2 = count2+1;
if count2 == count1
spectrum1 = load(filelist(fileidx1).name);
C1 = spectrum.B
end
end
% but the output is not favourable.

 Accepted Answer

startIndex = 1;
endIndex = 3;
startIndex1 = 4;
endIndex1 = 6;
count1 = 0;
filelist = dir('C:\Users\shedr\Downloads\tec data\2017\DOBUM\ttr\ttr1\tt4\*.mat');
% first set of files
for fileidx = startIndex:endIndex
count1 = count1+1;
count2 = 0;
spectrum = load( fullfile(filelist(fileidx).folder, filelist(fileidx).name));
C = spectrum.B;
% second set of files
fileidx1 = startIndex1 + (fileidx - startIndex);
spectrum1 = load(fullfile(filelist(fileidx1).folder, filelist(fileidx1).name));
C1 = spectrum.B;
%do something with C and C1
plot(C, 'displayname', filelist(fileidx).name);
hold on
plot(C1, 'displayname', filelist(fileidx1).name);
end
hold off
legend('show')

9 Comments

Thank for your answer. C and C1 are outputting well but they are not plotting. When I try to plot, it gives me an error message: invalid data argument. Is there a solution to this?

I think everything is ok now. I forgot I had to convert my file from cells to matrix. That was why it was telling me invalid data argument. Thanks.

I just noticed that I am getting only the last plot. The first and second plots are not shown. Is there any solution?

Considering the "hold on" then if only the last plot is showing up, either you removed the "hold on" or else the data happens to be exactly overlaying the previous plot.
yes the last plot is overlaying the first plot. The last plot is showing six legend instead of three with two line plots which is expected. I need the output to be three figures with two plots each. first plot; file1 from group1 and file1 from group2, second plot: file2 from group1 and file2 from group2 and so on. Thanks.
startIndex = 1;
endIndex = 3;
startIndex1 = 4;
endIndex1 = 6;
count1 = 0;
filelist = dir('C:\Users\shedr\Downloads\tec data\2017\DOBUM\ttr\ttr1\tt4\*.mat');
% first set of files
for fileidx = startIndex:endIndex
count1 = count1+1;
count2 = 0;
spectrum = load( fullfile(filelist(fileidx).folder, filelist(fileidx).name));
C = spectrum.B;
% second set of files
fileidx1 = startIndex1 + (fileidx - startIndex);
spectrum1 = load(fullfile(filelist(fileidx1).folder, filelist(fileidx1).name));
C1 = spectrum.B;
%do something with C and C1
figure()
tiledlayout()
nexttile();
plot(C, 'displayname', filelist(fileidx).name);
legend('show')
title(filelist(fileidx).name)
nexttile();
plot(C1, 'displayname', filelist(fileidx1).name);
legend('show')
title(filelist(fileidx1).name)
end
Not really what i want. The plots are plotting on 2 subplots of for each iteration. Please, I want the two subplots to be on one plot, distinguished by colour codes, thereby having three figures with two plots on each for each iteration.
If not for the loop, this could have been done easily using the hold on. But using the hold on inside the for loop is really my problem. Thanks.
startIndex = 1;
endIndex = 3;
startIndex1 = 4;
endIndex1 = 6;
count1 = 0;
filelist = dir('C:\Users\shedr\Downloads\tec data\2017\DOBUM\ttr\ttr1\tt4\*.mat');
% first set of files
for fileidx = startIndex:endIndex
count1 = count1+1;
count2 = 0;
spectrum = load( fullfile(filelist(fileidx).folder, filelist(fileidx).name));
C = spectrum.B;
% second set of files
fileidx1 = startIndex1 + (fileidx - startIndex);
spectrum1 = load(fullfile(filelist(fileidx1).folder, filelist(fileidx1).name));
C1 = spectrum.B;
%do something with C and C1
figure()
plot(C, 'displayname', filelist(fileidx).name);
hold on
plot(C1, 'displayname', filelist(fileidx1).name);
hold off
legend('show')
end

Thank you. This last one got it

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!