Plot with varying legend in for loop

5 visualizzazioni (ultimi 30 giorni)
Mads Bilde
Mads Bilde il 3 Apr 2020
Commentato: Mads Bilde il 3 Apr 2020
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.

Risposta accettata

Peng Li
Peng Li il 3 Apr 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)

Più risposte (0)

Prodotti


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by