Discussion:
how to stop execution
(too old to reply)
Todd Welti
2009-03-11 21:03:00 UTC
Permalink
Raw Message
This might seem like a dumb question, but how do you stop code execution? I dont mean 'return', or 'dbstop', or 'error'. I just want to stop the code immediately, with no fanfare and just return to the command line. Strangley enough, i can't seem to find a command that does this! "Return" doesn't necessarily work if the current function is not the main function. "dbstop" puts it in debug mode. "quit" quits matlab.

I've looked and looked and can't find this simple command.
Walter Roberson
2009-03-11 21:44:29 UTC
Permalink
Raw Message
Post by Todd Welti
This might seem like a dumb question, but how do you stop code execution? I dont mean
'return', or 'dbstop', or 'error'. I just want to stop the code immediately, with no
fanfare and just return to the command line.
There isn't a way: all of those routines have to be unwound and their workspaces
cleaned up, which might invoke exit handlers, and so on.

The closest I can think of is to have your main routine have a try/catch
and then when you wish to abort, error() the particular string that the
catch is keyed for, and when you detect it, bail out cleanly from your
main routine.
Todd Welti
2009-03-11 23:52:02 UTC
Permalink
Raw Message
Post by Walter Roberson
Post by Todd Welti
This might seem like a dumb question, but how do you stop code execution? I dont mean
'return', or 'dbstop', or 'error'. I just want to stop the code immediately, with no
fanfare and just return to the command line.
There isn't a way: all of those routines have to be unwound and their workspaces
cleaned up, which might invoke exit handlers, and so on.
The closest I can think of is to have your main routine have a try/catch
and then when you wish to abort, error() the particular string that the
catch is keyed for, and when you detect it, bail out cleanly from your
main routine.
Yes, that should work. is it just me or is it strange that there is not such a command - like ctrl c, but useable in an mfile?
Jos
2009-03-12 07:54:04 UTC
Permalink
Raw Message
Post by Todd Welti
Post by Walter Roberson
Post by Todd Welti
This might seem like a dumb question, but how do you stop code execution? I dont mean
'return', or 'dbstop', or 'error'. I just want to stop the code immediately, with no
fanfare and just return to the command line.
There isn't a way: all of those routines have to be unwound and their workspaces
cleaned up, which might invoke exit handlers, and so on.
The closest I can think of is to have your main routine have a try/catch
and then when you wish to abort, error() the particular string that the
catch is keyed for, and when you detect it, bail out cleanly from your
main routine.
Yes, that should work. is it just me or is it strange that there is not such a command - like ctrl c, but useable in an mfile?
help keyboard

(or modify finish.m and use quit cancel)

hth
Jos
Peter O'Connor
2010-03-31 18:27:07 UTC
Permalink
Raw Message
Here's what I use to do that (a little roundabout because, ridiculously, it requires that the user confirm that you'd like to quit)

disp('You chose to cancel. Quit the program now?');
disp('<a href="MATLAB: dbquit;">Yes</a> / <a href="MATLAB: dbcont;">No</a>');
keyboard;
Post by Jos
Post by Todd Welti
Post by Walter Roberson
Post by Todd Welti
This might seem like a dumb question, but how do you stop code execution? I dont mean
'return', or 'dbstop', or 'error'. I just want to stop the code immediately, with no
fanfare and just return to the command line.
There isn't a way: all of those routines have to be unwound and their workspaces
cleaned up, which might invoke exit handlers, and so on.
The closest I can think of is to have your main routine have a try/catch
and then when you wish to abort, error() the particular string that the
catch is keyed for, and when you detect it, bail out cleanly from your
main routine.
Yes, that should work. is it just me or is it strange that there is not such a command - like ctrl c, but useable in an mfile?
help keyboard
(or modify finish.m and use quit cancel)
hth
Jos
Todd Welti
2010-03-31 19:02:20 UTC
Permalink
Raw Message
Post by Peter O'Connor
Here's what I use to do that (a little roundabout because, ridiculously, it requires that the user confirm that you'd like to quit)
disp('You chose to cancel. Quit the program now?');
disp('<a href="MATLAB: dbquit;">Yes</a> / <a href="MATLAB: dbcont;">No</a>');
keyboard;
Yes, that is slightly easier than using keyboard, then having the user type dbquit, but still not very clean. Plus I get a web browser opened up when I tried it. Not sure what would happen for compiled code.

