Plot with varying legend in for loop

12 views (last 30 days)
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.

Accepted Answer

Peng Li
Peng Li on 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)
  1 Comment
Mads Bilde
Mads Bilde on 3 Apr 2020
Thanks a lot! It works perfectly!

Sign in to comment.

More Answers (0)

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by