MATLAB Language Working with Breakpoints



In software development, a breakpoint is an intentional stopping or pausing place in a program, put in place for debugging purposes.

More generally, a breakpoint is a means of acquiring knowledge about a program during its execution. During the interruption, the programmer inspects the test environment (general purpose registers, memory, logs, files, etc.) to find out whether the program is functioning as expected. In practice, a breakpoint consists of one or more conditions that determine when a program's execution should be interrupted.


Breakpoints in MATLAB


In MATLAB, when execution pauses at a breakpoint, variables existing in the current workspace (a.k.a. scope) or any of the calling workspaces, can be inspected (and usually also modified).

Types of Breakpoints

MATLAB allow users to place two types of breakpoints in .m files:

  • Standard (or "unrestricted") breakpoints (shown in red) - pause execution whenever the marked line is reached.
  • "Conditional" breakpoints (shown in yellow) - pause execution whenever the marked line is reached AND the condition defined in the breakpoint is evaluated as true.

Different breakpoint symbols in the GUI

Placing Breakpoints

Both types of breakpoints can be created in several ways:

  • Using the MATLAB Editor GUI, by right clicking the horizontal line next to the line number.

  • Using the dbstop command:

    % Create an unrestricted breakpoint:
    dbstop in file at location
    % Create a conditional breakpoint:
    dbstop in file at location if expression
    % Examples and special cases: 
    dbstop in fit at 99 % Standard unrestricted breakpoint.
    dbstop in fit at 99 if nargin==3 % Standard conditional breakpoint.
    dbstop if error % This special type of breakpoint is not limited to a specific file, and
                    % will trigger *whenever* an error is encountered in "debuggable" code.
    dbstop in file % This will create an unrestricted breakpoint on the first executable line
                   % of "file".
    dbstop if naninf % This special breakpoint will trigger whenever a computation result 
                     % contains either a NaN (indicates a division by 0) or an Inf
  • Using keyboard shortcuts: the default key for creating a standard breakpoint on Windows is F12; the default key for conditional breakpoints is unset.

Disabling and Re-enabling Breakpoints

Disable a breakpoint to temporarily ignore it: disabled breakpoints do not pause execution. Disabling a breakpoint can be done in several ways:

  • Right click on the red/yellow breakpoint circle > Disable Breakpoint.
  • Left click on a conditional (yellow) breakpoint.
  • In the Editor tab > Breakpoints > Enable\Disable.

Removing Breakpoints

All breakpoints remain in a file until removed, either manually or automatically. Breakpoints are cleared automatically when ending the MATLAB session (i.e. terminating the program). Clearing breakpoints manually is done in one of the following ways:

  • Using the dbclear command:

    dbclear all
    dbclear in file   
    dbclear in file at location    
    dbclear if condition
  • Left clicking a standard breakpoint icon, or a disabled conditional breakpoint icon.

  • Right clicking on any breakpoint > Clear Breakpoint.

  • In the Editor tab > Breakpoints > Clear All.

  • In pre-R2015b versions of MATLAB, using the command clear.

Resuming Execution

When execution is paused at a breakpoint, there are two ways to continue executing the program:

  • Execute the current line and pause again before the next line.

    F101 in the Editor, dbstep in the Command Window, "Step" in Ribbon > Editor > DEBUG.

  • Execute until the next breakpoint (if there are no more breakpoints, the execution proceeds until the end of the program).

    F121 in the Editor, dbcont in the Command Window, "Continue" in Ribbon > Editor > DEBUG.

1 - default on Windows.