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.
function PauseButtonValueChanged(app, event)
app.StopButton.Enable = 'off';
app.PauseButton.Text = 'Resume';
app.StopButton.Enable = 'on';
app.PauseButton.Text = 'Pause';
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)
app.PauseButton.Value = false;
PauseButtonValueChanged(app, [])
app.StopButton.Value = false;
app.PauseButton.Enable = 'off';
app.StopButton.Enable = 'off';
app.PauseButton.Enable = 'on';
app.StopButton.Enable = 'on';
error('Unknown flag value.')
checkPauseStopStatus(app,1)
error('MATLAB:eyeTrackAPP:abort','User aborted execution.')
waitfor(app.PauseButton,'Value',false)
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.
checkPauseStopStatus(app)
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.