Setting breakpoints pauses the execution of your MATLAB® program so that you can examine values where you think a problem might be. You can set breakpoints using the Editor or by using functions in the Command Window.
There are three types of breakpoints:
You can set breakpoints only at executable lines in saved files that are in the current folder or in folders on the search path. You can set breakpoints at any time, whether MATLAB is idle or busy running a file.
By default, MATLAB automatically opens files when it reaches a breakpoint. To disable this option:
From the Home tab, in the Environment section, click Preferences.
The Preferences dialog box opens.
Select MATLAB > Editor/Debugger.
Clear the Automatically open file when MATLAB reaches a breakpoint option and click OK.
A standard breakpoint pauses at a specified line in a file.
To set a standard breakpoint click the breakpoint alley at an executable line where you want to set the breakpoint. The breakpoint alley is the narrow column on the left side of the Editor, to the right of the line number. You also can use the F12 key to set the breakpoint.
Executable lines are indicated by a — (dash) in the breakpoint alley. If an executable statement spans multiple lines, you can set a breakpoint at each line in that statement, even though the additional lines do not have a — (dash) in the breakpoint alley. For example, in this code, you can set a breakpoint at all four lines:
If you attempt to set a breakpoint at a line that is not executable, such as a comment or a blank line, MATLAB sets it at the next executable line.
To set a standard breakpoint programmatically, use the
dbstop function. For example, to add a breakpoint at line 2 in a
dbstop in myprogram at 2
To examine values at increments in a
for loop, set the breakpoint
within the loop, rather than at the start of the loop. If you set the breakpoint at the
start of the for loop, and then step through the file, MATLAB pauses at the
for statement only once. However, if you
place the breakpoint within the loop, MATLAB pauses at each pass through the loop.
A conditional breakpoint causes MATLAB to pause at a specified line in a file only when the specified condition is met. Use conditional breakpoints when you want to examine results after some iterations in a loop.
To set a conditional breakpoint, right-click the breakpoint alley at an executable line
where you want to set the breakpoint and select
When the Editor dialog box opens, enter a condition and click OK. A condition is any valid MATLAB expression that returns a logical scalar value.
As noted in the dialog box, MATLAB evaluates the condition before running the line. For example, suppose that you
have a file called
Add a breakpoint with the following condition at line 6:
n >= 4
You also can set a conditional breakpoint programmatically using the
dbstop function. For example, to add a conditional breakpoint in
myprogram.m at line 6
dbstop in myprogram at 6 if n>=4
When you run the file, MATLAB enters debug mode and pauses at the line when the condition is met. In the
myprogram example, MATLAB runs through the
for loop twice and pauses on the third
iteration at line 6 when
4. If you continue
executing, MATLAB pauses again at line 6 on the fourth iteration when
An error breakpoint causes MATLAB to pause program execution and enter debug mode if MATLAB encounters a problem. Unlike standard and conditional breakpoints, you do not set these breakpoints at a specific line in a specific file. When you set an error breakpoint, MATLAB pauses at any line in any file if the error condition specified occurs. MATLAB then enters debug mode and opens the file containing the error, with the execution arrow at the line containing the error.
To set an error breakpoint, on the Editor tab, click Run and select from these options:
Pause on Errors to pause on all errors.
Pause on Warnings to pause on all warnings.
Pause on NaN or Inf to pause on
Inf (infinite) values.
You also can set a breakpoint programmatically by using the
dbstop function with a specified
condition. For example,
to pause execution on all errors,
dbstop if error
tryportion of a
try/catchblock that has a message ID of
dbstop if caught error MATLAB:ls:InputsMustBeStrings
You can set multiple breakpoints in a line of MATLAB code that contains anonymous functions. For example, you can set a breakpoint for the line itself, where MATLAB software pauses at the start of the line. Or, alternatively, you can set a breakpoint for each anonymous function in the line.
When you add a breakpoint to a line containing an anonymous function, the Editor asks where in the line you want to add the breakpoint. If there is more than one breakpoint in a line, the breakpoint icon is blue, regardless of the status of any of the breakpoints on that line.
To view information about all the breakpoints on a line, hover your pointer on the breakpoint icon. A tooltip appears with available information. For example, in this code, line 5 contains two anonymous functions, with a breakpoint at each one. The tooltip tells us that both breakpoints are enabled.
When you set a breakpoint in an anonymous function, MATLAB pauses when the anonymous function is called. A green arrow shows
where the code defines the anonymous function. A white arrow shows where the code
calls the anonymous functions. For example, in this code, MATLAB pauses the program at a breakpoint set for the anonymous function
sqr, at line 2 in a file called
myanonymous.m. The white arrow indicates that the
sqr function is called from line 3.
A gray breakpoint indicates an invalid breakpoint.
Breakpoints are invalid for these reasons:
There are unsaved changes in the file. To make breakpoints valid, save the file. The gray breakpoints become red, indicating that they are now valid.
There is a syntax error in the file. When you set a breakpoint, an error message appears indicating where the syntax error is. To make the breakpoint valid, fix the syntax error and save the file.
You can disable selected breakpoints so that your program temporarily ignores them and runs uninterrupted. For example, you might disable a breakpoint after you think you identified and corrected a problem, or if you are using conditional breakpoints.
To disable a breakpoint, right-click the breakpoint icon, and select Disable Breakpoint from the context menu.
X appears through the breakpoint icon to indicate that it is
To reenable a breakpoint, right-click the breakpoint icon and select Enable Breakpoint from the context menu.
X no longer appears on the breakpoint icon and program execution
pauses at that line.
To enable or disable all breakpoints in the file, select Enable All in File or Disable All in File. These options are only available if there is at least one breakpoint to enable or disable.
All breakpoints remain in a file until you clear (remove) them or until they are cleared automatically at the end of your MATLAB session.
To clear a breakpoint, right-click the breakpoint icon and select Clear Breakpoint from the context menu. You also can use the F12 key to clear the breakpoint.
To clear a breakpoint programmatically, use the
dbclear function. For example, to
clear the breakpoint at line 6 in a file called
dbclear in myprogram at 6
To clear all breakpoints in the file, right-click the breakpoint alley and select
Clear All in File. You can also use the
all command. For example, to clear all the breakpoints in a file called
dbclear all in myprogram
To clear all breakpoints in all files, including error breakpoints,
right-click the breakpoint alley and select Clear All. You also
can use the
dbclear all command.
Breakpoints clear automatically when you end a MATLAB session. To save your breakpoints for future sessions, see the