Access Keys:
Skip to content (Access Key - 0)

How can I debug a .m file?

MATLAB provides a debugging facility that allows you to stop at specific points in your .m files, examine the workspace and step through execution of your code. For example, while developing a function ops.m, an error occurs. Matlab will give an error message similar to:

   ??? Error using ==> ones
   Too many input arguments.

   Error in ==> /afs/
   On line 10  ==> A = ones(n,n,n);

The first 2 lines explain the error, and the next two give the location of the error. To use the debugging facility to determine what is happening, you start with the "dbstop" command. This command provides a number of options for stopping execution of a Matlab function. A particularly useful option is

         >> dbstop if error

This causes a stop in any M-file function causing a run-time error. Then just run the Matlab command. In this case

         >> ops(5)

Execution will stop at the point where the error occurs, and you will get the Matlab prompt back so that you can examine variables or step through execution from that point. In the example:

         >> ops(5)
        10  A = ones(n,n,n);

The command "dbstep" allows you to step through execution one line at a time. You can continue execution with the "dbcont" command. To exit debug mode, type "dbquit".

For more information, see the Matlab help for the following topics:

debug, dbstop, dbclear, dbcont, dbdown, dbstack, dbstatus, dbstep, dbtype, dbup, dbquit

IS&T Contributions

Documentation and information provided by IS&T staff members

Last Modified:

February 18, 2009

Get Help

Request help
from the Help Desk
Report a security incident
to the Security Team
olc-matlab olc-matlab Delete
matlab matlab Delete
debug debug Delete
matlab-review matlab-review Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
This product/service is:
Easy to use
Difficult to use

This article is:
Adaptavist Theme Builder (4.2.3) Powered by Atlassian Confluence 3.5.13, the Enterprise Wiki