Create drop down menu to change values of y on a graph.
5 views (last 30 days)
Show older comments
steve geddes
on 6 Jun 2021
Commented: steve geddes
on 6 Jun 2021
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
0 Comments
Accepted Answer
Sulaymon Eshkabilov
on 6 Jun 2021
Edited: Sulaymon Eshkabilov
on 6 Jun 2021
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
More Answers (0)
See Also
Categories
Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!