Notice! This document is currently in Archived status.
The content of this document may be incorrect or outdated.

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

  1. Begin the debug session by creating two M-files: test.m and test1.m.
    function a = test(b)
    c = sqrt(b)*cos(b);
    a = test1(b,c);

    function a = test1(b,c)
    q = cond(b);
    [w,e] = eig(c);
    a = w*q;

  2. 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
    6 % See also RCOND and NORM
    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.
    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);
  3. 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

  1. After setting the breakpoints, execute test. As directed by the dbstop command, execution stops at line 2:
    2 c=sqrt(b)*cos(b);
  2. 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:
    in pathname test.m at line 2
  3. Continue execution of the function:
    19 s = svd(x);
    Execution stops at line 19 in cond.
  4. Again, use dbstack to display the function calls:
    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

  1. Check the variables in the current workspace of cond:
    Your variables are:
    x y
  2. Check the contents of x:
    x =
    8 1 6
    3 5 7
    4 9 2

Execute the Next Line and Check Variables

  1. Use dbstep to execute line 20, which is the next line to be executed:
    20 if any(s == 0) % Handle singular matrix
  2. Check the contents of s:
    s = 15.0000
  3. Next, check the contents of the cond workspace again. Notice that it now includes s:
    Your variables are:
    s x y

Change your Workspace and Check the Contents

  1. Now change to the workspace context of the function that called cond:
    In workspace belonging to pathname test1.m.
  2. Check the test1 workspace:
    Your variables are:
    a b c
  3. Check the contents of variable b in test1:
    b = 8 1 6
    3 5 7
    4 9 2
  4. Then, check the contents of c:
    c = -3.0026 -0.4199 2.4503
    -4.1951 -0.8405 2.2478
    -4.1854 0.6431 3.5935
  5. Check the contents of a:
    a = []
    Since a has not been assigned a value, empty brackets display.
  6. Change the workspace context to test, the function that called test1:
    In workspace belonging to pathname test.m.
  7. Change the workspace context to the base workspace, which called test, and check the contents:
    In base workspace.
    Your variable are:

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.

  1. Type a new variable:
          new_var = 123
    new_var =
    When the M-file finishes, check this variable.
  2. change the workspace context to test. This time, use dbdown to change to the workspace of the called M-file function:
    In workspace belonging to pathname test.m.
  3. Check the workspace contents for test:
    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
  4. Check the contents of variable b:
    b = 8 1 6
    3 5 7
    4 9 2
  5. Change the context to test1, then to cond:
    In workspace belonging to pathname test1.m.
    In workspace belonging to pathname cond.m.

Step Through the Function

  1. Continue executing cond by stepping through each executable line. After cond finishes, step into test1:
    25 y = max(s)./min(s);
    End of M-file function cond.
    3 [w,e]=eig(c);
  2. Display the stack of called functions:
    In pathname test1.m at line 3
    In pathname test.m at line 3
  3. Step to the next executable in test1:
    4 a=w*q
  4. Continue executing the function until another breakpoint, or until the function returns to the base workspace:
    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.

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
Notice that the variable new_var that you created is in the base workspace:
new_var =

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.

  1. First, verify the breakpoints using dbstatus. Type
          dbstatus test
    Breakpoints are on lines 2.
  2. Start the program again.
    2 c=sqrt(b)*cos(b);
    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
    b =
    8 1 6
    3 5 7
    4 9 2
  3. 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
    Break points for test are on line 2.
    To clear breakpoints, use dbclear

Resources used for this FAQ:

MATLAB User's Guide for UNIX Workstations.  December 1994. 

Last modified: 2007/03/20 16:56:56.173000 GMT-4 by Unknown
Created: 2007/03/20 16:56:56.173000 GMT-4 by brian.r.brinegar.1.



Type in a few keywords describing what information you are looking for in the text box below.

Admin Options: Edit this Document