Using Matlab App Designer with 2 Dependent Pop Up Menus
조회 수: 9 (최근 30일)
이전 댓글 표시
I am trying use app designer to write the following program.
Part 1: There's a first drop down menu with 2 options: California, Texas
Part 2: There a second drop down menu that depends on the first drop down menu. If California is selected, the second drop down menu has the following options: San Diego, Sacramento, Irvine. If Texas is selected, the drop down menu has the following options: Austin, Dallas, Houston
How do I write a program that successfully connects the second drop down menu to the first drop down menu?
댓글 수: 0
채택된 답변
StefBu
2019년 1월 23일
You can do this by using a callback on changinge the value in your first drop down. All the magic is done in
function DropDown1ValueChanged(app, event)
Here you go:
classdef app1 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
DropDown2_2Label matlab.ui.control.Label
DropDown2_2 matlab.ui.control.DropDown
DropDown1Label matlab.ui.control.Label
DropDown1 matlab.ui.control.DropDown
end
methods (Access = private)
% Value changed function: DropDown1
function DropDown1ValueChanged(app, event)
value = app.DropDown1.Value;
switch value
case "California"
app.DropDown2_2.Items = {'San Diego', 'Sacaramento', 'Irvine'};
case "Texas"
app.DropDown2_2.Items = {'Austin', 'Dallas', 'Houston'};
end
end
end
% App initialization and construction
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure
app.UIFigure = uifigure;
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'UI Figure';
% Create DropDown2_2Label
app.DropDown2_2Label = uilabel(app.UIFigure);
app.DropDown2_2Label.HorizontalAlignment = 'right';
app.DropDown2_2Label.Position = [370 287 69 22];
app.DropDown2_2Label.Text = 'DropDown2';
% Create DropDown2_2
app.DropDown2_2 = uidropdown(app.UIFigure);
app.DropDown2_2.Items = {'San Diego', 'Sacaramento', 'Irvine'};
app.DropDown2_2.Position = [454 287 100 22];
app.DropDown2_2.Value = 'San Diego';
% Create DropDown1Label
app.DropDown1Label = uilabel(app.UIFigure);
app.DropDown1Label.HorizontalAlignment = 'right';
app.DropDown1Label.Position = [97 287 69 22];
app.DropDown1Label.Text = 'DropDown1';
% Create DropDown1
app.DropDown1 = uidropdown(app.UIFigure);
app.DropDown1.Items = {'California', 'Texas'};
app.DropDown1.ValueChangedFcn = createCallbackFcn(app, @DropDown1ValueChanged, true);
app.DropDown1.Position = [181 287 100 22];
app.DropDown1.Value = 'California';
end
end
methods (Access = public)
% Construct app
function app = app1
% Create and configure components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end
Greetins Stefan
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Develop uifigure-Based Apps에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!