Create drop down menu to change values of y on a graph.

조회 수: 5 (최근 30일)
steve geddes
steve geddes 2021년 6월 6일
댓글: steve geddes 2021년 6월 6일
Hello I'm wanting to create a plot that changes the values of y based on user selection from a drop down menu.
I'm thinking it's similar to this example that changes the colour but obviously I would like to change the values of y.
I've tried to use a callback function to change the value of y but I can't figure out how to implement it correctly.
function plotOptions
fig = uifigure;
fig.Position(3:4) = [440 320];
ax = uiaxes('Parent',fig,...
'Position',[10 10 300 300]);
x = linspace(-2*pi,2*pi);
y = sin(x);
p = plot(ax,x,y);
p.Color = 'Blue';
dd = uidropdown(fig,...
'Position',[320 160 100 22],...
'Items',{'Red','Yellow','Blue','Green'},...
'Value','Blue',...
'ValueChangedFcn',@(dd,event) selection(dd,p));
end
% Create ValueChangedFcn callback:
function selection(dd,p)
val = dd.Value;
p.Color = val;
end

채택된 답변

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021년 6월 6일
편집: Sulaymon Eshkabilov 2021년 6월 6일
Here is a completed code with two dropdown menus:
function plotOptions
fig = uifigure;
fig.Position(3:4) = [440 320];
ax = uiaxes('Parent',fig,...
'Position',[10 10 300 300]);
x = linspace(-2*pi,2*pi);
y.A = sin(x);
y.B = cos(x);
y.C = sinc(x);
y.D = tan(x);
y.E = exp(x);
p = plot(ax,x,y.A);
p.Color = 'Blue';
dd = uidropdown(fig,...
'Position',[320 160 100 22],...
'Items',{'Red','Yellow','Blue','Green'},'Value','Blue',...
'ValueChangedFcn',@(dd,event) selection(dd,p));
N = uidropdown(fig, 'Position',[320 100 100 22],...
'Items',{'sin()','cos()','sinc()','tan()', 'exp()'},'Value','sin()', ...
'ValueChangedFcn', @(N, event) YD(N, p, y));
end
% Create ValueChangedFcn callback:
function selection(dd,p)
val = dd.Value; p.Color = val;
end
function YD(N, p, y)
switch N.Value
case 'sin()'
val = y.A;
case 'cos()'
val = y.B;
case 'sinc()'
val = y.C;
case 'tan()'
val = y.D;
case 'exp()'
val = y.E;
otherwise
error('WHAT?!')
end
p.YData = val;
end
  댓글 수: 1
steve geddes
steve geddes 2021년 6월 6일
Hi thanks for the help!
Is there a way to plot two sets of data on the same plot so they update based on the drop down menus?
So instead of colour for the one drop down menu you could select a second function.
Meaning you could select cos and sin to display at the same time. Then you could change one to tan or whatever else?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by