Changing Colors in a Plot Using a For Loop

5 ビュー (過去 30 日間)
Bob
Bob 2014 年 12 月 14 日
回答済み: Star Strider 2014 年 12 月 14 日
Here is my code.
Z=[3 1 7];
for i=1:length(Z);
A(i)=Z(i)+2
B(i)=Z(i)-7
C(i)=Z(i)+4
col=['g' 'b' 'r'];
for j:length(col)
x=[1 2 3];
y(:,i)=[A(i) B(i),C(i)];
subplot(3,1,i)
plot(x,y(:,i),'color',col(j))
end
end
My last line of code is failing to change the color on the plots. All of the plots end up red. I want one of the plots to be green, one to be blue, and one to be red. How can I fix my code?

採用された回答

Star Strider
Star Strider 2014 年 12 月 14 日
They will be if you change the ‘col’ subscript from ‘j’ to ‘i’:
plot(x,y(:,i),'color',col(i))
because you subscripted the subplots by ‘i’.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLine Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by