Using a for loop to plot multiple sets of data
조회 수: 3 (최근 30일)
이전 댓글 표시
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
Dyuman Joshi
2022년 7월 27일
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.
채택된 답변
David Hill
2022년 7월 27일
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);
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!