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/athena.mit.edu/software/vizdev/matlab/ops.m 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); K>>
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