| Products & Services | Industries | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB |
| Contents | Index |
| Learn more about MATLAB |
Use the Stack field
in the Editor or in the Workspace browser.
dbstack
dbstack(n)
dbstack('-completenames')
[ST,I] = dbstack(...)
dbstack displays the line numbers and M-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 M-file function is reached. Each line number is a hyperlink you can click to go directly to that line in the Editor. The notation functionname>subfunctionname is used to describe the subfunction location.
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:
| file | The file in which the function appears. This field is the empty string if there is no file. |
| name | Function name within the file. |
| line | Function line number. |
The current workspace index is returned in I.
If you step past the end of an M-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.
In addition to using dbstack while debugging, you can also use dbstack within an M-file outside the context of debugging. In this case, to get and analyze information about the current M-file stack. For example, to get the name of the calling M-file, use dbstack with an output argument within the file being called. For example:
st=dbstack;
This example shows the information returned when you issue dbstack while debugging an M-file:
dbstack 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 =
1
dbclear, dbcont, dbdown, dbquit, dbstatus, dbstep, dbstop, dbtype, dbup, evalin, mfilename, whos
MATLAB Desktop Tools and Development Environment Documentation
![]() | dbquit | dbstatus | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |