Display different graphs from a dropdown menu
조회 수: 14 (최근 30일)
이전 댓글 표시
I want to be able to use a dropdown menu to display different graphs. I have been able to generate the graphs and the dropdown menu. However, when I go to use the dropdown menu I get an error which will be described below. Here is my code:
clear
clc
% create figure and components
fig = uifigure;
ax = uiaxes('Parent', fig, 'Position', [10 10 400 400]);
% Create a plot
x = (-100:100)';
y.A = x.^2;
y.B = x.^3;
p = plot(ax, x, y.A);
p.YData = y.A;
% Create a dropdown component
dd = uidropdown(fig, 'Position', [430 210 100 22],...
'Items', {'A', 'B'},...
'Value', 'A',...
'ValueChangedFcn', @(dd, event) selection(dd,p));
% Create ValueChangedFcn callback
function selection(dd, p)
val = dd.Value;
p.YData = val;
end
The error that I get is as follows:
Value must be a vector of numeric type
Error in dropDownTest2>selection (line 29)
p.YData = val;
Error in dropDownTest2>@(dd,event)selection(dd,p)
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 378)
Error while evaluating DropDown PrivateValueChangedFcn.
Thanks for your help
채택된 답변
Adam Danz
2019년 5월 10일
This should do what you're describing. Let me know if you have any questions.
% create figure and components
fig = uifigure;
ax = uiaxes('Parent', fig, 'Position', [10 10 400 400]);
% Create a plot
x = (-100:100)';
y.A = x.^2;
y.B = x.^3;
p = plot(ax, x, y.A);
% p.YData = y.A; %No need for this line
% Create a dropdown component
dd = uidropdown(fig, 'Position', [430 210 100 22],...
'Items', {'A', 'B'},...
'Value', 'A',...
'ValueChangedFcn', @(dd, event) selection(dd,p,y));
% Create ValueChangedFcn callback
function selection(dd, p, y)
switch dd.Value
case 'A'
val = y.A;
case 'B'
val = y.B;
otherwise
error('Did not recognize selection.')
end
p.YData = val;
end
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Specifying Target for Graphics Output에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!