Thread Subject: how to stop execution

Subject: how to stop execution

From: Todd Welti

Date: 11 Mar, 2009 21:03:00

Message: 1 of 12

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.

Subject: how to stop execution

From: Walter Roberson

Date: 11 Mar, 2009 21:44:29

Message: 2 of 12

Todd Welti wrote:
> 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.

Subject: how to stop execution

From: Matt Fig

Date: 11 Mar, 2009 22:05:05

Message: 3 of 12

ctrl+c

Subject: how to stop execution

From: Todd Welti

Date: 11 Mar, 2009 23:50:21

Message: 4 of 12

"Matt Fig" <spamanon@yahoo.com> wrote in message <gp9cig$lul$1@fred.mathworks.com>...
> ctrl+c

I meant a command that would be in the code.

Subject: how to stop execution

From: Todd Welti

Date: 11 Mar, 2009 23:52:02

Message: 5 of 12

Walter Roberson <roberson@hushmail.com> wrote in message <XgWtl.34251$l71.6225@newsfe23.iad>...
> Todd Welti wrote:
> > 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?

Subject: how to stop execution

From: Jos

Date: 12 Mar, 2009 07:54:04

Message: 6 of 12

"Todd Welti" <twelti@harman.com> wrote in message <gp9ir2$kcf$1@fred.mathworks.com>...
> Walter Roberson <roberson@hushmail.com> wrote in message <XgWtl.34251$l71.6225@newsfe23.iad>...
> > Todd Welti wrote:
> > > 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

Subject: how to stop execution

From: Peter O'Connor

Date: 31 Mar, 2010 18:27:07

Message: 7 of 12

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;




"Jos " <#10584@fileexchange.com> wrote in message <gpaf2s$hc1$1@fred.mathworks.com>...
> "Todd Welti" <twelti@harman.com> wrote in message <gp9ir2$kcf$1@fred.mathworks.com>...
> > Walter Roberson <roberson@hushmail.com> wrote in message <XgWtl.34251$l71.6225@newsfe23.iad>...
> > > Todd Welti wrote:
> > > > 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

Subject: how to stop execution

From: Todd Welti

Date: 31 Mar, 2010 19:02:20

Message: 8 of 12

"Peter O'Connor" <peter.oconnor@mail.mcgill.ca> wrote in message <hp045r$h0$1@fred.mathworks.com>...
> 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.

Subject: how to stop execution

From: Walter Roberson

Date: 31 Mar, 2010 19:47:45

Message: 9 of 12

Peter O'Connor wrote:
> 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;

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.

Subject: how to stop execution

From: Mike Hoff

Date: 29 Oct, 2011 02:18:15

Message: 10 of 12

"Todd Welti" <twelti@harman.com> wrote in message <gp98u4$g9h$1@fred.mathworks.com>...
> 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

Subject: how to stop execution

From: Radek

Date: 28 Jan, 2012 21:45:11

Message: 11 of 12

"Todd Welti" <twelti@harman.com> wrote in message <gp98u4$g9h$1@fred.mathworks.com>...
> 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

Subject: how to stop execution

From: Todd Welti

Date: 30 Jan, 2012 21:17:11

Message: 12 of 12

"Radek " <dzidmail@gmail.com> wrote in message <jg1q97$gv7$1@newscl01ah.mathworks.com>...
> "Todd Welti" <twelti@harman.com> wrote in message <gp98u4$g9h$1@fred.mathworks.com>...
> > 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

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.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
break Panagiotis 30 Jan, 2012 11:33:26
break Radek 28 Jan, 2012 16:43:50
abort execution Mazvydas Tadaravicius 26 Jan, 2011 19:27:58
execution Todd Welti 31 Mar, 2010 15:04:12
stop Todd Welti 31 Mar, 2010 15:04:12
code Todd Welti 31 Mar, 2010 15:04:11
rssFeed for this Thread

Contact us at files@mathworks.com