Filter löschen
Filter löschen

Plot with varying legend in for loop

12 Ansichten (letzte 30 Tage)
Mads Bilde
Mads Bilde am 3 Apr. 2020
Kommentiert: Mads Bilde am 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.

Akzeptierte Antwort

Peng Li
Peng Li am 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)

Weitere Antworten (0)

Kategorien

Mehr zu Plot Customization finden Sie in Help Center und File Exchange

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by