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

2 views (last 30 days)
Armando MAROZZI on 16 Sep 2020
Commented: Armando MAROZZI on 16 Sep 2020
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 on 16 Sep 2020
Try replacing
if (j == t)
with
if (mod(j,2)==1)
and
if (j == d)
with
if (mod(j,2)==0)

#### 1 Comment

Armando MAROZZI on 16 Sep 2020
Brilliant! Thanks!