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:

- Web resources ( the MATLAB FAQ). Also try a Google Search on "MATLAB FAQ".
- Local sources - did you RTFM?
- "How to Resolve an Error"
- Some numerical errors
and**log, sqrt**of negative numbers`^`- Omitting operators
- Array vs. matrix operations

- Forgetting about case sensitivity
- Reading MATLAB error messages

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

[Back to main page]

- Tell yourself that the computer does not have it in for you
- Articulate clearly what you are trying to do
- Read the error message carefully and completely. Copy it into a file, if need be.
- From the error message, identify what is causing the error and why (if possible)
- 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)
- See if other people have encountered such an error and work with them to resolve it
- If your friends cannot determine the problem or find a solution, ask the instructor (or local expert)
- If the local expert is stumped, consider sending a news message to
and ask for assistance there`comp.soft-sys.matlab` - If the experts cannot solve it, it may be a bug in MATLAB (not very common but it is possible).
- Tell yourself that the computer does not have it in for you

Remember that MATLAB does not understand a statement like

to mean "b(3 + a)

b*(3 + a)

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 calledand tried to run it:**plotex****>> 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 locationIt did not run but it looks like I should pay attention to line 28, the function (

) and variables (**plot**,**t**and**y**, apparently) to see what is wrong. Either how I am not following the syntax of plot or not properly preparing the inputs.**yfit**

**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.

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:

: The logarithm of a negative number is a complex number. For example`log, log10`**>> log(-3) ans = 1.0986 + 3.1416i**: The square root of a negative number is usually the first place you see imaginary numbers. As with`sqrt`, MATLAB handles the operation`log`

in a "natural" manner that is mathematically correct but practically frustrating, at times.**>> sqrt(-3) ans = 0 + 1.7321i**

: Raising a number to a power less than 1 is a generalization of the square-root function. For example,`^, .^`

[NB. If parentheses are not used to force precedence, the result is different.**>>(-3)^0.2 ans = 1.0078 + 0.7322i**

so make sure you let MATLAB know what you mean.]**>>-3^0.2 ans = -1.2457**

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
to ensure a positive input (e.g.,`abs`).**log(abs(x))** - Check the sign of the argument before using any of these functions.
- Ask yourself why your computation would lead to a negative input into these functions.

The MATLAB operators ` ^`,

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

Sincev1 = [2 5 1] 1/v ??? Error using ==> / Matrix dimensions must agree.

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.

There is a difference between ` Tom`,

Note: Help information available via the ` help` command often uses upper-case for emphasis. Unless the help information for a function

[Back to main page]

Comments? Contact Jim Maneval at