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

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

采纳的回答

Dyuman Joshi
Dyuman Joshi 2023-3-9
编辑:Dyuman Joshi 2023-3-9
%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)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

标签

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by