GLG410/598--Computers in Earth and Space Exploration


Announcements Syllabus Schedule Weekly lecture notes Assignments Links

Matlab Debugging Strategies


A bug is a flaw in the program

**** Debugging requires practice, experience, and PATIENCE!!

We will try to give you general strategies to help as a guide, but there is no right or wrong way to go about debugging. The only rule is find the bug and fix it!

Variable Inspection

Variable Inspection is the key to finding most bugs. You want to be able to look at your variables as they are being created while the program is running.

Ways to inspect variables:
  1. omit semicolons
  2. use disp or fprintf
  3. plot results - Matlab is super-awesome at this!
  4. array editor
Bugs are often found because variables contain something other than what was intended.

Error and Warning Messages

You have no doubt ALL seen these by now. You try and run your code, something breaks, your computer may beep at you, and Matlab displays a red message that maybe something like this:
>> km2deg
??? Input argument "km" is undefined.

Error in ==> km2deg at 27
deg = km / conv;
>>
This is a typical error message. In this case, the program has aborted due to a severe problem. The error MUST be fixed in order for the code to work properly.
There are other times that a message may be displayed in black, but the program was NOT stopped and continued to run until the end. It may look something like this:
>> 891 / 0
Warning: Divide by zero.

ans =

   Inf

>>
This is a typical warning message. In this case, we tried to divide by zero, but the program still gave us an answer (infinity). Depending on the effect of this computation this MAY or MAY NOT be necessary to fix.
If you are running a script/function that calls another function, error messages can be stacked on top of each other to indicate where the errors occurred within EACH script/function that was being executed at the time. It may look like this:
>> TZtranscoefs
??? Error using ==> mpower
Matrix must be square.

Error in ==> rtcoef at 22
a = (p2*(1 - (2*(b2^2)*(RayP^2)))) - (p1*(1 - (2*(b1^2)*(RayP^2))));

Error in ==> TZtranscoefs at 6
coefs = rtcoef(2.9,6.8,3.9,3.3807,8.1106,4.4910,rayp);

>>
In this case, the top-most error message is where you want to start looking for your error.

Types of Bugs

  1. Typographic Errors
  2. Syntax Errors
  3. Algorithmic Errors

1. Typographic Errors

These are simply typing errors. You either typed the name of a function or variable wrong.
>>% Function name typos are usually easy to catch
cod(pi)
??? Undefined command/function 'cod'.

>>% Variable name typos are sometimes harder to catch
>>x=cos(y);		% instead of x=cos(t);
The second example may hang you up later in the code when you need to use x. What might happen if t was a different size than y? It could be a problem.

2. Syntax Errors

A syntax error results when the order, structure, or form of a function call is incorrect. These can occur when a funciton is called with inputs that are the wrong shape, size, and/or type or are otherwise not valid.
>> cos('10.2')
??? ??? Function 'cos' is not defined for values of class 'char'.
Deciphering syntax related error messages can sometimes be difficult, and tracking down where the problem originated can be even harder.

3. Algorithmic Errors

An algorithmic error occurs when a program completes its execution, but the results are unintended or unexpected. For example you wrote a program to add two numbers, passed it 2, 3 and received 6 as a result with no error or warning messages.

2 Strategies for Solving Algorithmic Errors
  1. Do the calculation by hand or with a calculator.
  2. Find somewhere that uses the same algorithm and gives you an answer. Then check your calculations against theirs. It helps to verify the credibility of the source, as it is conceivable that others may have an unknown error as well.

Using Matlab's Tools for Debugging

Matlab has some great built-in tools to help you debug your code. All of the ones that I use are in Matlab's Editor. Let me run through an example to show you how I go about debugging a code:



I made my vector rayp and tried to call the function rtcoef, but it is giving me an error. This error is occurring at line 22, so let's open rtcoef in the Matlab Editor and inspect line 22.



Well, I don't see anything that is immediately wrong with this function, so lets just place a breakpoint at line 22. A breakpoint is a place in the code where we want to temporarily "pause" the execution so that we can inspect our variables! Remember, that is the key to finding out what is going wrong. We place our "breakpoint" by clicking the dashed line to the left of line 22.



Now that we have our breakpoint, lets run the last line of code that calls rtcoef and see what happens.



Our function "paused" AT line 22 and is giving us a K>>. It is waiting for us to do something. In the meantime, check out our workspace. It has been populated with variables that have been created WITHIN the rtcoef function, along with the input variables that were passed to it. This is a great time to start "playing" with the variables! (I've also drawn some hints on the figure to help direct you to some of the features in the debug mode.)



So I played around with parts of line 22 to try and isolate the part of the equation that is giving me problems. I started typing in the equation AT THE COMMAND LINE. I think I found the problem with RayP^2.



It turns out that RayP is a 1x100 matrix and I am trying to square it. However, what I REALLY want to do is not square the whole matrix, but square EACH ELEMENT of the matrix. This is where I need to add the "period" in front of the ^ so I would type RayP.^2. Lets just type that out to make sure it works.



YES!!! That was the problem! You see I typed:
K>> temp = RayP.^2;
K>>
And temp is a 1x100 matrix of the answer to RayP.^2. Then, I fixed ALL instances where I know that I need a "period" (.* ./ .^). I then re-type line 22 with all of my "fixes".
K>> a = (p2.*(1 - (2.*(b2.^2).*(RayP.^2)))) - (p1.*(1 - (2.*(b1.^2).*(RayP.^2))));
K>>
And sure enough, IT WORKS!!!