10
$\begingroup$

Like the title says, I want to be able to stop the code at a specific location and have it halt safely. I cannot find a command to do it like for example in FORTRAN there is the stop command.

$\endgroup$
15
$\begingroup$

The function error('error message'); will exit your program and print the error message to the console.

$\endgroup$
2
$\begingroup$

There is break to exit a loop and return to return to the invoking function or keyboard/promt.

$\endgroup$
1
$\begingroup$

Here is a function that does it (without printing any message):

function stoprun()
  ms.message='';
  ms.stack = dbstack('-completenames');
  ms.stack(1:end) = [];
  ds = dbstatus();
  stoponerror = any(strcmp('error', {ds.cond}));
  setappdata(0, 'dberrorkeep', stoponerror);
  dbclear error
  error(ms);
end
$\endgroup$
-2
$\begingroup$

you can use "return" and it will "return control to the invoking function before it reaches the end of the function", which for the main program means exiting the program.

$\endgroup$
  • $\begingroup$ What happen in the case that you don't have a function? $\endgroup$ – nicoguaro Oct 7 '14 at 19:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.