App Designer terminate an execution of a function

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

Perfect, thank you Adam!
Glad I could help!
I've used this approach myself within an app.
Hello Adam!
I have ran into same problem and followed the instructions mentioned in the answer to create a public property in the App and use it in
a) callback function that gets invoked upon a button push (using which I want to kil the execution),
b) a for loop of n iterations (as an if block) within a .m script (triggering it by another button from the GUI button)
However, when I run the simulation using one GUI button and try to abort it using another button on same GUI App after couple of iterations, the value of the flag doesn't seem to be getting updated until all the iterations occur which defeats my purpose. Do you suggest any alternative or if you could provide some inputs on what I might be doing incorrect, I would appreciate it.
It doesn't sound like it's set up properly. I assume the external m-file has access to the app object containing the graphics handles etc.
Is there anyway the termination could be done with a non loop function but takes a long time to process?
Does ctrl + c works when the matlab script is run from the app designer in this case? Because i tried doing ctrl + c and it does not seems to do anything
@Ngoc Minh Nhat Nguyen, from the documentation,
Ctrl+C does not always stop execution for files that run a long time, or that call built-ins or MEX-files that run a long time.
@Adam Danz adding pause(1) before the if condiiton within the loop helped. Thanks!
@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.
@Adam Danz Will give it a try. Thank you!

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2020a

Asked:

on 24 Aug 2020

Edited:

on 27 Dec 2024

Community Treasure Hunt

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

Start Hunting!