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 Center および File ExchangeCreating, 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