The content of this document may be incorrect or outdated.
Print this article Edit this article
Debugging matlab m-files
MATLAB finds syntax errors during compilation. These errors are usually easy to fix. MATLAB can also encounter errors a run-time; these errors tend to be more difficult to track down, because the workspace local to the function is lost when the error forces a return to the MATLAB prompt and the base workspace. If you use the semicolon to suppress the display of intermediate results, you won't know where the error occurred.
To display the intermediate results, you can use any of these methods:
- Remove selected semicolons so intermediate results display.
- Add keyboard statements to allow to you examine the workspace state at the point where keyboard is issued.
- Comment out the leading function declaration so the M-file can be run as a script, making the intermediate results accessible in the base workspace.
- Use the MATLAB debugger.
Using the Debugger
Debugging Commands:
- dbstop - Set breakpoint
- dbclear - Remove breakpoint
- dbcont - Resume execution
- dbdown - Change local workspace context
- dbstack - List who called whom
- dbstatus - List all breakpoints
- dbstep - Execute one or more lines
- dbtype - List M-files with line numbers
- dbup - Change local workspace context
- dbquit - Quit debug mode
When you encounter an error in an M-file function, use the debugging commands to set breakpoints to help you debug the error. When execution stops at a breakpoint, that line displays, along with a keyboard command prompt. You can then enter any valid MATLAB command at the keyboard prompt. Some important points to remember about MATLAB's debugging commands are:
- The debugging commands work on M-files functions, not scripts.
- The M-file breakpoint information is closely associated with the compiled M-file. If the M-file is cleared either by editing or by the clear command, all M-file breakpoints are deleted. You can use this method when it is necessary to clear numerous breakpoints from one or more M-files.
A Debugging Session
- Begin the debug session by creating two M-files: test.m
and test1.m.
test.m
function a = test(b)
c = sqrt(b)*cos(b);
a = test1(b,c);
test1.m
function a = test1(b,c)
q = cond(b);
[w,e] = eig(c);
a = w*q;
- Use the dbtype command to list the line numbers of these
functions and the MATLAB supplied function cond
dbtype test
1 function a = test(b)
2 c = sqrt(b)*cos(b);
3 a = test1(b,c);
dbtype test1
1 function a = test1(b,c)
2 q = cond(b);
3 [w,e] = eig(c);
4 a = w*q;
dbtype cond
1 function y = cond(x)
2 % COND Matrix condition number.
3 % COND(X) is the ratio of the largest singular value of X
4 % to the smallest, which is the condition number of X in 2-norm
5
6 % See also RCOND and NORM
7
8 % J.N. Little 11-15-85
9 % Revised 3-9-87 JNL, 2-11-92 LS.
10 % Copyright (c) 1985-1992 by the MathWorks, Inc.
11
12 if length(x) == 0 % Handle null matrix
13 y = NaN;
14 return
15 end
16 if issparse(x)
17 error('Matrix must be non-sparse.')
18 end
19 s = svd(x);
20 if any(s == 0) % Handle singular matrix
21 disp('Condition is infinite')
22 y = Int;
23 return
24 end
25 y = max(s)./min(s);
- Create a variable in the base workspace
hi = 'hello again'
Set Breakpoints
Use the dbstop command to set a breakpoint in test.m
dbstop in test
This format causes execution to stop before the first executable line. It is equivalent to typing dbstop at 2 in test. Next, set breakpoint at line 19 in the cond M-file by entering
dbstop at 19 in cond
Run the M-file and Display the Stack
- After setting the breakpoints, execute test. As directed by
the dbstop command, execution stops at line 2:
test(magic(3))
2 c=sqrt(b)*cos(b);
- When the function stops, use dbstack to display the function
calls that led to the breakpoint. Since the function just started, only
one entry displays:
dbstack
in pathname test.m at line 2
- Continue execution of the function:
dbcont
Execution stops at line 19 in cond.
19 s = svd(x);
- Again, use dbstack to display the function calls:
dbstack
In pathname cond.m at line 19
In pathname test1.m at line 2
In pathname test.m at line 3
Check the Current Workspace and Variables
- Check the variables in the current workspace of cond:
who
Your variables are:
x y
- Check the contents of x:
x
x =
8 1 6
3 5 7
4 9 2
Execute the Next Line and Check Variables
- Use dbstep to execute line 20, which is the next line
to be executed:
dbstep
20 if any(s == 0) % Handle singular matrix
- Check the contents of s:
s
s = 15.0000
6.9282
3.4641
- Next, check the contents of the cond workspace again. Notice that
it now includes s:
who
Your variables are:
s x y
Change your Workspace and Check the Contents
- Now change to the workspace context of the function that called
cond:
dbup
In workspace belonging to pathname test1.m.
- Check the test1 workspace:
who
Your variables are:
a b c
- Check the contents of variable b in test1:
b
b = 8 1 6
3 5 7
4 9 2
- Then, check the contents of c:
c
c = -3.0026 -0.4199 2.4503
-4.1951 -0.8405 2.2478
-4.1854 0.6431 3.5935
- Check the contents of a:
a
Since a has not been assigned a value, empty brackets display.
a = []
- Change the workspace context to test, the function that called
test1:
dbup
In workspace belonging to pathname test.m.
- Change the workspace context to the base workspace, which called
test, and check the contents:
dbup
In base workspace.
who
Your variable are:
hi
Create a New Variable
Create a new variable in the base workspace, change the context of the workspace to the called M-file function, and check the contents.
- Type a new variable:
new_var = 123
When the M-file finishes, check this variable.
new_var =
123
- change the workspace context to test. This time,
use dbdown to change to the workspace of the called
M-file function:
dbdown
In workspace belonging to pathname test.m.
- Check the workspace contents for test:
whos
Name Size Elements Bytes Density Complex
a 0 by 0 0 0 full NO
b 3 by 3 9 72 full NO
c 3 by 3 9 72 full NO
Grand total is 18 elements using 144 bytes
- Check the contents of variable b:
b
b = 8 1 6
3 5 7
4 9 2
- Change the context to test1, then to cond:
dbdown
In workspace belonging to pathname test1.m.
dbdown
In workspace belonging to pathname cond.m.
Step Through the Function
- Continue executing cond by stepping through each executable line.
After cond finishes, step into test1:
dbstep
25 y = max(s)./min(s);
dbstep
End of M-file function cond.
dbstep
3 [w,e]=eig(c);
- Display the stack of called functions:
dbstack
In pathname test1.m at line 3
In pathname test.m at line 3
- Step to the next executable in test1:
dbstep
4 a=w*q
- Continue executing the function until another breakpoint, or until
the function returns to the base workspace:
dbcont
ans =
-2.0428 -2.0030 - 0.7944i -2.0030 + 0.7944i
-3.6832 0.5225 - 0.6343i 0.5115 + 0.6343i
-1.0056 -3.1257 - 1.9965i -3.1257 + 1.9165i
Display the Base Workspace
when the function completes, check the workspace.
whosNotice that the variable new_var that you created is in the base workspace:
Name Size Elements Bytes Density Complex
ans 3 by 3 9 144 Full Yes
new_var 1 by 1 1 8 Full No
Grand total is 10 elements using 152 bytes
new_var
new_var =
123
Stopping Debugging
You can stop debugging any time you think you know the cause of the problem. The dbquit command quits the debugging session and returns to the base workspace. Use this same example.
- First, verify the breakpoints using dbstatus. Type
dbstatus test
Breakpoints are on lines 2.
- Start the program again.
test(magic(3))
You may suspect that the variable b in the test workspace is the cause of the problem. You can inspect the value of b at the breakpoint's prompt. Type
2 c=sqrt(b)*cos(b);
b
b =
8 1 6
3 5 7
4 9 2
- When you think you know the solution of the problem, type dbquit
to quit the debugging session and return to the base workspace.
Note that dbquit does not clear the breakpoints, which you can
see with dbstatus:
dbstatus test
To clear breakpoints, use dbclear
Break points for test are on line 2.
Resources used for this FAQ:
MATLAB User's Guide for UNIX Workstations. December 1994.
Last Modified:
Dec 19, 2016 11:12 am US/Eastern
Created:
Mar 20, 2007 4:56 pm GMT-4
by
admin
JumpURL: