Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
an exit(0) function

Subject: an exit(0) function

From: Chris

Date: 13 Jan, 2011 23:05:51

Message: 1 of 12

Hey, Is there some way in Matlab to use a C-style exit(0) function? The reason I ask, is I want to be able to break out of nested functions without displaying anything on the command line (as assert and error both do),

Thanks!

Subject: an exit(0) function

From: Matt Fig

Date: 13 Jan, 2011 23:25:04

Message: 2 of 12

Chris <skynet414@gmail.com> wrote in message <2071624916.24176.1294959981193.JavaMail.root@gallium.mathforum.org>...
> Hey, Is there some way in Matlab to use a C-style exit(0) function? The reason I ask, is I want to be able to break out of nested functions without displaying anything on the command line (as assert and error both do),
>
> Thanks!

I am not a C expert, but I suspect the RETURN function could do what you want.

Subject: an exit(0) function

From: I am too much me

Date: 14 Jan, 2011 00:53:00

Message: 3 of 12

On 13/01/11 5:25 PM, Matt Fig wrote:
> Chris <skynet414@gmail.com> wrote in message
> <2071624916.24176.1294959981193.JavaMail.root@gallium.mathforum.org>...
>> Hey, Is there some way in Matlab to use a C-style exit(0) function?
>> The reason I ask, is I want to be able to break out of nested
>> functions without displaying anything on the command line (as assert
>> and error both do),
>>
>> Thanks!
>
> I am not a C expert, but I suspect the RETURN function could do what you
> want.

No, exit(0) corresponds to quit(). Yes, that does quit the matlab
session entirely, but then exit(0) in C ends execution of the executable
entirely and there is no equivalent in C to "return to the main
function" (though there the C setjump() longjump() )

Subject: an exit(0) function

From: Husam Aldahiyat

Date: 14 Jan, 2011 06:31:05

Message: 4 of 12

There is no such thing as exit(0) in MATLAB. I've searched for two months for such a command with no results, and I'm 99% sure about it.

Subject: an exit(0) function

From: Husam Aldahiyat

Date: 14 Jan, 2011 06:32:05

Message: 5 of 12

Chris <skynet414@gmail.com> wrote in message <2071624916.24176.1294959981193.JavaMail.root@gallium.mathforum.org>...
> Hey, Is there some way in Matlab to use a C-style exit(0) function? The reason I ask, is I want to be able to break out of nested functions without displaying anything on the command line (as assert and error both do),
>
> Thanks!

Subject: an exit(0) function

From: I am too much me

Date: 14 Jan, 2011 06:46:38

Message: 6 of 12

On 14/01/11 12:31 AM, Husam Aldahiyat wrote:
> There is no such thing as exit(0) in MATLAB. I've searched for two
> months for such a command with no results, and I'm 99% sure about it.

quit() is a pretty close approximation. You can't control the return
status, though.

Subject: an exit(0) function

From: Chris

Date: 14 Jan, 2011 11:57:43

Message: 7 of 12

It seems there is no such function, anyone have ideas on how I could create my own exit() function? I can't seem to find the source for quit() and error() which is annoying.

Also I found a bug in mlint after creating the script

disp(123)
quit cancel
disp(456)

MLint gives the warning that the disp(456) line can't be reached, but running the script prints 456!

Subject: an exit(0) function

From: Bruno Luong

Date: 14 Jan, 2011 12:47:04

Message: 8 of 12

Chris <skynet414@gmail.com> wrote in message <2071624916.24176.1294959981193.JavaMail.root@gallium.mathforum.org>...
> Hey, Is there some way in Matlab to use a C-style exit(0) function? The reason I ask, is I want to be able to break out of nested functions without displaying anything on the command line (as assert and error both do),
>

Why not use TRY/CATCH in the main function, rethrows the error if a real error occurs, and simply returns if the dummy error is detected.

Bruno

Subject: an exit(0) function

From: Bruno Luong

Date: 14 Jan, 2011 12:53:04

Message: 9 of 12

"Husam Aldahiyat" wrote in message <igoqj8$hnq$1@fred.mathworks.com>...
> There is no such thing as exit(0) in MATLAB. I've searched for two months for such a command with no results, and I'm 99% sure about it.

I have a project idea for you Husam. Extend your GOTO emulator to be able to jump any point to to another m file.

GOTO anothermfile:labelx;

Bruno

Subject: an exit(0) function

From: Steven_Lord

Date: 14 Jan, 2011 15:51:27

Message: 10 of 12



"Chris" <skynet414@gmail.com> wrote in message
news:777612481.26978.1295006293276.JavaMail.root@gallium.mathforum.org...
> It seems there is no such function, anyone have ideas on how I could
> create my own exit() function? I can't seem to find the source for quit()
> and error() which is annoying.

Those are built-in functions implemented in the source code for MATLAB, and
we do not share the source code for MATLAB.

> Also I found a bug in mlint after creating the script
>
> disp(123)
> quit cancel
> disp(456)
>
> MLint gives the warning that the disp(456) line can't be reached, but
> running the script prints 456!

Yes, this does seem like Code Analyzer assumes that anything after a call to
QUIT is unreachable and didn't take into account the 'cancel' input. Still,
you shouldn't be using "quit cancel" anywhere but your finish.m file, as it
doesn't do anything when used outside finish.m as per the documentation.

http://www.mathworks.com/help/techdoc/ref/quit.html

I'll note that to the development staff.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlab.wikia.com/wiki/FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: an exit(0) function

From: Husam Aldahiyat

Date: 16 Feb, 2011 15:45:04

Message: 11 of 12

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <igpgvg$9ji$1@fred.mathworks.com>...
> "Husam Aldahiyat" wrote in message <igoqj8$hnq$1@fred.mathworks.com>...
> > There is no such thing as exit(0) in MATLAB. I've searched for two months for such a command with no results, and I'm 99% sure about it.
>
> I have a project idea for you Husam. Extend your GOTO emulator to be able to jump any point to to another m file.
>
> GOTO anothermfile:labelx;
>
> Bruno

Not even close. Next update will allow a jump to a label in another code written in another language.

Subject: an exit(0) function

From: Joshua

Date: 3 May, 2014 18:07:11

Message: 12 of 12

Chris <skynet414@gmail.com> wrote in message <2071624916.24176.1294959981193.JavaMail.root@gallium.mathforum.org>...
> Hey, Is there some way in Matlab to use a C-style exit(0) function? The reason I ask, is I want to be able to break out of nested functions without displaying anything on the command line (as assert and error both do),
>
> Thanks!

You can do this with if statements.

When you use the 'return' function it will return control to where it came from. So if you called the file from MATLAB's main window, 'return' will have the same effect as 'exit(0).'

But if you have a main function that is calling others then at the start of your main function set an EXIT flag manually and then constantly check the flag status throughout the main code.

For example, if you have a main routine, named MainRoutine, that calls two files named, SubRoutineA and SubRoutineB and you want to exit the entire routine if a condition in SubRoutineA is met (in this case if B = 2, exit), write the code as follows.


For SubRoutineA:

....(whatever your code is)...

if B == 2
     EXIT = 1; %Set exit flag to one
     return; %Return control to main function
end

...(whatever else you want in your code)...



Then for MainRoutine:

EXIT = 0; %Define exit flag and set to 0 right at the start to avoid problems

....(whatever your code is)...

SubRoutineA;

if EXIT == 1; %Test exit flag
      return; %Return control to main MATLAB window
end

SubRoutineB;

...(whatever else you want in your code)...



I hope that helps

Tags for this Thread

No tags are associated with this thread.

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.

Contact us