In this Insight, I’ll go over 5 common MATLAB error messages, what they mean, and how to fix them. Hopefully after reading this post you’ll find yourself being more productive, and maybe even helping your friends with their code.
Most forums online where people post MATLAB questions generate quite a bit of duplicates, and PhysicsForums is no exception. The fact is, there are just certain situations that come up constantly in MATLAB, and if you’re a newer user, don’t consider yourself a programmer, or haven’t used the software in a while, then you’re likely to get tripped up and receive one of those red error messages. It can be especially frustrating when the message doesn’t make sense to you, or your best efforts to fix it come up dry.
1. Error using * Inner matrix dimensions must agree.
By far the most common error message I see posted about by new users is this one. They create a few matrices or vectors, and just go to multiply them with A*B, and this message is returned. Some example code that produces this message is:
A = [1 2 3]; B = [4 5 6]; A*B Error using * Inner matrix dimensions must agree.
The key to this error message is usually that people are not aware of the elementwise operators in MATLAB. The * operator performs matrix multiplication, where an NxM matrix is multiplied by an MxP matrix, resulting in an NxP matrix. Notice how those matrices have the common dimension “M”? That’s where this message comes from; it’s a common inner dimension.
Most often, you simply need to use .* instead of * to perform the elementwise multiplication, where corresponding elements are multiplied and the result is the same size as the inputs.
A.*B ans = 4 10 18
For more information about the different MATLAB operators, see Array vs. Matrix Operations. Note that even though this error message is the most common in this situation (since, well, multiplication is pretty popular) there are similar messages for the misuse of ^ , / , and \ as opposed to .^ , ./ , and .\ .
2. Index exceeds matrix dimensions.
Quite simply, this error arises when you try to reference an element that doesn’t exist. For example, if the matrix has N elements, and you try to index into the N+1 element:
A = magic(5) A = 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9 A(26) Index exceeds matrix dimensions.
To fix this error, double check that the matrix is the size you were expecting it to be, and that the index you’re using is also what you expect. For example, if the index is the result of a calculation or is part of a loop, then you might need to adjust the calculation, or the number of loop iterations. Some useful functions to check sizes and number of elements are numel(), size(), and length().
3. Subscript indices must either be real positive integers or logicals.
The most common reason this message arises is because people come to MATLAB from other programming languages and can’t get used to the fact that MATLAB indexing begins at 1. A(1) is the first element in a vector or matrix (or equivalently A(1,1)), not A(0) like in other programming languages!
A = magic(3) A = 8 1 6 3 5 7 4 9 2 A(0) Subscript indices must either be real positive integers or logicals.
There are a number of theories for why MATLAB uses 1-based indexing, but ultimately the answer is pretty simple. 1-based indexing is the language of Mathematics, as confirmed by Cleve Moler himself in a comment on this April Fools blog post.
We have always had BOTH 0-based indexing and 1-based indexing. In order to distinguish between the two, 0-based indices are followed by “+1″. The 1-based indices are preferred becaused they are the language of mathematics. — Cleve
I won’t expound on this any more, but suffice it to say if you’re interested in this, a quick google search will turn up bountiful results, as it has a long and contentious history!
This error message can also arise if you use a noninteger (or negative) value to index. What is MATLAB supposed to do with A(1.5) or A(-3)? In this context, it’s again likely that you’ll want to check the bounds of any loop statements in your code to make sure they aren’t producing decimal or negative values for indexing.
4. The expression to the left of the equals sign is not a valid target for an assignment.
This error message arises because of misuse of the = and == operators. The = operator does an assignment, and the == operator does a logical test for equality. In the context of an if statement, for example, the if operator is expecting to see a logical condition to determine whether to continue executing code. So the following example code produces this error:
n = 5; if n = 4 n = n.^2; end if n = 4 | Error: The expression to the left of the equals sign is not a valid target for an assignment.
To fix this all you need to do is use == instead:
n = 5; if n == 4 n = n.^2; end
This code outlines the differences between the two operators more clearly:
A = 1:5 A = 1 2 3 4 5 B = 5; A == B ans = 0 0 0 0 1 C = A == B C = 0 0 0 0 1
In short: when you need to compare values, use ==. When you want to assign a value, use =.
5. Subscripted assignment dimension mismatch.
This error message arises because of an attempt to assign a vector or matrix into a compartment that it does not fit in. The dimension of the subscripted elements does not match the dimension of the assignment. For example, you cannot assign the first element in a matrix to be a vector, because there is only room for 1 element:
A = magic(3) A = 8 1 6 3 5 7 4 9 2 A(1) = [4 5 6] Subscripted assignment dimension mismatch.
This error can be much more subtle when you’re working with large matrices or loops, and it can occur because of a mismatch on either side of the equals sign. Sometimes the size of a vector or matrix can grow in an unexpected way in a loop, and you’ll receive this message and wonder what went wrong. The best way to debug this error is to double check that all of your assignments are the sizes you expect them to be, and that your matrices are growing (or not) as you expect them to.
If you don’t have any loops, just break the statement apart and check the size of each side. You won’t get this error if the sizes match exactly:
size(A(1:3)) ans = 1 3 size([4 5 6]) ans = 1 3 A(1:3) = [4 5 6] A = 4 1 6 5 5 7 6 9 2
Obviously I could go on with another 25 error messages, but I think these are the most common ones I see people posting about. If you’re interested in reading about some others, check out this link:
Post about your favorite or least favorite MATLAB error messages in the comments, and let me know what you think! Have an idea for a simulation or a question about quantum mechanics? Some other idea for a future post? Let me know!
Disclaimer: All views and/or opinions expressed in this post are my own, and should not be interpreted in any other way.