dbstop - Set breakpoints for debugging - MATLAB (original) (raw)

Set breakpoints for debugging

Syntax

Description

dbstop in [file](#buyrwwr-file) sets a breakpoint at the first executable line in file. When you run file, MATLAB® enters debug mode, pauses execution at the breakpoint, and displays the line where it is paused.

example

dbstop in [file](#buyrwwr-file) at [location](#buyrwwr-location) sets a breakpoint at the specified location. MATLAB execution pauses immediately before that location, unless the location is an anonymous function. If the location is an anonymous function, then execution pauses just after the breakpoint.

example

dbstop in [file](#buyrwwr-file) if [expression](#buyrwwr-expression) sets a conditional breakpoint at the first executable line of the file. Execution pauses only if expression evaluates to true (1).

example

dbstop in [file](#buyrwwr-file) at [location](#buyrwwr-location) if [expression](#buyrwwr-expression) sets a conditional breakpoint at the specified location. Execution pauses at or just before that location only if the expression evaluates to true.

example

dbstop if [condition](#buyrwwr-condition) pauses execution at the line that meets the specified condition, such aserror or naninf. Unlike other breakpoints, you do not set this breakpoint at a specific line in a specific file. MATLAB pauses at any line in any file when the specifiedcondition occurs.

example

dbstop([b](#buyrwwr-b)) restores breakpoints you previously saved to b. The files containing the saved breakpoints must be on the search path or in the current folder. MATLAB assigns breakpoints by line number, so the lines in the file must be the same as when you saved the breakpoints.

example

Examples

collapse all

Set a breakpoint and pause execution at the first executable line of a program.

Create a file, buggy.m, that contains these statements.

function z = buggy(x) n = length(x); z = (1:n)/x';

Issue the dbstop command and run buggy.

dbstop in buggy buggy(1:5)

MATLAB displays the line where it pauses and enters debug mode.

Type dbquit to exit debug mode.

Set a breakpoint in a program at the first executable line of a local function.

Create a file, myfile.m, that contains these statements

function n = myfile(x) n = myfunction(x);

function y = myfunction(x) y = x + 1;

Set a breakpoint at myfunction.

dbstop in myfile>myfunction

Set a breakpoint in a program that causes MATLAB to pause after some iterations of a loop.

Create a file, myprogram.m, that contains these statements

x = ones(1,10);

for n = 1:10 x(n) = x(n) + 1; end

Set a breakpoint to pause when n >= 4, and run the code.

dbstop in myprogram at 4 if n>=4 myprogram

MATLAB pauses at line 4 after 3 iterations of the loop, whenn = 4.

Type dbquit to exit debug mode.

Set a breakpoint and pause execution if a run-time error occurs.

Create a file, mybuggyprogram.m, that contains these statements.

x = ones(1,10);

for n = 1:10 x(n) = x(n+1) + 1; end

Set an error breakpoint, and call mybuggyprogram.

dbstop if error mybuggyprogram

A run-time error occurs, and MATLAB goes into debug mode, pausing at line 4 in mybuggyprogram.m.

Index exceeds matrix dimensions. Error in mybuggyprogram (line 4) x(n) = x(n+1) + 1; 4 x(n) = x(n+1) + 1;

Type dbquit to exit debug mode.

Run MException.last to obtain the error message identifier generated by the program.

ans =

MException with properties:

identifier: 'MATLAB:badsubscript'
   message: 'Index exceeds matrix dimensions.'
     cause: {}
     stack: [1×1 struct]

Clear the error breakpoint and set a new error breakpoint specifying the identifier of the error message to catch. Callmybuggyprogram.

dbclear if error dbstop if error MATLAB:badsubscript mybuggyprogram

The same run-time error occurs, and MATLAB goes into debug mode, pausing at line 4 inmybuggyprogram.m.

Index exceeds matrix dimensions. Error in mybuggyprogram (line 4) x(n) = x(n+1) + 1; 4 x(n) = x(n+1) + 1;

Type dbquit to exit debug mode.

Set a breakpoint and pause execution if the code returns aNaN or Inf value.

Create a file, buggy.m, that requires an input vector.

function z = buggy(x) n = length(x); z = (1:n)/x';

Set a warning breakpoint, and call buggy with an input vector containing a 0 as one of its elements.

dbstop if naninf buggy(0)

A division by zero error occurs, and MATLAB goes into debug mode, pausing at line 3 in buggy.m.

NaN/Inf breakpoint hit for buggy on line 3.

Type dbquit to exit debug mode.

Set, save, clear, and then restore saved breakpoints.

Create a file, buggy.m, which contains these statements.

function z = buggy(x) n = length(x); z = (1:n)/x';

Set an error breakpoint and a standard breakpoint at the second line inbuggy.

dbstop at 2 in buggy dbstop if error

Run dbstatus. MATLAB describes the breakpoints you set.

Breakpoint for buggy is on line 2. Stop if error.

Assign a structure representing the breakpoints to the variableb, and then save b to the MAT-filebuggybrkpnts. Useb=dbstatus('-completenames') to save absolute paths and the breakpoint function nesting sequence.

b = dbstatus('-completenames'); save buggybrkpnts b

Clear all breakpoints.

Restore the breakpoints by loading the MAT-file and callingdbstop with the saved structure,b.

load buggybrkpnts dbstop(b)

Input Arguments

collapse all

File name, specified as a character vector or string scalar. The file name can include a partial path name for files on the MATLAB search path or an absolute path name for any file. For more information on valid file names in MATLAB, see Specify File Names.

Example: myfile.m

In addition, file can include a filemarker (>) to specify the path to a particular local function or to a nested function within the file.

Example: myfile>myfunction

If file is not a MATLAB code file (for instance, it is a built-in or MDL-file), then MATLAB issues a warning. MATLAB cannot pause in the file, so it pauses before executing the file.

Data Types: char | string

Breakpoint location to set in file, specified as one of these options:

Note

When setting a breakpoint, you cannot specifylocation if file includes a filemarker. For example, the command dbstop in myfile>myfilefunction at 5 is invalid.

Data Types: char | string

Logical expression that evaluates to a scalar logical value of1 or 0, specified as a character vector or string scalar.

Example: n >= 4

Data Types: char | string

Pause condition, specified as one of these options:

List of breakpoints previously saved to a structure array using b=dbstatus.

Tips

Version History

Introduced before R2006a

expand all

Pause execution when unsuppressed output is displayed because the line is not suppressed by a semicolon (;). Outputs that are displayed as the result of a function call, such as from disp orfprintf will not stop execution.