How to update the number of options in a drop-down menu using a slider in App Designer
6 views (last 30 days)
Show older comments
I am struggling to understand how to update the properties of other components within an app using App Designer; specifically, how would one update the 'Items' vector of a drop-down menu based on the value set by a slider?
1 Comment
Tommy
on 19 Apr 2020
The drop-down menu should be saved as a property. In the below example, it is saved in a property called DropDown. In the slider's ValueChangedFcn, you can then access it with app.DropDown.
classdef app1 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
DropDownLabel matlab.ui.control.Label
DropDown matlab.ui.control.DropDown
SliderLabel matlab.ui.control.Label
Slider matlab.ui.control.Slider
end
% Callbacks that handle component events
methods (Access = private)
% Value changed function: Slider
function SliderValueChanged(app, event)
value = app.Slider.Value;
app.DropDown.Items = compose('Option %d', 1:(floor(value/20)+1));
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'UI Figure';
% Create DropDownLabel
app.DropDownLabel = uilabel(app.UIFigure);
app.DropDownLabel.HorizontalAlignment = 'right';
app.DropDownLabel.Position = [68 319 66 22];
app.DropDownLabel.Text = 'Drop Down';
% Create DropDown
app.DropDown = uidropdown(app.UIFigure);
app.DropDown.Items = {'Option 1'};
app.DropDown.Position = [149 319 100 22];
% Create SliderLabel
app.SliderLabel = uilabel(app.UIFigure);
app.SliderLabel.HorizontalAlignment = 'right';
app.SliderLabel.Position = [322 330 36 22];
app.SliderLabel.Text = 'Slider';
% Create Slider
app.Slider = uislider(app.UIFigure);
app.Slider.ValueChangedFcn = createCallbackFcn(app, @SliderValueChanged, true);
app.Slider.Position = [379 339 150 3];
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = app1
% Create UIFigure and 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
Answers (0)
See Also
Categories
Find more on Develop Apps Using App Designer 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!