How to plot two lines in a looped subplot in matlab?

19 次查看(过去 30 天)
I plotted 5 graphs with a loop in MATLAB. Now, I want to add a further line only to the third plot. However, when I try to do it, it adds the additional line to every subplot.
What I have is this:
lines = rand([30 5])
line2 = rand([30 1])
K =5
for j = 1:k
subplot(3, 2, j);
plot(lines(:,j), 'LineWidth',1,'Color', [0 0 0.5]);
hold on
plot(line2, 'LineWidth',1,'Color', [0 0 0.5], 'LineStyle','b--o')
yline(0, '-')
end
Can anyone help me out?
Thanks!

采纳的回答

Star Strider
Star Strider 2020-5-30
Add an if block in the loop:
lines = rand([30 5]);
line2 = rand([30 1]);
k = 5;
for j = 1:k
subplot(3, 2, j);
plot(lines(:,j), 'LineWidth',1,'Color', [0 0 0.5]);
hold on
if j == 3
plot(line2, 'LineWidth',1,'Color', [0 0 0.5], 'LineStyle','-.')
end
yline(0, '-')
end
.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by