How to stop further execution of M-script by using command?

조회 수: 1,396 (최근 30일)
Dong-Gyu Jang
Dong-Gyu Jang 2016년 1월 27일
댓글: DGM 2024년 5월 2일
Hello.
I'm trying to use 'questdlg' to shows question dialog with several buttons. My aim is that when I click 'stop' button in the dialog generated by using 'questdlg', no further execution of code.'switch' is used to define each action of buttons right after 'questdlg'.
I'm using break and return and they're all just to get out of the 'switch'. How can I abort all code running by using command?

답변 (5개)

Salam Ismaeel
Salam Ismaeel 2019년 11월 21일
편집: Salam Ismaeel 2019년 11월 21일
Just use:
return
anywhere in your code.
  댓글 수: 1
Walter Roberson
Walter Roberson 2019년 11월 22일
return only returns from the current function. If you are servicing a callback for a pushbutton, you are just going to return from the callback, without having affected anything in the loop of code that is executing.
The context of the question is that the user has a calculation loop and wishes to be able to click a button and have the calculation loop interrupted. When graphics interruptions are serviced it is in a callback function. The question then becomes:
  1. is there a way for the callback function to force the calculation function to abort?
  2. if not, is there a way for the callback function to cooperate with the calculation function to have the calculation function abort?
The answer for the first of those is "NO": the only two ways to force something to stop without its cooperation are to
  1. quit or exit MATLAB; or
  2. use java robot or similar to emit control-C into the command line to force the program to stop running
Even clear all cannot force a function to stop running.
The answer to the second of those possibilities is "Yes, it can be done" -- and the methods for doing that are what I discussed in my Answer.

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


Walter Roberson
Walter Roberson 2016년 1월 27일
exit() and quit() are identical and will terminate your MATLAB session.
error() will return to the closest "try" upwards in the calling stream. If you are inside a callback, the calling stream probably is not very deep.
If you are in nested loops and want to exit out of several of them, then you should define a "quitthis" variable that is tested right after the end of each loop, issuing a "break" if detected, and in this way having the quitting cascade out of all of the loops.
If you have a loop that is executing and you want to be able to interrupt it with a GUI action, then you need to have the GUI set a variable in a location that the loop polls the value of periodically to determine whether it is being told to quit. The termination must be cooperative: there is no way for a GUI element to force a loop to quit. For example,
handles.stopbutton = uicontrol('style', 'toggle', 'String', 'STOP', 'Value', 0, 'Position', ....)
for K = 1 : 200000
drawnow(); %give time for callback
quitthis = get(handles.stopbutton, 'Value');
if quitthis;
break;
end
....
end
Notice I used a toggle, not a pushbutton. pushbuttons have their value reset to 0 right after their callback is taken.
  댓글 수: 1
Tim Lueth
Tim Lueth 2020년 9월 15일
As already mentioned by Walter Roberson there no know solution yet. In fact, I wrote a function "dbexit", which finally works with
error ('Terminated correctly')
At least a practical solution during debugging.
I think Mathworks should provide this possibility to terminate a program cleanly or add an option in the error function, which prints only one line without the word "error" - and without the whole function hierarchy.

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


Leon
Leon 2024년 5월 1일
This doesn't answer your question, but something I use to stop a program without using an error message like error("Don't panic, just stopping the program.") and it's annoying bong sound is simply:
input("Please press Ctrl+C")
The user (which is usually me) still has to hit Ctrl+C to stop the program but it's the politest way I've found so far :)
I also wish MATLAB would implement a stopprogram() command for when return just gets you out of a function, etc.
  댓글 수: 1
DGM
DGM 2024년 5월 2일
I sometimes forget that people use system alert sounds. It's like remembering what it was like before disabling the CapsLock key. Ah, memories.

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


Daniel Oberbauer
Daniel Oberbauer 2022년 1월 26일
Maybe assert?
It stops execution and prints an error message to the Command Window if the enclose statement evaluates false.
  댓글 수: 2
Walter Roberson
Walter Roberson 2022년 1월 26일
assert() calls error()
Jose
Jose 2024년 3월 29일
In my case, assert() was what I needed. Thanks to Daniel for the suggestion.
My test code in Matlab 2022b:
function f1
try
f2
catch ME
if strcmp(ME.identifier,'MyException:EndOK')
disp('End with a nice message.')
else
rethrow(ME)
end
end
function f2
while 1
% infinite loop, just to show assert() works nicely
assert(false, 'MyException:EndOK', 'Uggly message, not to show')
end
PS: In this example I could have used return, but the purpose was to test whether assert could work better than error.

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


Asimananda Khandual
Asimananda Khandual 2023년 12월 9일
Cntrl+C
  댓글 수: 4
Scott Cohen
Scott Cohen 2024년 4월 10일
I'm a novice at this stuff, but I had what seems like a similar question and solved it using this: I just put the relevant code inside a for loop, for example "for m=1:1 . . . end" and then I inserted a "break" command where I wanted to discontinue my code. Would that work for this issue?
DGM
DGM 2024년 4월 10일
It depends what your goals are and whether you already needed the loop for the task at hand. This doesn't stop further execution in general. It just breaks out of the loop. If there is code after the loop, it still gets executed. If this is within a function, it will complete and return control to whatever called it.

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

카테고리

Help CenterFile Exchange에서 Dialog Boxes에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by