How to use if statements and for loop to plot the following series?
1 次查看(过去 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!
0 个评论
采纳的回答
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 Center 和 File Exchange 中查找有关 Subplots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!