Programming |
Debugging
This section covers the following topics:
The MATLAB Debug Functions
For a brief description of the main debug functions in MATLAB, type
For more information: See Debugging M-Files in the MATLAB Desktop Tools and Development Environment documentation.
More Debug Functions
Other functions you may find useful in debugging are listed below.
Function |
Description |
echo |
Display function or script code as it executes. |
disp |
Display specified values or messages. |
sprintf ,fprintf |
Display formatted data of different types. |
whos |
List variables in the workspace. |
size |
Show array dimensions. |
keyboard |
Interrupt program execution and allow input from keyboard. |
return |
Resume execution following a keyboard interruption. |
warning |
Display specified warning message. |
error |
Display specified error message. |
lasterr |
Return error message that was last issued. |
lasterror |
Return last error message and related information. |
lastwarn |
Return warning message that was last issued. |
The MATLAB Graphical Debugger
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 M-Files and Using Debugging Features in the MATLAB Desktop Tools and Development Environment documentation.
A Quick Way to Examine Variables
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.
Setting Breakpoints from the Command Line
You can set breakpoints with dbstop
in any of the following ways:
NaN
values are encountered.
For more information: See Setting Breakpoints in the MATLAB Desktop Tools and Development Environment documentation.
Finding Line Numbers to Set Breakpoints
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 copyfile.m
, use
To display only lines 70 through 90, use
Stopping Execution on an Error or Warning
Use dbstop
if error
to stop program execution on any error and enter debug mode. Use warning
debug
to stop execution on any warning and enter debug mode.
For more information: See Debug, Backtrace, and Verbose Modes in the MATLAB Programming documentation.
Locating an Error from the Error Message
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 Types of Errors in the MATLAB Desktop Tools and Development Environment documentation.
Using Warnings to Help Debug
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 and Data Types" documentation to find out how to selectively enable warnings.
For more information: See the warning
function reference page.
Making Code Execution Visible
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 in the MATLAB Desktop Tools and Development Environment documentation.
Debugging Scripts
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 |
© 1994-2005 The MathWorks, Inc.