How to use if statements and for loop to plot the following series?

조회 수: 2 (최근 30일)
I am trying to plot the following series in a subplot (6X2). The idea is to have the first column in blue, while the second in red. Besides, I would like to have a legend only in the first graph for each column that explains the meaning of the colours. I tried the following solution but it doesn't work.
VAR.tot = rand(30, 12)
x = (1:size(VAR.tot,1)).'; % Create ‘x’ To Plot Correctly
k = size(VAR.tot,2)
t = [1,3,5,7,9,11]
d = [2,4,6,8,10,12]
for j = 1:k
subplot(6, 2, j);
if (j == t)
plot(VAR.tot(:,j), 'LineWidth',1,'Color', [0.25,0.3,0.8]);
hold on
legend('External Instrument','Location','northwest','Orientation','horizontal')
end
if (j == d)
plot(VAR.tot(:,j), 'LineWidth',1,'Color', [0.6,0.1,0.2]);
hold on
legend('Cholesky','Location','northwest','Orientation','horizontal')
end
yline(0, '-')
end
What I would like to get is similar to the figure attached.
Can anyone help me sort this out?
Thanks a lot!

채택된 답변

Alan Stevens
Alan Stevens 2020년 9월 16일
Try replacing
if (j == t)
with
if (mod(j,2)==1)
and
if (j == d)
with
if (mod(j,2)==0)

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Subplots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by