Create drop down menu to change values of y on a graph.
조회 수: 5 (최근 30일)
이전 댓글 표시
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
채택된 답변
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File 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!