10

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.

4 Answers4

15

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

Godric Seer
  • 4,637
  • 2
  • 23
  • 38
2

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

Dirk
  • 1,738
  • 10
  • 22
1

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
-2

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.

Nikola
  • 1