Tatsuya Yokota
Tatsuya Yokota

Reputation: 429

Matlab equivalent of Python exit function for debugging purposes

I was wondering what the common practice in Matlab is when you want to make a program terminate at a certain point to do simple checks like seeing the shape of arrays, etc.

In Python, I usually insert exit() at the place I want my program to terminate.

I looked at the documentation for Matlab and here it says there is an exit statement, but this results in closing the Matlab GUI, and thus I am not able to check the output to the Matlab terminal.

I understand that I can simply terminate the program manually when running it, but that gives me less control over when exactly to terminate.

Upvotes: 0

Views: 1071

Answers (2)

nekomatic
nekomatic

Reputation: 6284

As long as you're running your MATLAB code in the MATLAB IDE, and it isn't a Live Script, then the simplest way of doing this is to manually set a breakpoint on the line of interest, by clicking to the right of the line number:

MATLAB editor with breakpoint set

When execution reaches the line with the breakpoint it will pause and you can browse the contents of the workspace, type commands in the command window, or use the Continue, Step or Quit Debugging buttons in the toolbar.

This is basically the same as what dbstop and its related commands are doing, but more convenient (IMHO) while developing in the IDE. I can't think of a good justification to prefer the command-line version unless you're restricted to text-only input for some reason.

Using breakpoints is a pretty fundamental part of debugging in any language, Python included. If the IDE you're currently using for Python doesn't support them then I'd strongly recommend you consider changing to one that does.

Upvotes: 1

Ramashalanka
Ramashalanka

Reputation: 8864

You are looking for dbstop: https://mathworks.com/help/matlab/ref/dbstop.html

"The dbstop function is used to temporarily stop the execution of a program and give the user an opportunity to examine the local workspace." For the use you describe, the most likely form will be dbstop in FILESPEC at LINENO, e.g. dbstop in myfunction at 34.

Upvotes: 1

Related Questions