## How to deal with errors in MATLAB

Errors are part of all human endeavors. We (hopefully) learn from then but that is not often much comfort. When you use the computer to create something like a new numerical routine or a simple data analysis, you are bound to make error - from simple typos to subtle errors in logic.

This page is meant to help solve some common problems that first-time MATLAB programmers encounter. The current offerings are:

If you come across a really good error, send it to me for inclusion in this list.

[Back to main page]

### Steps to take when you get an error

1. Tell yourself that the computer does not have it in for you
2. Articulate clearly what you are trying to do
3. Read the error message carefully and completely. Copy it into a file, if need be.
4. From the error message, identify what is causing the error and why (if possible)
5. Read the manual and on-line help information on the function (if that is the problem) or on the topic (especially if you are learning MATLAB)
6. See if other people have encountered such an error and work with them to resolve it
7. If your friends cannot determine the problem or find a solution, ask the instructor (or local expert)
8. If the local expert is stumped, consider sending a news message to comp.soft-sys.matlab and ask for assistance there
9. If the experts cannot solve it, it may be a bug in MATLAB (not very common but it is possible).
10. Tell yourself that the computer does not have it in for you

[OPTIONS]

### Omitting operators in expressions

Remember that MATLAB does not understand a statement like

```   b(3 + a)
```
to mean "b times the quantity (3 + a)". Rather, MATLAB interprets it as "the (3+a)th element of b". This interpretation is not likely to give you the result you are expecting. To produce the desired result, be explicit about the multiplication operation.
`   b*(3 + a)`

[OPTIONS]

MATLAB error messages can sometimes be very helpful and can sometimes be quite obscure. The degree to which a message can helps is significantly improved if you spend a little time reading what information is provided in the message.

Chapter 7 in Pratap's book (see references) is a good place to start.

• Read the whole message. Start from the last typed command to the new cursor location. For example, I wrote a script called plotex and tried to run it:
```   >> plotex                    <= This is the last command
??? Error using ==> plot
Vectors must be the same lengths.

Error in ==> Dinsdale:engr211:mystuff:plotex.m
On line 28  ==> plot(t,y,'ob',t,yfit,'r-')

>>                         <= This is the new cursor location
```

It did not run but it looks like I should pay attention to line 28, the function (plot) and variables (t, y and yfit, apparently) to see what is wrong. Either how I am not following the syntax of plot or not properly preparing the inputs.

• Read from the bottom up. If you are using "function functions" (doing things like root-finding or integration), the error message will display the "stack trace" of the functions involved and the specific lines (and line numbers) where the execution halted. If you do not recognize a function as one you have written, it is probably a MATLAB built-in function. Read the error message to see where your activity starts.

• Look for the pointer. Sometimes the error message "points" to the place where the interpreter bombed out. This is the usual response to syntax errors. For example, forgetting the closing parenthesis is a common way to see the pointer (the pipe character, |):
```   >> x = sin(t
??? x = sin(t
|
Improper function reference. A "," or ")" is expected.

```
The pointer is telling you where to start looking.

[OPTIONS]

### Negative numbers in functions

The primary (though not only, as of version 5) data type in MATLAB is the complex-valued array. Hence, MATLAB deals "naturally" with complex numbers, a feature that can cause problems when you expect real numbers as answers.

Many engineering problems use three functions/operations that can cause problems when their inputs are negative numbers:

• log, log10: The logarithm of a negative number is a complex number. For example
```   >> log(-3)
ans =
1.0986 + 3.1416i```
• sqrt: The square root of a negative number is usually the first place you see imaginary numbers. As with log, MATLAB handles the operation
```   >> sqrt(-3)
ans =
0 + 1.7321i```
in a "natural" manner that is mathematically correct but practically frustrating, at times.

• ^, .^: Raising a number to a power less than 1 is a generalization of the square-root function. For example,
```   >>(-3)^0.2
ans =
1.0078 + 0.7322i```
[NB. If parentheses are not used to force precedence, the result is different.
```   >>-3^0.2
ans =
-1.2457```
so make sure you let MATLAB know what you mean.]

From a mathematical point of view, handling complex numbers by default is a useful feature. However, most engineering computations and formulas assume that the results from computations are real-valued.

How can you avoid the problems noted above?

• Use abs to ensure a positive input (e.g., log(abs(x)) ).
• Check the sign of the argument before using any of these functions.

[OPTIONS]

### Confusing array and matrix operations

The MATLAB operators ^, *, \ and / all "do the right thing" when applied to array variables - they compute the linear-algebra operation, if it is defined. This is fine if you are expecting to do numerical linear algebra. It is frustrating if you are trying to do element-by-element computations.

Very often you will get an error message when you "forget to add the dot". For example,

```   v1 = [2 5 1]
1/v
??? Error using ==> /
Matrix dimensions must agree.```
Since 1 is a 1-by-1 array and v1 is a 1-by-3 array, the shapes of these arrays are not compatible for right division.

In some cases, the matrix operation is defined and so you won't get an error message that can help you fix a mistake. For example,

```   v1 = [2 5 1]
v2 = [3 4 2]
v1/v2
ans =
0.9655```

In this case, the shapes of the arrays were such that right-division is defined and so a valid (though possibly unexpected) result is returned. This type of error can produce subtle logic errors in a program. Pay careful attention to your computations and be "picky" about what you type in. Read them carefully and use the "dot" when you mean it.

[OPTIONS]

### MATLAB is case-sensitive

There is a difference between Tom, tom and TOM to MATLAB for names of variables and functions. Generally, MATLAB function names should be rendered to in lower-case letters (sin, not SIN) though sometimes MATLAB is flexible enough to recognize the equivalence (as seems to be the case for TITLE and related plot-labelling commands). Be safe - use lower case.

Note: Help information available via the help command often uses upper-case for emphasis. Unless the help information for a function explicitly says that a function name is to include upper-case letters, assume that all names should be in lower-case.

[OPTIONS]

[Back to main page]
Comments? Contact Jim Maneval at maneval@bucknell.edu