App Designer terminate an execution of a function

53 views (last 30 days)
Hello guys,
I am working on an application with App Designer where I am running a script that's running an optimisation problem. Program starts with the button press that calls the function. Inside the function there's an optimisation algorithm that takes an anonymous function which calls multiple functions inside.
I would like to like to cancel the optimisation process by clicking on a button so I created a callback that contains command return, but it does not work, which is probably expected.
There's the command closereq but it closes the figure/app, which I don't want to.
Is it possible to pass a variable or property during the optimisation or is there some other alternative way to do it?

Accepted Answer

Adam Danz
Adam Danz on 24 Aug 2020
Edited: Adam Danz on 27 Dec 2024
Since Matlab does not have a "kill all tasks" command, you can continuously check the value of a flag within the function and stop execution when the flag changes. Here are two approaches.
Use a StateButton
  1. Add a StateButton named "Stop" (or whatever you want to name it).
  2. Initialize the button's state as off either in Design Mode or in the startup: app.StopButton.Value=false
  3. Pass the app object into your external function.
  4. Add a condition that stops execution when the StateButton value is true and toggle the button back to it's default state.
if app.StopButton.Value
app.StopButton.Value = false;
return
% or error('User stopped execution')
end
  • In some circumstances, you may need to add drawnow limitrate just before the conditional statement to force a graphics update so that the button state change is recognized.
Create a flag
You can use any callback function to change the value of a flag that is stored as an app property.
  1. Define the flag as a public property of the app (see instructions) so the external function has access to the flag.
  2. Set the flag to false in the app's startup function: app.stopFlag = false;
  3. In a callback function that triggers termination, set the flag to true: app.stopFlag = true;
  4. Within the external (or internal) function, check the value of the flag in a conditional statement and use the same logic as above.
if app.stopFlag % assuming app.stopFlag=true means process should abort
app.stopFlag = false;
return
% or error('User stopped execution')
end
Note that if you return before the function is complete, you'll need to assign default output values and you may need to add return commands to any other invoking functions.
  11 Comments
Adam Danz
Adam Danz on 17 Dec 2021
@Shyam Vudata, a 1-second pause is quite long. A shorter duration would likely work as well pause(0.05)or 0.1. You could also try drawnow instead of pause.

Sign in to comment.

More Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!