Looking back in this thread (and one other on tis subject) I see several suggestions to use try/catch. That's fine if your not in a subfunction or subsubfunction. The last time I had this problem, not only was I in a sub sub funciton, but complicated by the fact that it was called from a GUI callback anonymous funciton. I suppose you could have each subfunction returning a success/failed (0 or 1) as is often done anyway. Then bailing out of a subsub function would result in clean bail out of main.
Walter Roberson
2010-03-31 19:47:45 UTC
Permalink
Raw Message
Post by Peter O'Connor
Here's what I use to do that (a little roundabout because, ridiculously,
it requires that the user confirm that you'd like to quit)
disp('You chose to cancel. Quit the program now?');
dbcont;">No</a>');
keyboard;
Testing in 2008b, I found that I had to use lower-case for that matlab. Also,
the space after the colon was not required but was accepted.

disp('<a href="matlab:dbquit;">Yes</a> / <a href="matlab:dbcont;">No</a>');


When I used MATLAB: then the help browser would complain that matlab was
unable to link to the document.
Rajaram
2013-02-20 20:41:14 UTC
Permalink
Raw Message
What i usually do is, close the main window, and i'll restart the application,since i save my work every time i run the program, i don't mind closing it...!
Matt Fig
2009-03-11 22:05:05 UTC
Permalink
Raw Message
ctrl+c
Todd Welti
2009-03-11 23:50:21 UTC
Permalink
Raw Message
Post by Matt Fig
ctrl+c
I meant a command that would be in the code.
Mike Hoff
2011-10-29 02:18:15 UTC
Permalink
Raw Message
Post by Todd Welti
This might seem like a dumb question, but how do you stop code execution? I dont mean 'return', or 'dbstop', or 'error'. I just want to stop the code immediately, with no fanfare and just return to the command line. Strangley enough, i can't seem to find a command that does this! "Return" doesn't necessarily work if the current function is not the main function. "dbstop" puts it in debug mode. "quit" quits matlab.
I've looked and looked and can't find this simple command.
I'm going to use

