Selecting a dropdown value and getting an answer AFTER pressing button (in App Designer)

212 views (last 30 days)
Hi there,
I've been trying to build a GUI in App Designer where:
  1. you select a certain drop down value;
  2. You press a button;
  3. You get a message or the answer displayed in a text box, depending on the selected drop down value.
I can't seem to get this done though. I don't really know how to connect the selected drop down value to a certain outcome in the textbox, AFTER the button is pressed. In fact, I'm generally struggling with these callback functions.
Can anyone help me?
My current code is as follows:
methods (Access = private)
% Callback function
function ScenarioDropDownValueChanged(app, event)
value = app.ScenarioDropDown.Value;
end
% Button pushed function: BerekenenButton
function BerekenenButtonPushed(app, event)
if ScenarioDropDownValueChanged(value,'-kies-')
app.BestesystemenTextArea.Value='error, select scenario'
elseif ScenarioDropDownValueChanged(value,'Selection1')
app.BestesystemenTextArea.Value='Systeem 1'
else app.BestesystemenTextArea.Value='Systeem 2'
end
end
% App initialization and construction
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create BerekenenButton
app.BerekenenButton = uibutton(app.UIFigure, 'push');
app.BerekenenButton.ButtonPushedFcn = createCallbackFcn(app, @BerekenenButtonPushed, true);
app.BerekenenButton.FontWeight = 'bold';
app.BerekenenButton.Position = [266 219 100 22];
app.BerekenenButton.Text = 'Berekenen';
% Create BestesystemenTextArea
app.BestesystemenTextArea = uitextarea(app.UIFigure);
app.BestesystemenTextArea.Position = [272 97 150 60];
% Create ScenarioDropDown
app.ScenarioDropDown = uidropdown(app.UIFigure);
app.ScenarioDropDown.Items = {'-kies-', 'Selection1', 'Selection2', 'Selection3'};
app.ScenarioDropDown.ValueChangedFcn = createCallbackFcn(app, @ScenarioDropDownValueChanged, true);
app.ScenarioDropDown.Position = [281 368 145 22];
app.ScenarioDropDown.Value = '-kies-';
end
Thank you,
Jo

Accepted Answer

Geoff Hayes
Geoff Hayes on 1 May 2020
Jo - I think that you can just access the drop down value directly from the button callback (you probably don't want to be calling ScenarioDropDownValueChanged from with the button callback). Try the following
function BerekenenButtonPushed(app, event)
value = app.ScenarioDropDown.Value;
if strcmpi(value,'-kies-')
app.BestesystemenTextArea.Value='error, select scenario'
elseif strcmpi(value,'Selection1')
app.BestesystemenTextArea.Value='Systeem 1'
else
app.BestesystemenTextArea.Value='Systeem 2'
end
end
I'm assuming that value is the string representing the selection from the drop down control.

More Answers (0)

Categories

Find more on Develop uifigure-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!