Computer Aided Design (CAD)
Matlab Debugging
Dr. Sawsan Abdellatif 1
Reference:
Matlab by Example: Programming Basics, Munther Gdeisat
2
3
Bugs: Errors in programs.
Debugging: the process of locating and eliminating bugs
1- Syntax Error 2- Runtime Error
Ex. misspelling of variable or Runtime errors are found by Matlab
function names or missing quotes or during the execution of a program
parentheses..
Matlab checks syntax first before Example:
executing commands.
Matlab give some information helps
in identifying and fixing this error.
Examples:
4
3- Logical Error
Logical errors occur when the program compiles and runs
successfully but produces the wrong answer.
Example: if we need Matlab program to calculate the absolute values of a vector
(Ex. Values from -10 to 10)
Note: the result is not as required. So we need to fix this bug using Matlab’s
debugging capabilities to track down the location and exact nature of the bug.
5
Break point
Set break point as shown then “Run” the code. The
Command Prompt changes to:
This means debug mode
Matlab starts executing the program and stops at line 1.
6
To direct Matlab to run the command in line 1, click on the Step icon. Matlab runs the
first line and then proceeds to line 2.
7
After determining the error and fix it. Then step through the program
First step indicate correct operation
First 3 steps indicate correct operation 8
Step In Tool: Track bugs inside functions
Step Out Tool: To run the function all together in a single step. This
executes the rest of the commands in the function and returns the
control to the file from which we call the function.