Clear Filters
Clear Filters

Using a for loop to plot multiple sets of data

3 views (last 30 days)
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
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.
Kacey Lange
Kacey Lange on 27 Jul 2022
No there are six sets of data from seven stations.
1st station is fTSM(1,8,15,22,29,36)
2nd station is fTSM(2,9,16,23,30,37)
3rd station is fTSM(3,10,17,24,31,38)
4th station is fTSM(4,11,18,25,32,39)
5th station is fTSM(5,12,19,26,33,40)
6th station is fTSM(6,13,20,27,34,41)
7th station is fTSM(7,14,21,28,35,42)
I want all the stations to be separate lines with 6 data points in each, corresponding to the 6 weeks of data collection.

Sign in to comment.

Accepted Answer

David Hill
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)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!