Debugging M-Files Steve Gu Feb 08, 2008

Download Report

Transcript Debugging M-Files Steve Gu Feb 08, 2008

Debugging M-Files
Steve Gu
Feb 08, 2008
Outline
•
•
•
•
•
What’s Debugging?
Types of Errors
Finding Errors
Debugging Example
Using Debugging Features
What’s Debugging?
• Debugging is the process by which you can
– Recognize a bug exists
– Isolate source of bug
– Identify cause of bug
– Determine fix for bug
Types of Errors
• Syntax Errors
– Misspelling
– Omitting parenthesis
– MATLAB detects most syntax error and display error
message in the command window
• Run-time Errors
– Modify the wrong variables
– Perform a calculation incorrectly
– Apparent when an M-file produces unexpected result
Find Errors
• For syntax errors
– Easy
– MATLAB’s error messages
• For Run-time errors
– More difficult
– Some useful techniques:
•
•
•
•
•
Remove ;
Add keyboard statements  K>> type return
Run M-file as a script
Use the ‘depfun’: e.g,list = depfun('binomial','-toponly');
Use the MATLAB Editor/Debugger or debugging functions
Debugging Example –Binomial.m
Using Debugging Features
•
•
•
•
•
•
Preparing for Debugging
Setting Breakpoints
Running an M-File with Breakpoints
Stepping through an M-File
Examining Values
Correcting Problems and Ending Debugging
Preparing for Debugging
• Open the file – To use the Editor/Debugger
• Save changes
Setting Breakpoint
• Setting Breakpoints Using the Editor
– Set/Clear Breakpoint
• Function Equivalent
– dbstop in binomial at 14
– Some Useful related functions
• dbtype :Lists the M-file code
• dbstatus :Lists breakpoints
Running an M-File with Breakpoints
• Run M-file from command window or Editor
• K>> -------MATLAB is in debug mode
• The program is paused at the first breakpoint
• The function displayed in the Stack changes to
reflect the current function
Stepping Through an M-File
Examining Values
• Viewing Datatips in the Editor/Debugger
• Viewing Values in the Array Editor
Viewing Values in Array Editor
Ending Debugging
•
•
•
•
Quit Debugging
Clear all the breakpoints in the file
Make changes to the M-file
Save M-file