Desktop Tools and Development Environment |
Debugging is the process by which you isolate and fix problems with your code. Debugging helps to correct two kinds of errors:
In addition to finding and fixing problems with your M-files, you might want to improve the performance and make other enhancements using MATLAB tools.
Use the following techniques to isolate the causes of errors and improve your M-files.
Technique or Tool |
Description |
For More Information |
---|---|---|
Syntax highlighting |
Syntax highlighting helps you identify syntax errors in an M-file before you run the file. |
Syntax Highlighting |
Error messages |
When you run an M-file with a syntax error, MATLAB will most likely detect it and display an error message in the Command Window describing the error and showing its line number in the M-file. Click the underlined portion of the error message, or position the cursor within the message and press Ctrl+Enter. The offending M-file opens in the Editor/Debugger, scrolled to the line containing the error. To check for syntax errors in an M-file without running the M-file, use the pcode function. |
none |
Editor/ Debugger and Debugging Functions |
The MATLAB Editor/Debugger and debugging functions are useful for correcting run-time errors because you can access function workspaces and examine or change the values they contain. You can set and clear breakpoints, indicators that temporarily halt execution in an M-file. While stopped at a breakpoint, you can change workspace contexts, view the function call stack, and execute the lines in an M-file one by one. |
Debugging Example--The Collatz Problem and Debugging Process and Features |
Cells |
In the Editor/Debugger, isolate sections of an M-file, called cells, so you can easily make changes to and run a single section. |
Rapid Code Iteration Using Cells |
M-Lint |
Use M-Lint to help you verify the integrity of your code and learn about potential improvements. Access M-Lint from the Editor/Debugger by selecting Tools -> Check Code with M-Lint. |
M-Lint Code Check Report |
Profiler |
Use the Profiler to help you improve performance and detect problems in your M-files. Access the Profiler from the Editor/Debugger by selecting Tools -> Open Profiler. |
Profiling for Improving Performance |
Visual Directory and M-File Reports |
The Visual Directory tool and M-File Reports can help you polish and package M-files before providing them to others to use. Access all of these tools from the Current Directory browser. You can run one of these, the Dependency Report, for the current file directly from the Editor/Debugger--select Tools -> Show Dependency Report. |
Visual Directory in Current Directory Browser and Directory Reports in Current Directory Browser |
Other Useful Techniques for Finding and Correcting Errors
keyboard
statements to the M-file--keyboard
statements stop M-file execution at the point where they appear and allow you to examine and change the function's local workspace. This mode is indicated by a special prompt:
Resume function execution by typing return
and pressing the Enter key. For more information, see the keyboard
reference page.
depfun
function to see the dependent functions. Similarly, use the dependency report in the Visual Directory tools.
Debugging M-Files | Debugging Example--The Collatz Problem |
© 1994-2005 The MathWorks, Inc.