Function call stack


[ST,I] = dbstack(...)


dbstack displays the line numbers and file names of the function calls that led to the current breakpoint, listed in the order in which they were executed. The display lists the line number of the most recently executed function call (at which the current breakpoint occurred) first, followed by its calling function, which is followed by its calling function, and so on. This continues until the topmost MATLAB® function is reached. Each line number is a hyperlink you can click to go directly to that line in the Editor. The notation functionname>localfunctionname is used to describe the location of the local function.

dbstack(n) omits the first n frames from the display. This is useful when issuing a dbstack from within an error handler, for example.

dbstack('-completenames') outputs the "complete name" (the absolute file name and the entire sequence of functions that nests the function in the stack frame) of each function in the stack.

Either none, one, or both n and '-completenames' can appear. If both appear, the order is irrelevant.

[ST,I] = dbstack(...) returns the stack trace information in an m-by-1 structure, ST, with the fields:

fileThe file in which the function appears. This field is the empty string if there is no file.
nameFunction name within the file.
lineFunction line number.

The current workspace index is returned in I.

If you step past the end of a file, dbstack returns a negative line number value to identify that special case. For example, if the last line to be executed is line 15, then the dbstack line number is 15 before you execute that line and -15 afterwards.


This example shows the information returned when you issue dbstack while debugging a MATLAB code file:


In /usr/local/matlab/toolbox/matlab/cond.m at line 13
In test1.m at line 2
In test.m at line 3

This example shows the information returned when you issue dbstack while debugging lengthofline.m to get the complete name of the file, the function name, and line number in which the function appears:

[ST,I] = dbstack('-completenames')
ST = 
    file: 'I:\MATLABFiles\mymfiles\lengthofline.m'
    name: 'lengthofline'
    line: 28
I =

Related Examples

More About

collapse all


In addition to using dbstack while debugging, you can also use dbstack within a MATLAB code file outside the context of debugging. In this case, to get and analyze information about the current file stack. For example, to get the name of the calling file, use dbstack with an output argument within the file being called. For example:


Introduced before R2006a

Was this topic helpful?