App Designer terminate an execution of a function

조회 수: 29 (최근 30일)
Mario Malic
Mario Malic 2020년 8월 24일
편집: Adam Danz 2024년 12월 27일
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?

채택된 답변

Adam Danz
Adam Danz 2020년 8월 24일
편집: Adam Danz 2024년 12월 27일
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
Adam Danz
Adam Danz 2021년 12월 17일
@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.
Shyam Vudata
Shyam Vudata 2021년 12월 27일
@Adam Danz Will give it a try. Thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by