error('I don't want to play anymore')

It's quick and dirty, with red and errors and beeping, but it's better than a fancy loop, or commenting everything out
Radek
2012-01-28 21:45:11 UTC
Permalink
Raw Message
Post by Todd Welti
This might seem like a dumb question, but how do you stop code execution? I dont mean 'return', or 'dbstop', or 'error'. I just want to stop the code immediately, with no fanfare and just return to the command line. Strangley enough, i can't seem to find a command that does this! "Return" doesn't necessarily work if the current function is not the main function. "dbstop" puts it in debug mode. "quit" quits matlab.
I've looked and looked and can't find this simple command.
command:
break
Todd Welti
2012-01-30 21:17:11 UTC
Permalink
Raw Message
Post by Todd Welti
This might seem like a dumb question, but how do you stop code execution? I dont mean 'return', or 'dbstop', or 'error'. I just want to stop the code immediately, with no fanfare and just return to the command line. Strangley enough, i can't seem to find a command that does this! "Return" doesn't necessarily work if the current function is not the main function. "dbstop" puts it in debug mode. "quit" quits matlab.
I've looked and looked and can't find this simple command.
break
do you know what the break command does?!?!? This would not work at all.

BREAK Terminate execution of WHILE or FOR loop.
BREAK terminates the execution of FOR and WHILE loops.
In nested loops, BREAK exits from the innermost loop only.

BREAK is not defined outside of a FOR or WHILE loop.
Use RETURN in this context instead.
Jerry Gregoire
2012-10-02 16:48:07 UTC
Permalink
Raw Message
Post by Todd Welti
Post by Todd Welti
This might seem like a dumb question, but how do you stop code execution? I dont mean 'return', or 'dbstop', or 'error'. I just want to stop the code immediately, with no fanfare and just return to the command line. Strangley enough, i can't seem to find a command that does this! "Return" doesn't necessarily work if the current function is not the main function. "dbstop" puts it in debug mode. "quit" quits matlab.
I've looked and looked and can't find this simple command.
break
do you know what the break command does?!?!? This would not work at all.
BREAK Terminate execution of WHILE or FOR loop.
BREAK terminates the execution of FOR and WHILE loops.
In nested loops, BREAK exits from the innermost loop only.
BREAK is not defined outside of a FOR or WHILE loop.
Use RETURN in this context instead.
This is a very good question. And to a problem that I would think is common. None of the above solutions are particularly clean:

modifying finish.m - this alters the fundamental operation of ML and could lead to unexpected problems. Frankly, it scares me.

break - n/a

keyboard - You haven't emptied the interrupt stack and are still technically in the function. 'keyboard' BTW is a great debugging tool, but not applicable for the issue at hand.

issuing an error - This works but as Mike Hoff said it is rather gross. It is like using a sledge hammer, there is blood all over!

I HOPE SOMEONE FROM THE MATHWORKS READS THIS THREAD. There needs to be a clean way to halt execution and return directly to the workspace. In my case I have a timerfcn. A button on my GUI stops the timer, but ML merrily returns to whence it came when the callback was issued and causes grief.
dpb
2012-10-02 17:11:17 UTC
Permalink
Raw Message
On 10/2/2012 11:48 AM, Jerry Gregoire wrote:
...
Post by Jerry Gregoire
issuing an error - This works but as Mike Hoff said it is rather gross.
It is like using a sledge hammer, there is blood all over!
I HOPE SOMEONE FROM THE MATHWORKS READS THIS THREAD. There needs to be a
clean way to halt execution and return directly to the workspace. In my
case I have a timerfcn. A button on my GUI stops the timer, but ML
merrily returns to whence it came when the callback was issued and
causes grief.
ERROR() does exactly what you ask w/ the possible exception of a
message--what less of a sledge hammer could there be to do what you ask
(again, other than no message which is fairly minor altho I'll grant
I've wished for that as a feature as well).

Rather than complain here, submit a real feature request to official TMW
support if you really want some action.

--
Todd Welti
2012-10-02 17:31:08 UTC
Permalink
Raw Message
...
Post by Jerry Gregoire
issuing an error - This works but as Mike Hoff said it is rather gross.
It is like using a sledge hammer, there is blood all over!
I HOPE SOMEONE FROM THE MATHWORKS READS THIS THREAD. There needs to be a
clean way to halt execution and return directly to the workspace. In my
case I have a timerfcn. A button on my GUI stops the timer, but ML
merrily returns to whence it came when the callback was issued and
causes grief.
now that you mention it, I believe it was a timer issue that also was compounding the problem for me as well.

I suppose that one could write a function which used the error(), but suppresses the "blood". I tried
error(' ')

but still get
Error using untitled123 (line 4)
"warning off" had no effect.

ALSO, if you have "Always stop if error" turned on, which I always do, it will dbstop at the error() line. Mucho annoying.

--
Filipe
2014-04-27 02:56:08 UTC
Permalink
Raw Message
Post by Todd Welti
Post by Todd Welti
I've looked and looked and can't find this simple command.
break
do you know what the break command does?!?!? This would not work at all.
BREAK Terminate execution of WHILE or FOR loop.
BREAK terminates the execution of FOR and WHILE loops.
In nested loops, BREAK exits from the innermost loop only.
BREAK is not defined outside of a FOR or WHILE loop.
Use RETURN in this context instead.
I'm using Matlab2013a. Break works fine for me:

if ( strcmp(file,'file1.bmp')==1)
% code here
else
%errordlg('Invalid file!')
break;
end
Fontys
2013-09-02 13:33:08 UTC
Permalink
Raw Message
This might seem like a [...] "quit" quits matlab.
I've looked and looked and can't find this simple command.
I have been looking for this a while too. I found the following to be quite helpful.

a = 0
h = helpdlg('Question?', 'Title')
while ishandle(h)
a = a+1
pause(0.3)
% Or put any code that is good for you here.
end

This, of course, is only useful if it suits the environment of your code. That is completely up to you.
I hope this helps you along.
Adilson
2013-09-03 02:21:08 UTC
Permalink
Raw Message
I think that this could help:
http://www.mathworks.com/support/solutions/en/data/1-15KAD/?solution=1-15KAD
Post by Fontys
This might seem like a [...] "quit" quits matlab.
I've looked and looked and can't find this simple command.
I have been looking for this a while too. I found the following to be quite helpful.
a = 0
h = helpdlg('Question?', 'Title')
while ishandle(h)
a = a+1
pause(0.3)
% Or put any code that is good for you here.
end
This, of course, is only useful if it suits the environment of your code. That is completely up to you.
I hope this helps you along.
Theodore
2014-02-18 20:08:09 UTC
Permalink
Raw Message
Post by Todd Welti
This might seem like a dumb question, but how do you stop code execution? I dont mean 'return', or 'dbstop', or 'error'. I just want to stop the code immediately, with no fanfare and just return to the command line. Strangley enough, i can't seem to find a command that does this! "Return" doesn't necessarily work if the current function is not the main function. "dbstop" puts it in debug mode. "quit" quits matlab.
I've looked and looked and can't find this simple command.
You probably already solved your issue but for sake of options I use this: requires you to type y or n then hit enter to continue with the script or abort.

y='y'
n='n'
prompt='Continue? y/n'
result=input(prompt)
if result == y
'Continue'
else if result == 'n'
error('Stop')
end
end
Theodore
2014-02-18 20:09:09 UTC
Permalink
Raw Message
Post by Todd Welti
This might seem like a dumb question, but how do you stop code execution? I dont mean 'return', or 'dbstop', or 'error'. I just want to stop the code immediately, with no fanfare and just return to the command line. Strangley enough, i can't seem to find a command that does this! "Return" doesn't necessarily work if the current function is not the main function. "dbstop" puts it in debug mode. "quit" quits matlab.
I've looked and looked and can't find this simple command.
You probably already solved your issue but for sake of options I use this: requires you to type y or n then hit enter to continue with the script or abort.

y='y'
n='n'
prompt='Continue? y/n'
result=input(prompt)
if result == 'y'
'Continue'
else if result == 'n'
error('Stop')
end
end
Brandon Ballard
2016-07-19 12:20:09 UTC
Permalink
Raw Message
Post by Todd Welti
This might seem like a dumb question, but how do you stop code execution? I dont mean 'return', or 'dbstop', or 'error'. I just want to stop the code immediately, with no fanfare and just return to the command line. Strangley enough, i can't seem to find a command that does this! "Return" doesn't necessarily work if the current function is not the main function. "dbstop" puts it in debug mode. "quit" quits matlab.
I've looked and looked and can't find this simple command.
There seems to be no single command for exiting subscripts,
however there is a possibility of adding try/catch to the code at each stage.

For instance if you are running a script 3 levels down:

i.e.
command line
run (test.m) which in turn executes...
run (test2.m) which in turn executes...
run(test3.m)...

if script is running the function 'funcx.m'
and you wish to exit all levels to return to the command line.

Firstly, insert the try catch loop into test3.m in the following format.

try
[ ] = funcx.m( )
catch
error('your error message')
end

this will return the script to test2.m where a similar process is followed.

try
run (test3.m)
catch
clc
error('your error message')
end

the use of clc clears the error message previously displayed therefore leaving the command line free from the red error message.

By using the same format for each level the script can return to the highest level script. If important information needs to be kept on the command window this will need to be noted and written into the highest level script to avoid loss of information.

in test.m or your highest level script the following lines of code will return the script to the command line. (assuming the call function for test2.m is issued in a for or while loop)

try
run (test2.m)
catch
clc
(Any important messages etc.)
check = 1
break
end

(At the end of the for or while loop)

if check == 1
return
end

Providing all levels have been corrected the code will return the user to the command line.

Hope this helps people looking for a relatively neat solution to this issue.

Kind Regards

Brandon Ballard
Steven Lord
2016-07-19 15:50:03 UTC
Permalink
Raw Message
Post by Brandon Ballard
Post by Todd Welti
This might seem like a dumb question, but how do you stop code execution?
I dont mean 'return', or 'dbstop', or 'error'. I just want to stop the
code immediately, with no fanfare and just return to the command line.
Strangley enough, i can't seem to find a command that does this!
"Return" doesn't necessarily work if the current function is not the main
function. "dbstop" puts it in debug mode. "quit" quits matlab.
I've looked and looked and can't find this simple command.
There seems to be no single command for exiting subscripts,
however there is a possibility of adding try/catch to the code at each stage.
i.e.
command line
run (test.m) which in turn executes...
run (test2.m) which in turn executes...
run(test3.m)...
if script is running the function 'funcx.m'
and you wish to exit all levels to return to the command line.
Firstly, insert the try catch loop into test3.m in the following format.
*snip*
Post by Brandon Ballard
this will return the script to test2.m where a similar process is followed.
*snip*

Or just have test.m use TRY and CATCH. Uncaught exceptions will bubble up
through the stack until they reach the top (in which case they are thrown as
an error) or until they reach a TRY/CATCH block.
--
Steve Lord
***@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com
benbarrowes
2017-12-20 12:34:08 UTC
Permalink
Raw Message
Still no way to do this without throwing an error?

I need a way to cleanly stop execution and return to the Matlab prompt from within a function or script without pressing a key (Ctrl-C), without quitting Matlab (exit), and without throwing an error. "Return" and "break" do not do the job.
蕾宝708227
2017-12-21 05:41:45 UTC
Permalink
Raw Message
w***@gmail.com
2017-12-21 05:50:24 UTC
Permalink
Raw Message
Todd Welti於 2009年3月12日星期四 UTC+8上午5時03分00秒寫道:
Post by Todd Welti
This might seem like a dumb question, but how do you stop code execution? I dont mean 'return', or 'dbstop', or 'error'. I just want to stop the code immediately, with no fanfare and just return to the command line. Strangley enough, i can't seem to find a command that does this! "Return" doesn't necessarily work if the current function is not the main function. "dbstop" puts it in debug mode. "quit" quits matlab.
I've looked and looked and can't find this simple command.
【玖富娱乐】推荐主管QQ【708227】【玖富娱乐】推荐主管QQ【708227】【玖富娱乐】推荐主管QQ【708227】【玖富娱乐】推荐主管QQ【7082
Loading...