フィルターのクリア

Display different graphs from a dropdown menu

20 ビュー (過去 30 日間)
Nev Pires
Nev Pires 2019 年 5 月 8 日
回答済み: Adam Danz 2019 年 5 月 10 日
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
  2 件のコメント
Adam Danz
Adam Danz 2019 年 5 月 8 日
What is the value of "val"?
Nev Pires
Nev Pires 2019 年 5 月 10 日
To be honest, I wanted val to be equal to whatever I selected from the dropdown menu. If "A" was selected, it would display the graph: y.A, and if "B" was selected it would display the graph: y.B. The only way I could get anything to display in the first place is with this code. I have no idea how to associate the dropdown menu selections with the respective graphs I want to plot.

サインインしてコメントする。

採用された回答

Adam Danz
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 件)

カテゴリ

Help Center および File ExchangeSpecifying Target for Graphics Output についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by