How to use 'hold on' when plotting inside for loops

14 ビュー (過去 30 日間)
Fotis_oe
Fotis_oe 2017 年 5 月 15 日
コメント済み: KSSV 2017 年 5 月 15 日
I have two matrices Flow1(1:5) and RTL(i,:) or (i,5).
I want to plot Flow1 over RTL and I write the following.
for i=num_cat
plot(Flow1(1:5),RTL(i,:),'--go');
hold on
end
hold off
I saw a previous post here that said that this is the way to get all 7 lines in one plot when i = 7 let's say. However, with my code, I only get the last plot line and not all the others. What do I do wrong and how can I get all plots in one figure? Thanks!

採用された回答

KSSV
KSSV 2017 年 5 月 15 日
編集済み: KSSV 2017 年 5 月 15 日
Flow1 = rand(1,5) ;
RTL = rand(7,5) ;
num_cat = 7 ;
for i=1:num_cat
plot(Flow1(1:5),RTL(i,:),'--go');
hold on
end
hold off
you have use i = num_cat, so you were getting only one plot. It should be i = 1:num_cat
  2 件のコメント
Fotis_oe
Fotis_oe 2017 年 5 月 15 日
Thanks a lot for quick reply and sorry for posting such a question where spelling was wrong! Do you perhaps also know how to get each line with a different colour and different legend? Because now, I get all of them in green!
KSSV
KSSV 2017 年 5 月 15 日
It is in green because you have mentioned the color green and fixed the markers too. You may check this:
Flow1 = rand(1,5) ;
RTL = rand(5,5) ;
num_cat = 5 ;
for i=1:num_cat
plot(Flow1(1:5),RTL(i,:));
hold on
end
hold off
legend({'i=1' ; 'i =2' ; 'i =3' ; 'i =4' ; 'i = 5'});

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by