| MATLAB Environment | Search  Help Desk |
Debug Menu
The Debug menu provides an interface to the graphical M-file Debugger. These topics contain instructions for debugging in MATLAB:About the Debugger
The MATLAB Debugger helps you identify programming errors in your MATLAB code. Using the Debugger, you can:About Breakpoints
The Debugger allows you to set and clear breakpoints, specific lines in an M-file at which execution halts. It also lets you change workspace contexts, view the function call stack, and execute the lines in an M-file one by one. Noteclear Mfile, all of Mfile's breakpoints are also cleared.
Starting Debugging
To start debugging:Debugger Toolbar Buttons
Toolbar Button
Purpose
Description
Equivalent Command
Set/Clear Breakpoint
Set or clear a breakpoint at the line containing the cursor.
dbstop/dbclear
Clear All Breakpoints
Clear all breakpoints that are currently set.
dbclear all
Step In
Execute the current line of the M-file and, if the line is a call to another function, step into that function.
dbstep in
Single Step
Execute the current line of the M-file.
dbstep
Continue
Continue execution of M-file until completion or until another breakpoint is encountered.
dbcont
Quit Debugging
Exit the debugging state.
dbquit
NaN or Inf value.
Debugger Notations
(
) indicates that a breakpoint is set at that line. If the line selected for a breakpoint is not a valid executable line, then the breakpoint is set at the next executable line.
(
) shows the next line to execute when execution of an M-file pauses at a breakpoint.
(
) indicates a pause at the end of the script or function so you can examine variables before returning to the calling function.
Evaluating Variables
You can check values of variables from the Debugger. Highlight the text of each variable and right-click to bring up the context menu and choose Evaluate Selection. Or, alternatively, choose Evaluate Selection from the View menu. Both the selection and the result are displayed in the Command Window.Changing Workspace Context
Use the Stack pull-down menu in the upper-right corner of the Debugger window to change workspace contexts:Check the workspace context using whos or the graphical Workspace Browser. The variables you have created show up in the listing.
Ending a Debug Session
Select the Exit Editor/Debugger from the File menu to end the debugging session.Debugging from the Command Line
The MATLAB debugging commands are a set of tools that allow you to debug your M-files from the command line. The most general form for each debugging command is shown below.
Description
Syntax
Set breakpoint.
dbstop at line_num in file_name
Remove breakpoint.
dbclear at line_num in file_name
Stop on warning, error, or NaN/Inf generation.
dbstop if warning
error
naninf
infnan
Resume execution.
dbcont
List function call stack.
dbstack
List all breakpoints.
dbstatus file_name
Execute one or more lines.
dbstep nlines
List M-file with line numbers.
dbtype file_name
Change local workspace context (down).
dbdown
Change local workspace context (up).
dbup
Quit debug mode.
dbquit
When execution stops at a breakpoint, you're automatically in keyboard mode, as indicated by the K>> prompt. At this prompt, you can type standard MATLAB commands. When you're finished, type dbcont to resume execution.
Note