Filter löschen
Filter löschen

Color of legend does't change in the loop.

1 Ansicht (letzte 30 Tage)
Sa Moha
Sa Moha am 10 Dez. 2021
Beantwortet: Rik am 10 Dez. 2021
Hi guys
Please run this simple code:
syms x z;
for i=1:2
z=[x,0];
fplot(z(1,1),[i i+1] , 'r');
title('z(x)');
hold on;
end
for j=1:2
z=[-x,0];
fplot(z(1,1),[j j+1] , 'b');
title('z(x)');
legend('one' , 'two');
hold on;
end
Why the legend color is red for 'two' ?
I defined blue for it. Can someone help here please?
Thank you.
  1 Kommentar
KSSV
KSSV am 10 Dez. 2021
It is because, the first loop plots in red color.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Rik
Rik am 10 Dez. 2021
Each call to fplot will create a line object. So in your code you create 4 objects, but you provide only 2 legend entries.
syms x z;
plot_handles=[];
for i=1:2
z=[x,0];
plot_handles(i)=fplot(z(1,1),[i i+1] , 'r');
hold on;
end
for j=1:2
z=[-x,0];
plot_handles(i+j)=fplot(z(1,1),[j j+1] , 'b');
hold on;
end
title('z(x)');
legend(plot_handles([1 3]),{'one' , 'two'});

Weitere Antworten (0)

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by