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

조회 수: 21 (최근 30일)
Armando MAROZZI
Armando MAROZZI 2020년 5월 30일
댓글: Star Strider 2020년 5월 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
.
  댓글 수: 2
Armando MAROZZI
Armando MAROZZI 2020년 5월 30일
amazing! thanks a lot!
Star Strider
Star Strider 2020년 5월 30일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (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