App Designer terminate an execution of a function

92 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 24 Aug 2020
Since Matlab does not have a "kill all tasks" command, you can use a callback function to change the value of a flag that is checked repeatedly within the optimization function.
Define the flag as a public property of the app (see instructions) so the external function has access to the flag.
The app's startup function would set the flag to false: app.stopFlag = false;
The callback function would merely set the flag to true: app.stopFlag = true;
Within the external (or internal) function, you can use the condition,
if app.stopFlag % assuming app.stopFlag=true means process should abort
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.
Then, the flag should be reset: app.stopFlag=false.
  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!