| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB |
| Contents | Index |
| Learn more about MATLAB |
| On this page… |
|---|
A Quick Way to Examine Variables Setting Breakpoints from the Command Line Finding Line Numbers to Set Breakpoints Stopping Execution on an Error or Warning |
For a brief description of the main debug functions in MATLAB, type
help debug
For more information: See Debugging Process and Features in the MATLAB Desktop Tools and Development Environment documentation.
Other functions you may find useful in debugging are listed below.
Function | Description |
|---|---|
Display function or script code as it executes. | |
Display specified values or messages. | |
Display formatted data of different types. | |
List variables in the workspace. | |
Show array dimensions. | |
Interrupt program execution and allow input from keyboard. | |
Resume execution following a keyboard interruption. | |
Display specified warning message. | |
Access information on the cause of an error. | |
Return warning message that was last issued. |
Learn to use the MATLAB graphical debugger. You can view the function and its calling functions as you debug, set and clear breakpoints, single-step through the program, step into or over called functions, control visibility into all workspaces, and find and replace strings in your files.
Start out by opening the file you want to debug using File > Open or the open function. Use the debugging functions available on the toolbar and pull-down menus to set breakpoints, run or step through the program, and examine variables.
For more information: See Debugging Process and Features in the MATLAB Desktop Tools and Development Environment documentation.
To see the value of a variable from the Editor/Debugger window, hold the mouse cursor over the variable name for a second or two. You will see the value of the selected variable displayed.
You can set breakpoints with dbstop in any of the following ways:
Break at a specific M-file line number.
Break at the beginning of a specific subfunction.
Break at the first executable line in an M-file.
Break when a warning, or error, is generated.
Break if any infinite or NaN values are encountered.
For more information: See Setting Breakpoints in the MATLAB Desktop Tools and Development Environment documentation.
When debugging from the command line, a quick way to find line numbers for setting breakpoints is to use dbtype. The dbtype function displays all or part of an M-file, also numbering each line. To display delaunay.m, use
dbtype delaunay
To display only lines 35 through 41, use
dbtype delaunay 35:41
Use dbstop if error to stop program execution on any error and enter debug mode. Use dbstop if warning to stop execution on any warning and enter debug mode.
For more information: See Debugging Process and Features in the MATLAB Desktop Tools and Development Environment documentation.
Click on the underlined text in an error message, and MATLAB opens the M-file being executed in its editor and places the cursor at the point of error.
For more information: See Finding Errors, Debugging, and Correcting M-Files in the MATLAB Desktop Tools and Development Environment documentation.
You can detect erroneous or unexpected behavior in your programs by inserting warning messages that MATLAB will display under the conditions you specify. See the section on Warning Control in the MATLAB Programming Fundamentals documentation to find out how to selectively enable warnings.
For more information: See the warning function reference page.
An easy way to see the end result of a particular line of code is to edit the program and temporarily remove the terminating semicolon from that line. Then, run your program and the evaluation of that statement is displayed on the screen.
For more information: See Finding Errors, Debugging, and Correcting M-Files in the MATLAB Desktop Tools and Development Environment documentation.
Scripts store their variables in a workspace that is shared with the caller of the script. So, when you debug a script from the command line, the script uses variables from the base workspace. To avoid errors caused by workspace sharing, type clear all before starting to debug your script to clear the base workspace.
![]() | Program Development | Variables | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |