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

조회 수: 2 (최근 30일)
Unnamed User
Unnamed User 2023년 3월 9일
댓글: Unnamed User 2023년 3월 9일
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