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:
exit or finish an m-file program but not exit matlab

Subject: exit or finish an m-file program but not exit matlab

From: Hugh Thompson

Date: 22 Dec, 2009 08:14:04

Message: 1 of 4


Hello,

I'm searching for something that would terminate an m-file program, sort of like "return 0" in C++. I have a condition that, if true, should give certain results and then stop the program. But I don't want to exit matlab, I want to see the results. I just don't want to continue with the rest of the code.
I know about "quit" and "exit", but these shut down matlab, so I'm just wondering if there is anything else or should I put "if - else" statements to run the parts of the code that I want?
Thank you!
Hugh

Subject: exit or finish an m-file program but not exit matlab

From: Herve

Date: 22 Dec, 2009 09:06:03

Message: 2 of 4

>> doc return
>> doc break

Subject: exit or finish an m-file program but not exit matlab

From: pbuchsbaum

Date: 19 Aug, 2010 18:54:11

Message: 3 of 4

It doesn't works because "return" just exit from
the current function and "break" just exit from a
current inner loop.

The only solution that I know is not nice. But it works.
Put in your code:
           error('Program exit')
The program ends full of messages

Where everything is OK, you can use in the main program

try
 program
catch exception
end

And in any point just write

   throw(exception)

The program will be quite and no message will be showed. However, the programs doesn't not show any other error, because they suppose that you handle all them.

Subject: exit or finish an m-file program but not exit matlab

From: ImageAnalyst

Date: 19 Aug, 2010 19:59:52

Message: 4 of 4

A better way is to just use return, like if your script had this in
it:

x = 10
if x > 5
fprintf(1, 'Condition met, exiting early.\n');
return;
else
fprintf(1, 'Condition not met, continuing as normal.\n');
end
fprintf(1, 'Normal exit.\n');

In the above, it will hit the return and gracefully exit early

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