Plot with varying legend in for loop
7 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I need to do a for loop from -1 to 1 with increments of 0.1 where 0 is neglected. In this plot i create a transfer funciton and i plot the bode plot of if.
I would like to show a legend of a plot containing all the bode plots, with each system being names something like Gp(u1 = value of for loop).
My code looks something like this:
s = tf('s');
for u1 = -1:0.1:1
if u1 == 0
continue
end
Gp1 = tf([rand 1], [rand 1]);
txt = ['Gp, u1 = ',num2str(u1)];
bode(Gp1,'DisplayName',txt)
hold on
end
legend show
This works on a regular plot, but the bode() function does not accomodate the 'DisplayName' function.
How should i do this? I have tried saving the values in an array, but with no luck, as the index need to be positive and an integer.
0 commentaires
Réponse acceptée
Peng Li
le 3 Avr 2020
An easier work around i think is that you store txt during each loop in a string array, or a cell array, and plot legend afterwards.
try
s = tf('s');
ld = [];
for u1 = -1:0.1:1
if u1 == 0
continue
end
Gp1 = tf([rand 1], [rand 1]);
txt = "Gp, u1 = " + num2str(u1);
bode(Gp1)
ld = [ld; txt];
hold on
end
legend(ld)
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Legend dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!