Set breakpoints for debugging—>Matlab调试命令

来源:互联网 发布:浅析网络直播 编辑:程序博客网 时间:2024/06/05 12:02

dbstop

Set breakpoints for debugging

collapse all in page

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 infile.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 ananonymous 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 ifexpression 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 theexpression evaluatesto true.

example

dbstop if condition pauses execution at the line that meets the specifiedcondition, such as error ornaninf. 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) 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 runbuggy.

dbstop in buggybuggy(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 Aftern 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:10x(n) = x(n) + 1;end

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

dbstop in myprogram at 4 if n>=4myprogram

MATLAB pauses at line 4 after 3 iterations of the loop, whenn = 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:10x(n) = x(n+1) + 1;end

Set an error breakpoint, and call mybuggyprogram.

dbstop if errormybuggyprogram

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

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

dbclear if errordbstop if error MATLAB:badsubscriptmybuggyprogram

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.

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 naninfbuggy(0:2)

A division by zero error occurs, and MATLAB goes into debugmode, pausing at line 3 inbuggy.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 inbuggy.

dbstop at 2 in buggydbstop 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 thevariableb, and then save b tothe MAT-filebuggybrkpnts. 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 callingdbstop withthe saved structure, b.

load buggybrkpntsdbstop(b)

Input Arguments

collapse all

fileFile 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 aFully 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 pausein the file, so it pauses before executing the file.

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

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

  • 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 commanddbstop in myfile>myfilefunctionat 5 is invalid.

expressionCode that evaluates to scalar logical value 1 or0
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

conditionStatement 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 atry/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 atry/catch block.

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

  • caught error — Run-time error that occurs within thetry 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 thewarning off all command or if you disable warnings for the specifiedid. For more information about disabling warnings, seewarning.

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

bList of breakpoints
structure array

List of breakpoints previously saved to a structure array usingb=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,usedbcont ordbstep. To exit debug mode, usedbquit.

  • 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 (>>), useCtrl+C.












参考:

1. Set breakpoints for debugging

2. Matlab 调试工具 dbstop 的使用

阅读全文
0 0
原创粉丝点击