Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Set Breakpoints

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:

  • Standard breakpoints

  • Conditional breakpoints

  • Error 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:

  1. From the Home tab, in the Environment section, click Preferences.

    The Preferences dialog box opens.

  2. Select MATLAB > Editor/Debugger.

  3. Clear the Automatically open file when MATLAB reaches a breakpoint option and click OK.

Note

Debugging using the graphical debugger is not supported in live scripts. For more information, see What Is a Live Script?

Standard Breakpoints

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. 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.

You also can set a standard breakpoint programmatically using the dbstop function. For example, to add a breakpoint at line 2 in a file named myprogram.m, type:

dbstop in myprogram at 2
MATLAB adds a breakpoint at line 2 in the function myprogram.

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.

Conditional Breakpoints

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 Set/Modify Condition.

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 myprogram.m.

Add a breakpoint with the following condition at line 6:

n >= 4
A yellow, conditional breakpoint icon appears in the breakpoint alley at that line.

You also can set a standard breakpoint programmatically using the dbstop function. For example, to add a conditional breakpoint in myprogram.m at line 6 type:

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 n is 4. If you continue executing, MATLAB pauses again at line 6 on the fourth iteration when n is 5.

Error Breakpoints

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 NaN (not-a-number) or 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, type

dbstop if error
To pause execution at the first run-time error within the try portion of a try/catch block that has a message ID of MATLAB:ls:InputsMustBeStrings, type
dbstop if caught error MATLAB:ls:InputsMustBeStrings

Breakpoints in Anonymous Functions

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.

Invalid Breakpoints

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.

Disable Breakpoints

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.

An X appears through the breakpoint icon to indicate that it is disabled.

To reenable a breakpoint, right-click the breakpoint icon and select Enable Breakpoint from the context menu.

The 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.

Clear Breakpoints

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 dbclear function. For example, to clear the breakpoint at line 6 in a file called myprogram.m, type

 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 dbclear all command. For example, to clear all the breakpoints in a file called myprogram.m, type

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 dbstatus function.

Related Topics

Was this topic helpful?