Stop & pause a simulation in app designer using push button
120 views (last 30 days)
Show older comments
Amit Patankar
on 12 Mar 2021
Answered: Franck paulin Ludovig pehn Mayo
on 28 Dec 2021
Hi !
I would like to configure 2 push buttons in appdesigner, one of which will pause the running simulation, and another will stop the running simulation when clicked. What syntax should I use to get this worked ?
I have used "sim" command to run the simulation.
0 Comments
Accepted Answer
Adam Danz
on 12 Mar 2021
Edited: Adam Danz
on 12 Mar 2021
There is no built-in method for pausing a process as of r2021a.
Here's how I've achieved this behavior in apps. The main idea is to create a function in your app that merely checks the status of your pause/stop buttons and respond accordingly. The function can be called anywhere in or out of the app and can be called as many times as needed without consuming any measurable amount of time.
3 Steps to add a pause/resume and stop buttons to an app
1. Add a state button named "Pause" and assign a callback function. The callback function merely changes the button text between Pause<-->Resume. I also have a stop button and when the GUI/APP is paused, the stop button is disabled. The stop button does not have a callback function.
% Value changed function: PauseButton
function PauseButtonValueChanged(app, event)
% When paused, the stop button should be disabled.
if app.PauseButton.Value
app.StopButton.Enable = 'off';
app.PauseButton.Text = 'Resume';
else
app.StopButton.Enable = 'on';
app.PauseButton.Text = 'Pause';
end
2. Add a public function to the app named checkPauseStopStatus that, when called, merely checks that status of the pause and stop button. If paused, it changes the button name to resume and enters a wait loop until resumed. If stopped, an error is thrown indicating that the user aborted execution and the button states return to normal. The 2nd input in this function is a flag that can enable/disable the Pause/Stop buttons when you don't want the user interacting with them. You can call this function from the startupFcn to set up the buttons as well.
function checkPauseStopStatus(app,flag)
% This function can be called at any time to check that status of the pause and stop buttons.
% If paused, it will wait until un-paused.
% If stopped, it will throw an error to break execution. The error will not be thrown.
% If flag input is included and =1, buttons will be reset to de-selected.
% if flag input is included and =2, all buttons will be disabled (used during file writing and other processes)
% if flag input is included and =3, all buttons will be enabled (used during file writing and other processes)
if nargin > 1
switch flag
case 1 % reset pause/stop button
app.PauseButton.Value = false;
PauseButtonValueChanged(app, [])
app.StopButton.Value = false;
case 2 % disable buttons
app.PauseButton.Enable = 'off';
app.StopButton.Enable = 'off';
case 3 % enable buttons
app.PauseButton.Enable = 'on';
app.StopButton.Enable = 'on';
otherwise
error('Unknown flag value.')
end
drawnow()
else
if app.StopButton.Value
checkPauseStopStatus(app,1) % reset pause/stop buttons
error('MATLAB:eyeTrackAPP:abort','User aborted execution.')
end
if app.PauseButton.Value
waitfor(app.PauseButton,'Value',false)
end
end
3. Scatter this section below throughout your code in places where you want the code to check the pause/stop states and to repond accordingly. It can be at the top or bottom of a loop or before/after major sections of the code. Since the checkPauseStopStatus is a public property, this can exist in any external function or script that has access to the app. The app variable should be an input to the function.
% Check if process is paused
if ~isempty(app)
checkPauseStopStatus(app)
end
Limitations
The app will only pause/resume or stop at the checkpoints and it's not possible to put checkpoints in some processes. For example, loading a large file may take several seconds or minutes but the process can only be stopped/paused before or after loading. This method works well in loops or with functions that have discrete sections where status checks can be done between those sections.
1 Comment
More Answers (1)
Franck paulin Ludovig pehn Mayo
on 28 Dec 2021
@Adam Danz Hello and Merrx Xmas. Please would you mind having a look on the following thread?I have been struggling to fix it for weeks now.Thank you in advance
https://www.mathworks.com/matlabcentral/answers/1614760-appdesigner-gui-issue-code?s_tid=srchtitle
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements 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!