Plot with varying legend in for loop

6 次查看(过去 30 天)
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.

采纳的回答

Peng Li
Peng Li 2020-4-3
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)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Plot Customization 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by