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


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)


Reading MATLAB error messages

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.


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:

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?


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]
   ??? 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]
   ans =

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.


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.


[Back to main page]
Comments? Contact Jim Maneval at