Desktop Tools and Development Environment Previous page   Next Page

Conditional Breakpoints

Set conditional breakpoints to cause MATLAB to stop at a specified line in a file only when the specified condition is met. One particularly good use for conditional breakpoints is when you want to examine results after a certain number of iterations in a loop. For example, set a breakpoint at line 10 in collatzplot, specifying that MATLAB should stop only if N is greater than or equal to 2. This section covers the following topics:

Setting Conditional Breakpoints

To set a conditional breakpoint, follow these steps:

  1. Click in the line where you want to set the conditional breakpoint. Then select Set/Modify Conditional Breakpoint from the Debug or context menu. If a standard breakpoint already exists at that line, use this same method to make it conditional.
  1. The MATLAB Editor conditional breakpoint dialog box opens as shown in this example.

    Image of conditional breakpoint dialog box.

  1. Type a condition in the dialog box, where a condition is any legal MATLAB expression that returns a logical scalar value. Click OK. As noted in the dialog box, the condition is evaluated before running the line. For the example, at line 10 in collatzplot, enter
  1. as the condition. A yellow breakpoint icon (indicating the breakpoint is conditional) appears in the breakpoint alley at that line.

    Image of line with conditional breakpoint.

When you run the file, MATLAB enters debug mode and pauses at the line only when the condition is met. In the collatzplot example, MATLAB runs through the for loop once and pauses on the second iteration at line 10 when N is 2. If you continue executing, MATLAB pauses again at line 10 on the third iteration when N is 3.

Copying, Modifying, Disabling, and Clearing Conditional Breakpoints

To copy a conditional breakpoint, right-click the icon in the breakpoint alley and select Copy from the context menu. Then right-click in the breakpoint alley at the line where you want to paste the conditional breakpoint and select Paste from the context menu.

Modify the condition for the breakpoint in the current line by selecting Set/Modify Conditional Breakpoint from the Debug or context menu.

Click a conditional breakpoint icon to disable it. Click a disabled conditional breakpoint to clear it.

Function Alternative for Conditional Breakpoints

Use the dbstop function with appropriate arguments to set conditional breakpoints from the Command Window, and use dbclear to clear them. Use dbstatus to view the breakpoints currently set, including any conditions, which are listed in the expression field. If no condition exists, the value in the expression field is [  ] (empty). For details, see the function reference pages: dbstop, dbclear, and dbstatus.


Previous page  Running Sections in M-Files That Have Unsaved Changes Breakpoints in Anonymous Functions Next page

© 1994-2005 The MathWorks, Inc.