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.
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)
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.
>> 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.
>> 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:
>> log(-3) ans = 1.0986 + 3.1416i
>> sqrt(-3) ans = 0 + 1.7321iin a "natural" manner that is mathematically correct but practically frustrating, at times.
>>(-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.2457so 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?
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.
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.