How to number a plot legend for the number of values a user inputs
1 view (last 30 days)
Show older comments
I am trying to a legend that reads "G(s) #1" for the first plot, "G(s) #2" and so on for the number of values the user inputs into the system.
Appreciate the help.
clear
s = tf('s');
zeta = input('Enter damping ratio values in a 1D array: ');
omega = input('Enter natural frequency values in a 1D array: ');
stop = input('Enter stop time: ');
figure;
hold on;
for n = 1 : length(omega)
Gs = omega(n)^2 / ((s^2) + (2*zeta(n)*omega(n)*s) + omega(n)^2);
step(Gs, stop);
end
hold off;
title({['Unit Step Response of G(s) = \omega_{n}/(s^2 + 2\zeta\omega_{n}s + \omega_{n}^2)'], ...
['\zeta = ', num2str(zeta(n)), ' and \omega_n = ', num2str(omega(n))]});
ylabel('System Response');
legend ('G(s) #')
0 Comments
Answers (1)
Dyuman Joshi
on 3 Aug 2023
Use compose
%Random value for example
n = 5;
str = compose("G(s) #%d", 1:n)
%use the generated string as legend
legend(str)
1 Comment
Voss
on 3 Aug 2023
Another option is to use string concatenation:
n = 5;
str = "G(s) #" + (1:n)
See Also
Categories
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!