Set breakpoints for debugging—>Matlab调试命令

dbstop

Set breakpoints for debugging

Syntax

dbstop in file
dbstop in file at location
dbstop in file if expression
dbstop in file at location if expression
dbstop if condition
dbstop(b)

Description

example

dbstop in file setsa 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 itis paused.

example

dbstop in file at location setsa breakpoint at the specified location. MATLAB execution pausesimmediately before that location, unless the location is an anonymous function.If the location is an anonymous function, then execution pauses justafter the breakpoint.

example

dbstop in file if expression setsa conditional breakpoint at the first executable line of the file.Execution pauses only if expression evaluates totrue (1).

example

dbstop in file at location if expression setsa conditional breakpoint at the specified location. Execution pausesat or just before that location only if the expression evaluatesto true.

example

dbstop if condition pauses execution at the line that meets the specified condition, such as error 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 specified condition occurs.

example

dbstop(b) restoresbreakpoints you previously saved to b. The filescontaining the saved breakpoints must be on the search path or inthe current folder. MATLAB assigns breakpoints by line number,so the lines in the file must be the same as when you saved the breakpoints.

Examples

collapse all

Pause at First Executable Line

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

Create a file, buggy.m, that containsthese 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 debugmode.

2   n = length(x);
K>> 

Type dbquit to exit debug mode.

Pause at Function in File

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

Create a file, myfile.m, that containsthese statements

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

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

Set a breakpoint at myfunction.

 dbstop in myfile>myfunction

Pause in File After n Iterations of a Loop

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

Create a file, myprogram.m, that containsthese 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, when n = 4.

4   x(n) = x(n) + 1;
K>> 

Type dbquit to exit debug mode.

Pause If Error

Set a breakpoint and pause execution if a run-timeerror occurs.

Create a file, mybuggyprogram.m, thatcontains 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.

MException.last
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. Call mybuggyprogram.

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

Pause If NanInf

Set a breakpoint and pause execution if thecode returns a NaN value.

Create a file, buggy.m, that requiresan input vector.

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

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

dbstop if naninf
buggy(0:2)

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

NaN/Inf breakpoint hit for buggy on line 3.

Type dbquit to exit debug mode.

Restore Saved Breakpoints

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

Create a file, buggy.m, which containsthese statements.

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

Set an error breakpoint and a standard breakpoint at thesecond line in buggy.

dbstop at 2 in buggy
dbstop if error

Run dbstatus. MATLAB describesthe breakpoints you set.

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

Assign a structure representing the breakpoints to thevariable b, and then save b tothe MAT-file buggybrkpnts. Use b=dbstatus('-completenames') tosave absolute paths and the breakpoint function nesting sequence.

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

Clear all breakpoints.

dbclear all

Restore the breakpoints by loading the MAT-file and calling dbstop withthe saved structure, b.

load buggybrkpnts
dbstop(b)

Input Arguments

collapse all

file — File name
character vector

File name, specified as a character vector. The file name caninclude a partial path, but must be in a folder on the search pathor in the current folder.

Example: myfile.m

If the file name includes the -completenames option,then the file does not need to be on the search path, as long as thefile name is a Fully Qualified Name.

Example: c:\Program Files\MATLAB\myfile.m -completenames

In addition, file can include a filemarker(>) to specify the path to a particular localfunction 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.

location — Location in file to set a breakpoint
line number | line number and anonymous function number | local function name

Location in file to set a breakpoint, specifiedas one of these options:

  • Line number in file specified asa character vector. The default is 1.

  • Line number in file, at the anonymousfunction number, specified as a character vector. For example, 1@2 specifiesline number 1, at the second anonymous function. The default anonymousfunction number is 1.

  • Name of a local function in file,specified as a character vector.

Note

When setting a breakpoint, you cannot specify location if file includesa filemarker. For example, the command dbstop in myfile>myfilefunctionat 5 is invalid.

expression — Code that evaluates to scalar logical value 1 or 0
character vector

Code that evaluates (as if by eval)to a scalar logical value of 1 or 0,(true or false, respectively), specified as a character vector.

Example: n >= 4

condition — Statement that causes execution to pause when that condition evaluates to true
error | caught error | warning | naninf | ...

Statement that causes execution to pause when that conditionevaluates to true, specified as one of these options:

  • error — Run-time error thatoccurs outside a try/catch block. You cannot resumeexecution after an uncaught run-time error.

    If you want execution to pause only if a specific error occurs,specify the message id. For example:

    • dbstop if error pauses execution at the first run-time error that occurs outside a try/catch block.

    • dbstop if error MATLAB:ls:InputsMustBeStrings pausesexecution at the first run-time error outside a try/catch blockthat has a message ID of MATLAB:ls:InputsMustBeStrings.

  • caught error — Run-time error that occurs within the try portion of a try/catch block. If you want execution to pause only if a specific error occurs, specify the message id.

  • warning — Run-time warningoccurs. If you want execution to pause only if a specific warningoccurs, specify the message id.

    This condition has no effect if you disable warnings with the warning off all command or if you disable warnings for the specified id. For more information about disabling warnings, see warning.

  • naninf — The code returnsan infinite value (Inf) or a value that is nota number (NaN) as a result of an operator, functioncall, or scalar assignment.

b — List of breakpoints
structure array

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

More About

collapse all

Fully Qualified Name

A fully qualified name is an exact file namethat is uniquely specified such that it cannot be mistaken for anyother file on your system.

  • Windows® platforms — A file name that beginswith two back slashes (\\) or with a drive letterfollowed by a colon (:).

  • UNIX® platforms — A file name that beginswith a slash (/) or a tilde (~).

Tips

  • Before you begin debugging, make sure that your programis saved and that the program and any files it calls exist on yoursearch path or in the current folder.

  • To resume execution after a breakpoint pauses execution,use dbcont or dbstep. To exit debug mode, use dbquit.

  • MATLAB can become unresponsive when it pauses at a breakpoint while displaying a modal dialog box or figure created by your program. To exit debug mode and return to the MATLAB prompt (>>), use Ctrl+C.












参考:

1. Set breakpoints for debugging

2. Matlab 调试工具 dbstop 的使用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值