Effacer les filtres
Effacer les filtres

Color of legend does't change in the loop.

4 vues (au cours des 30 derniers jours)
Sa Moha
Sa Moha le 10 Déc 2021
Réponse apportée : Rik le 10 Déc 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 commentaire
KSSV
KSSV le 10 Déc 2021
It is because, the first loop plots in red color.

Connectez-vous pour commenter.

Réponse acceptée

Rik
Rik le 10 Déc 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'});

Plus de réponses (0)

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by