Debugging Matlab Code

Here are some tips, in no particular order, on debugging matlab code. Rather than running all of your code at once, test it by typing it line-by-line into the matlab window. As it is an interpreter, this will work (with the caveat that all variables will be Global). Do not put a semicolon at the end of the line if you would like to see the output. For example, if you downloaded by code you could type the following:
>> D = randData

D =

     1     0     1     1

>> X = noise(.4,D)

X =

     1     0     0     1

For help on any matlab command, just type "help" followed by the name of the command.
To use the matlab debugger, you create breakpoints with the command "dbstop". For example, you could type
>> dbstop if error
which will cause matlab to enter the debugger whenever there is an error. Type "help dbstop" for more info.
The command "who" gives you a list of variables currently defined.
One of the most common bugs encountered when programming matlab is that matlab is always happy to create a new variable for you, even if you just mistyped the name of an old variable. For example, consider the following:

>> ops = [3 4 5];
>> oops = ops + 1;
>> ops

ops =

     3     4     5
If you program in Matlab long enough, you will be bitten by this bug!
Dan Spielman
Last modified: Sun Sep 8 15:28:18 EDT 2002