Setting breakpoints pauses the execution of your MATLAB® program so that you can examine values where you think an issue might have occurred. You can set breakpoints interactively in the Editor or Live 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, when MATLAB reaches a breakpoint, it opens the file containing the breakpoint. To disable this option:
From the Home tab, in the Environment section, click Preferences.
In the Preferences window, select MATLAB > Editor/Debugger.
Clear the Automatically open file when MATLAB reaches a breakpoint option and click OK.
A standard breakpoint pauses at a specific line in a file. To set a standard breakpoint, click the gray area to the left of the executable line where you want to set the breakpoint. Alternatively, you can press the F12 key to set a breakpoint at the current line. 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 three in a
dbstop in plotRand at 3
When debugging a file that contains a loop, set the breakpoint inside the loop to
examine the values at each increment of the loop. Otherwise, if you set the
breakpoint at the start of the loop, MATLAB pauses at the loop statement only once. For example, this code creates
an array of ten ones and uses a
for loop to perform a
calculation on items two through six of the
x = ones(1:10); for n = 2:6 x(n) = 2 * x(n-1); end
forloop (a total of five times), set a breakpoint at line four.
A conditional breakpoint causes MATLAB to pause at a specific line in a file only when the specified condition is met. For example, you can use conditional breakpoints when you want to examine results after some iterations in a loop.
To set a conditional breakpoint, right-click the gray area to the left of the executable line where you want to set the breakpoint and select Set Conditional Breakpoint. If a breakpoint already exists on that line, select Set/Modify Condition. In the dialog box that opens, enter a condition and click OK. A condition is any valid MATLAB expression that returns a logical scalar value.
When you run the code, MATLAB evaluates the condition before running the line. If the condition is
met, MATLAB enters debug mode and pauses at the line. For example, this code
creates an array of ten ones and uses a
for loop to perform a
calculation on items two through six of the
x = one(1:10) for n = 2:6 x(n) = 2 * x(n-1); end
Set a conditional breakpoint at line four with the condition
4. When you run the code, MATLAB runs through the
for loop twice and pauses on the
third iteration at line four when
you continue running the code, MATLAB pauses again at line four on the fourth iteration when
5, and then once more, when
You also can set a conditional breakpoint programmatically using the
dbstop function. For example, to add a conditional breakpoint in
myprogram.m at line six, type:
dbstop in myprogram at 6 if n>=4
In the Editor, you can set an error breakpoint to have MATLAB pause and enter debug mode if MATLAB encounters an issue. Setting error breakpoints is not supported in the Live Editor.
Unlike standard and conditional breakpoints, you do not set error breakpoints at a specific line or 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
NaN (not-a-number) or
Alternatively, you can set an error 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. You can set a breakpoint for the line itself and for each anonymous function in the line.
To set a breakpoint on a line containing an anonymous function, click the gray area to the left of the line. MATLAB adds a breakpoint for the line, and a disabled breakpoint for each anonymous function in the line. To enable a breakpoint for an anonymous function, click the disabled breakpoint for that function.
To view information about all the breakpoints on a line, place your cursor on the breakpoint icon. A tooltip appears with available information. For example, in this code, line seven contains two anonymous functions, with a breakpoint at each one.
When you set a breakpoint in an anonymous function, MATLAB pauses when the anonymous function is called. The line highlighted in
green is where the code defines the anonymous function. The line highlighted in gray
is where the code calls the anonymous functions. For example, in this code,
MATLAB pauses the program at a breakpoint set for the anonymous function
g, defined at line seven, and called at line eight.
A dark gray breakpoint indicates an invalid breakpoint.
Breakpoints are invalid for these reasons:
Unsaved changes in the file. To make breakpoints valid, save the file. The gray breakpoints become red, indicating that they are now valid.
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 an issue or if you are using conditional breakpoints.
To disable a breakpoint, right-click the breakpoint icon, and select Disable Breakpoint from the context menu.
The breakpoint becomes light gray to indicate that it is disabled.
To reenable a breakpoint, right-click the breakpoint icon and select Enable Breakpoint from the context menu.
The gray breakpoints become red, and program execution pauses at that line.
To enable or disable all breakpoints in the file, right-click the gray area to the left of an executable line and select Enable All Breakpoints in File or Disable All Breakpoints in File. These options are available only 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. Alternatively, you can press the F12 key to clear the breakpoint.
To clear a breakpoint programmatically, use the
dbclear function. For example, to
clear the breakpoint at line six 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 Breakpoints in File. You can also use the
all command. For example, to clear all the breakpoints in a file
dbclear all in myprogram
To clear all breakpoints in all files, including error
breakpoints, right-click the breakpoint alley and select Clear All
Breakpoints. You also can use the
Breakpoints clear automatically when you end a MATLAB session. To save your breakpoints for future sessions, use the