How to set a graph legend based off user inputted values?

3 views (last 30 days)
I'm trying to display a set of graphs based on generated data from earlier in the code. I'd like to be able to view the graphs for different time steps throughout which the user can set. There are about 500 time values from 0s to 5s with a step of 0.01s. How can I set the legend of the graph to update based on the values the user input?
prompt = {'First Time','Second Time','Third Time','Fourth Time'...
'Fifth Time'};
name = ('Enter time (s) values to compare data');
defaultanswer = {'0.1','0.2','0.3','0.4','0.5'};
N = 80;
graph_time = inputdlg(prompt,name,[1 N],defaultanswer);
graph_time = str2double(graph_time);
% Multiple the user inputted values by 100 to align with stored values %
graph_time = graph_time * 100;
hold on
plot (lambda,output(graph_time(1,:),:))
plot (lambda,output(graph_time(2,:),:))
plot (lambda,output(graph_time(3,:),:))
plot (lambda,output(graph_time(4,:),:))
plot (lambda,output(graph_time(5,:),:))
hold off
xlabel('Wavelength (\mum)')
ylabel('Output')
legend({'t = 0.1s','t = 0.2s','t = 0.3s','t = 0.4s','t = 0.5s'})
if (variable == 1)
title('Title 1')
elseif (variable == 2)
title('Title 2')
end
Thank you in advance!

Accepted Answer

Dyuman Joshi
Dyuman Joshi on 9 Mar 2023
Edited: Dyuman Joshi on 9 Mar 2023
%Sample output (from user input)
out = {'0.2';'0.15';'0.36';'0.4';'0.07'};
num=str2double(out);
%String arrays corresponding to each value
str=compose('t = %0.02fs', num)
str = 5×1 cell array
{'t = 0.20s'} {'t = 0.15s'} {'t = 0.36s'} {'t = 0.40s'} {'t = 0.07s'}
%Sample data
x=0:0.1:10;
y=sin(x);
%5 Random plots
plot(x,y,x,y.^2,x,x+y,x,x-y,x,x.*y)
%legend
legend(str)

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!