How to wait for another GUI button press in AppDesigner?

39 views (last 30 days)
So, I have webapp with the following loose structure.
  1. User clicks start in the GUI
  2. A sound is played
  3. User clicks one of several buttons to rate the sound
  4. The app waits for response
  5. Plays another sound shortly after recieving repsonse etc.
I'm stuck with trying to make the app wait for user response. I tried setting a while loop that would wait for a check variable to become true, and assigning that change in each button, but this didn't work, the buttons are not executable when it is in the while loop.
Documentations suggests "use uiwait" but the UI wait documentation doesn't give any further clue how this would be done when relying on callbacks etc, only when closing like dialogue boxes etc.
If I use uiwait()&uiresume, then it creates a figure external to the app which cannot be disabled as far as I can tell.
Thank you!

Answers (1)

Naman Kaushik
Naman Kaushik on 16 Jun 2023
Hi Huw!
I understand that you wish to make an application which first has a start button which on clicking, plays a sound, and the user then has to rate this. Once rated, the next sound is played and this continues.
One workaround for this is to use a public app property for this. You can define a counter variable as a property (let's call it x) (or the sound name, as you prefer). Now whenever, the user clicks the button to rate the sound, you can check the value of the property x, play the sound which corresponds to xand then update the value of x according to the new sound.
Consider the code shown below:
classdef firstApp < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
GiveratingButton matlab.ui.control.Button
StartButton matlab.ui.control.Button
RatethesoundButtonGroup matlab.ui.container.ButtonGroup
Button_3 matlab.ui.control.RadioButton
Button_2 matlab.ui.control.RadioButton
Button matlab.ui.control.RadioButton
end
properties (Access = public)
counter; % Description
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
app.RatethesoundButtonGroup.Visible = "off";
app.GiveratingButton.Visible = 'off';
app.counter = 1;
end
% Button pushed function: StartButton
function StartButtonPushed(app, event)
app.RatethesoundButtonGroup.Visible = "on";
app.GiveratingButton.Visible = 'on';
app.StartButton.Visible = 'off';
load train;
sound(y, Fs);
app.counter = app.counter+1;
end
% Button pushed function: GiveratingButton
function GiveratingButtonPushed(app, event)
% Can also use string for comparison and the string may be the
% sound name.
if(rem(app.counter, 4) ==0)
rating = app.RatethesoundButtonGroup.SelectedObject.Text;
% The rating is for the previous sound
load train;
sound(y, Fs);
elseif(rem(app.counter, 4) ==1)
rating = app.RatethesoundButtonGroup.SelectedObject.Text;
% The rating is for the previous sound
load laughter;
sound(y, Fs);
elseif(rem(app.counter, 4) == 2)
rating = app.RatethesoundButtonGroup.SelectedObject.Text;
% The rating is for the previous sound
load gong;
sound(y, Fs);
elseif(rem(app.counter, 4) == 3)
rating = app.RatethesoundButtonGroup.SelectedObject.Text;
% The rating is for the previous sound
load chirp
sound(y, Fs);
end
app.counter = app.counter+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 = 'MATLAB App';
% Create RatethesoundButtonGroup
app.RatethesoundButtonGroup = uibuttongroup(app.UIFigure);
app.RatethesoundButtonGroup.Title = 'Rate the sound';
app.RatethesoundButtonGroup.Position = [247 188 123 106];
% Create Button
app.Button = uiradiobutton(app.RatethesoundButtonGroup);
app.Button.Text = '1';
app.Button.Position = [11 60 58 22];
app.Button.Value = true;
% Create Button_2
app.Button_2 = uiradiobutton(app.RatethesoundButtonGroup);
app.Button_2.Text = '2';
app.Button_2.Position = [11 38 65 22];
% Create Button_3
app.Button_3 = uiradiobutton(app.RatethesoundButtonGroup);
app.Button_3.Text = '3';
app.Button_3.Position = [11 17 65 22];
% Create StartButton
app.StartButton = uibutton(app.UIFigure, 'push');
app.StartButton.ButtonPushedFcn = createCallbackFcn(app, @StartButtonPushed, true);
app.StartButton.Position = [258 321 100 23];
app.StartButton.Text = 'Start';
% Create GiveratingButton
app.GiveratingButton = uibutton(app.UIFigure, 'push');
app.GiveratingButton.ButtonPushedFcn = createCallbackFcn(app, @GiveratingButtonPushed, true);
app.GiveratingButton.Position = [259 146 100 23];
app.GiveratingButton.Text = 'Give rating';
% 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 = firstApp
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
% Execute the startup function
runStartupFcn(app, @startupFcn)
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
To learn more about App Designer properties you can refer the following documentation:
For understanding how to get selected value from a radio button group:
To learn about the startup function callback:

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!