Using a for loop to plot multiple sets of data
3 views (last 30 days)
Show older comments
I am trying to use a for loop to plot multiple sets of data. My data matrix (fTSM) is 42x1 where I have six sets of data (corresponding to 6 weeks) from seven stations. The data is organized by the weeks the data was sampled, in order of the stations that are being sampled each week. I want to plot data each stations data in a time series, where the x-axis are the sampling weeks 1-6 and there will be seven lines in the graph corresponding to the seven stations. The for loop I have written is:
for k = 1:7:42
PC = fTSM(k);
PM = fTSM(k+1);
TG = fTSM(k+2);
SJ = fTSM(k+3);
HP = fTSM(k+4);
BL = fTSM(k+5);
BB = fTSM(k+6);
x = [1:7]';
fprintf('%d: %s vs %s\n',k,PC,PM,TG,SJ,HP,BL,BB);
h1=semilogy(x,[PC;PM;TG;SJ;HP;BL;BB],'LineWidth',1.5);
ylim([0 110])
xlim([1 7])
hold on
set(gca,'FontSize',20)
legend({'PC','PM','TG','SJ','HP','BL','BB'},'location','northeast','FontSize',6);
set(h1,'linewidth',1.5);
xlabel('Sampling week','FontSize',20);
ylabel('VSF (m^{-1} sr^{-1})','FontSize',20);
end
The outcome of this that there are six lines with seven weeks of data where the data is in order, instead of going to the next week of data. For example PC consists of fTSM(1:7) instead of fTSM(1,8,15,22,29,36) and PM consists of fTSM(8:14) instead of fTSM(2,9,16,23,30,37) and so on. This leaves out station BB in the graph.
2 Comments
Dyuman Joshi
on 27 Jul 2022
There are only 6 sets of data and you get 6 lines in the plot as well.
1:7 , 8:14 , 15:21 , 22:28 , 29:35 , 36:42
And since your legend has 7 elements in it, it doesn't show the extra one(s) here.
Accepted Answer
David Hill
on 27 Jul 2022
fTSM=reshape(fTSM,7,[])';
h1=semilogy(fTSM);
ylim([0 110])
xlim([1 7])
set(gca,'FontSize',20)
legend({'PC','PM','TG','SJ','HP','BL','BB'},'location','northeast','FontSize',6);
set(h1,'linewidth',1.5);
xlabel('Sampling week','FontSize',20);
ylabel('VSF (m^{-1} sr^{-1})','FontSize',20);
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements 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!