Using Matlab App Designer with 2 Dependent Pop Up Menus

19 views (last 30 days)
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?

Accepted Answer

StefBu
StefBu on 23 Jan 2019
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

More Answers (0)

Categories

Find more on Maintain or Transition figure-Based Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!