How to number a plot legend for the number of values a user inputs

1 view (last 30 days)
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) #')

Answers (1)

Dyuman Joshi
Dyuman Joshi on 3 Aug 2023
%Random value for example
n = 5;
str = compose("G(s) #%d", 1:n)
str = 1×5 string array
"G(s) #1" "G(s) #2" "G(s) #3" "G(s) #4" "G(s) #5"
%use the generated string as legend
legend(str)
  1 Comment
Voss
Voss on 3 Aug 2023
Another option is to use string concatenation:
n = 5;
str = "G(s) #" + (1:n)
str = 1×5 string array
"G(s) #1" "G(s) #2" "G(s) #3" "G(s) #4" "G(s) #5"

Sign in to comment.

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!