Product Support
1207 - Commonly Encountered Error and Warning Messages
This Tech Note is intended for anyone writing code in MATLAB who would like to know more about commonly encountered error/warning messages and techniques to eliminate them.
The Tech Note cannot, of course, describe all the possible error and warning messages that functions in MATLAB may return, due to the infinite number of code sequences you might program. This Tech Note focuses on the commonly encountered error and warning messages.
Introduction
Bug Types
Errors
- Subscripted Assignment Dimension Mismatch
- Unbalanced or Unexpected Parenthesis or Bracket
- CAT Arguments Dimensions Are Not Consistent
- Attempt to Execute SCRIPT Filename as a Function
- Error When Evaluating Uicontrol Callback
- Index Exceeds Matrix Dimensions
- Matrix Dimensions Must Agree
- Invalid Object Property: Property
- Invalid Handle
- Matrix Must Be Square
- Product of Dimensions Is Greater Than Maximum Integer
- Too Many Input/Output Arguments
- Undefined Function or Variable
- One or More Output Arguments Not Assigned During Call to Function
- Subscript Indices Must Be Real Positive Integers or Logicals
Warnings
- Suppressing Warning Messages
- Imaginary Parts of Complex X and/or Y Arguments Ignored
- Integer Operands Are Required for Colon Operator When Used as Index
- Matrix is Close to Singular or Badly Scaled
- OpenGL Not Available. Using ZBuffer.
Additional Information
Section 1: Conventions
Within this Technical Note, example MATLAB code is provided. This code appears in the following type style:
cos(pi)
If you would like to evaluate the code within MATLAB for yourself, you can either copy and paste the code directly into MATLAB, or, if you are using the MATLAB Web Browser to view this Tech Note, you can simply highlight the code you are investigating, right-click, and select Evaluate selection.
For instructions on using the examples provided with this Tech Note, see Section 3.
Words in all caps and in monospace font are the names of MATLAB functions. Often, these words will also be links to the corresponding MATLAB function documentation. For example, this sentence refers to the MATLAB function SIN, which computes the sine of an angle.
Section 2: For What Versions of MATLAB Are These Techniques Valid?
The majority of these techniques should work for MATLAB 7.9 (R2009b) and higher. However most of them are expected to work for older versions of MATLAB too.
Section 3: Download the Examples
To obtain the complete set of examples used in the following sections, download the .ZIP file.
The corresponding example for each error or warning is listed at the end of each section. You should open the example listed at the end of each section in the Editor/Debugger before reading the description, so you can compare the code in the example file that produces the error with the explanation in the section.
Note: In the Solutions listed below, the sentence "Stop MATLAB on the line where the error [warning] occurs." means- Set a breakpoint using DBSTOP or the Breakpoints menu in the Editor/Debugger if MATLAB detects an error (or set a breakpoint on the line where the error occurs, if you know the line number).
- Execute your code.
- Examine the line on which the debugger stops.
Section 4: What Are the Three Main Types of Bugs?
The three major types of bugs are typographic errors (typos), syntax errors, and algorithmic errors. This Tech Note will not address algorithmic errors in detail, due to the individuality of each algorithm. The definitions of each type of bug are as follows:
- A typo, or typographic error, is a simple typing error. If it occurs while you are typing a function name, it can
be easy to find -- but if you mistype a variable name, it can lead to unexpected results that can be extremely difficult
to track down and eliminate. For example, typing
cod(pi)
when you meant to type
cos(pi)
will return an error message that is easy to understand:
??? Undefined function or variable 'cod'.However, if you type
x=cos(y);
when you meant to type
x=cos(t);
and t is a different size than y, you will likely receive an error message about the size of the array when you try to manipulate it later in your code, long after the assignment statement has been executed. Tracing the error back to this line may be difficult if you have a significant amount of code between the assignment and the line where the error occurs. - A syntax error occurs when the calling syntax you use for a function is incorrect, or when you provide the function with inputs that are the wrong shape, size, and/or type or are otherwise not valid for the function in question. For example, the typo in the previous bullet (typing y when you intended to type t) can lead to a syntax error (when you try to use the x variable in a later line of your code.) The error messages for these types of errors usually indicate the problem, but deciphering what they mean can sometimes be difficult, and tracking down where the problem originated can be even harder. This type of error message is what the techniques in this Tech Note are designed to catch and investigate, but the techniques also work to some extent for the other two main types of bugs. The Finding Errors, Debugging, and Correcting MATLAB Files chapter of the Editing and Debugging MATLAB Files section of the documentation, which discusses using the Editor/Debugger and debugging MATLAB files, groups typographical errors and syntax errors under the category of syntax errors.
- An algorithmic error occurs when the program executes perfectly, but the result you receive is not what you expect. For instance, if you wrote a program to add two numbers, passed it 2 and 3, and received 6 as a result (with no error or warning messages) that would be an algorithmic error. The main technique involved in debugging these types of bugs is to compute the answer you expect to the problem by some means other than MATLAB (or by using an example worked in a text on the subject) and stepping through the code using the Step button (which is described in the following section of the Debugging Process and Features documentation) or the DBSTEP function verifying that the algorithm computes the correct result at each stage of the calculation. This procedure can be streamlined by setting breakpoints after sections of the code and stopping the first time the answer at a breakpoint disagrees with the answer computed using the other method; then you can step through the code between that breakpoint and the previous one to locate the error precisely.
Section 5: Subscripted Assignment Dimension Mismatch
??? Subscripted assignment dimension mismatch.
??? In an assignment A(I) = B, the number of elements in B and
I must be the same.
You are attempting to assign more elements to a section of a matrix or vector than that section can hold. These two error messages occur when you
- Use a colon operator to specify an entire row or column in the destination matrix
- Use linear indexing with an incorrectly sized index array
You are attempting to replace a section of one matrix with another and the size of the index vector or vectors used to specify the section of the destination matrix to be replaced does not match that of the source matrix. Alternatively, you are attempting to assign values to individual elements using row and column indices, which allocates a submatrix section of the original matrix. Solution:
Stop MATLAB on the line where the error occurs. Verify that the size of the index array or arrays agrees with the size of the array you are attempting to assign to the destination matrix. You should verify that the computations that created the index array and the vector or matrix to be assigned are correct. An unexpected size may indicate that the computations that created the matrix were incorrect.
Finally, you may want to use the REPMAT function to expand a smaller matrix to fill a large block, make use of scalar expansion to fill the region to which you are trying to assign, or use the SUB2IND or IND2SUB function to generate an appropriate index vector or set of index vectors.
Example demonstrating this error:AssignmentSizeMismatch.m
Section 6: Unbalanced or Unexpected Parenthesis or Bracket.
Error: Unbalanced or unexpected parenthesis or bracket. Explanation:The number of left parenthesis is not equal to the number of right parenthesis in your expression. Common causes:
You added a left parenthesis or removed a right parenthesis from the line of code. Solution:
Count the number of left parentheses and right parentheses on the line of code. Verify that the quantity of the two types of parentheses are equal. Add in an appropriate number of right parentheses or remove extraneous left parentheses. Example demonstrating this error:
ParenthesisExpected.m
Section 7: CAT Arguments Dimensions Are Not Consistent.
CAT arguments dimensions are not consistent. Explanation:You are attempting to concatenate two arrays using [] but the appropriate dimensions along which to concatenate are not the same size. Common causes:
You have forgotten a semicolon (;) or a comma (,) when attempting to concatenate two character arrays, or you are using the wrong separator. This error commonly occurs when attempting to concatenate character arrays to try to create a multi-line string. Solution:
Examine the line listed in the error message and insert the appropriate matrix element separator. Also verify that the dimensions along which you are trying to concatenate the matrices agree. The help files for the HORZCAT and VERTCAT functions describe the relationship that needs to exist for this concatenation to work correctly.
horzcat(A,B)is the equivalent of [A B] while
vertcat(A,B)is the equivalent of [A;B]. If you are attempting to concatenate strings, you should use the STRCAT and STRVCAT functions, which handle adjusting the appropriate dimensions automatically if it is necessary. Example demonstrating this error:
BracketedExpressionDimensions.m
Section 8: Attempt to Execute SCRIPT Filename as a Function
Attempt to execute SCRIPTYou are attempting to call a script file as though it were a function file by passing arguments to it. Common causes:
You have created a script MATLAB file with the same name as a function on the MATLAB path but higher on the path than that function, or are attempting to index into an array that does not exist when a script file with that name exists. Solution:
Execute
which -all filenamefor the file specified in the error message. Verify that the file you expect to execute is being used instead of any other file with the same name. If it is not, rename or delete the files higher on the MATLAB path than the file you expect to use, or reorder the directories on your MATLAB path to place the version of the file you want to use higher than any other version. For more information on the MATLAB path and file precedence, see the information on MATLAB Path. Example demonstrating this error:
AttemptExecuteScriptAsFunction.m and TestScriptAsFunction.m
Section 9: Error When Evaluating Uicontrol Callback
Error when evaluating uicontrol Callback. Explanation:A callback function for a uicontrol in your figure or GUI has thrown an error. Common causes:
There are many possible causes for this error. The section of the error message that follows the line above will explain the cause of the error; the line above simply identifies that the error is located in a uicontrol's callback function. Solution:
Debug the callback function as normal based on the second portion of the error message returned from MATLAB. Search for that portion of the error message in this Tech Note. Example demonstrating this error:
N/A
Section 10: Index Exceeds Matrix Dimensions
Index exceeds matrix dimensions. Explanation:You are attempting to reference a nonexistent element of an array. Common causes:
Changing the size of an array unexpectedly, or encountering an empty array when you expected a nonempty array. Solution:
Stop MATLAB on the line where the error occurs. Verify the size of the array into which you are referencing is large enough that the index you are using falls before the last element of the array - one way to do this is to check the Workspace Browser. Verify that the variable was correctly created at each of the previous lines, starting with the most recent, where it appeared on the left side of an assignment statement. Example demonstrating this error:
IndexExceedsMatrixDimensions.m
Section 11: Matrix Dimensions Must Agree
Inner matrix dimensions must agree. Explanation:You are attempting to perform a matrix operation, which requires certain matrix dimensions to agree, on matrices that do not satisfy this requirement. Common causes:
You are attempting to multiply or divide two matrices where the number of columns in the first is not equal to the number of rows in the second (for *) or the number of rows do not match (for \). This often indicates that you are performing matrix operations when you instead intended to perform array operations. Solution:
Stop MATLAB on the line where the error occurs. Verify that you are not performing an extra transpose operation or omitting one where necessary. Also verify the sizes of the matrices, which you are multiplying or dividing, agree in the corresponding dimensions. You can do this using the Workspace browser or the SIZE function. If you intended to perform array operations instead of matrix operations, replace the *, /, \, or ^ matrix operators with the .*, ./, .\, or .^ array operators instead. If you pass your formula as a string to the VECTORIZE function, VECTORIZE will return the formula with the matrix operators (*, /, and ^) replaced by the array operators (.*, ./, .^). Example demonstrating this error:
MatrixDimensionsMustAgree.m
Section 12: Invalid Object Property
There is no PropertyName property in the ClassName class. Explanation:You are attempting to set a property of a graphics object, but that property is not a property of that object. Alternatively, that name does not correspond to a property of that class. Common causes:
You are attempting to set a property of a graphics object, such as FontName or Callback for a figure, but figure objects have neither a FontName nor a Callback property. If you know that the object has that property, there may be a typographical error in the name of the property. Solution:
Verify that the name in the SET or GET statement on the line listed in the error message is spelled correctly and that the property you are attempting to access with the SET or GET function is a valid property for that type of Handle Graphics object using the Handle Graphics Object Properties browser. Example demonstrating this error:
InvalidObjectProperty.m
Section 13: Invalid Handle
Invalid handle. Explanation:You are using Handle Graphics commands to try to manipulate a handle to an object that no longer exists. Common causes:
You have removed the Handle Graphics object from the figure or GUI before attempting to manipulate it using SET or GET . Another common cause is creating objects inside a for loop without turning HOLD on; this frequently occurs using the PLOT function. Solution:
Stop MATLAB on the line where the error occurs. Verify that the reference you are using is to an existing Handle Graphics object by backtracking in the code to the statement that created the Handle Graphics object. Also verify that the line, which was intended to create the object, did so by setting a breakpoint on it and executing the function again. The FINDALL, FINDOBJ, ALLCHILD, and ISHANDLE functions allow you to find the handles of objects and verify that the objects exist. Example demonstrating this error:
InvalidHandle.m
Section 14: Matrix Must Be Square
Matrix must be square. Explanation:You are attempting to perform an operation that is only defined for square matrices on a matrix with an unequal number of rows and columns. Common causes:
You are attempting to use an element-wise operator on a matrix that is nonsquare, but you are using the linear algebra operator instead of the element-wise operator. For example, you use the ^ operator (taking the power of a matrix) rather than .^ (which takes the power of each element of the matrix). Solution:
Examine the line listed in the error message and verify the matrix whose power you want to take is square, or that you are using the appropriate operator. Example demonstrating this error:
MatrixMustBeSquare.m
Section 15: Maximum variable size allowed by the program is exceeded.
??? Maximum variable size allowed by the program is exceeded. Explanation:You are attempting to create a matrix with more elements than the maximum number of elements allowed in MATLAB. Common causes:
MATLAB will attempt to create a matrix with a large number of elements, as long as that number of elements is less than the maximum number of elements allowed in a matrix. You can determine this maximum using the COMPUTER function. Solution:
If the matrix you are attempting to create has relatively few nonzero elements, you may be able to create it as a sparse matrix. You can use the SPARSE function and the other sparse matrix manipulation functions to create and manage this matrix. Type
help sparfunfor a list of the sparse matrix manipulation functions. Note, however, that the limitation on the maximum number of elements still exists; now it only applies to the nonzero elements of the sparse matrix. If your matrix is not sparse, however, you will need to break it into sections with a number of elements less than the maximum returned by the COMPUTER function. Example demonstrating this error:
ProductDimensionsTooLarge.m
Section 16: Too Many Input/Output Arguments
Too many output arguments.Too many input arguments.
Not enough input arguments. Explanation:
A function you are trying to call expects fewer input/output arguments, or more input arguments, than you have provided it Common causes:
You have passed a function more input arguments than it expected to receive, perhaps by passing a list of inputs rather than a vector of inputs, or have tried to obtain two outputs from a function that only returns one. Solution:
Stop MATLAB on the line where the error occurs. Verify that you have specified the correct number of input and/or output arguments, using WHICH to determine which version of the function you are using if necessary. If this occurs immediately when you try to run your main function, verify that it can accept and/or return sufficient arguments. Another way to debug this problem is to use the NARGIN and NARGOUT functions. When you pass NARGIN or NARGOUT the name of the function, it returns the number of input or output arguments a function can accept, respectively. When used inside a function, it returns how many inputs or outputs are actually specified in the call to the function. Finally, the DBTYPE function, when used with the name of a file and the number 1 as inputs, will display the first line of the MATLAB file (which shows the input and output arguments the function expects). Example demonstrating this error:
TooManyArguments.m
Section 17: Undefined Function or Variable
??? Undefined function or variable
??? Undefined function or method FunctionName for input arguments of type ClassName.
Explanation:MATLAB does not recognize the specified string as the name of a function on the MATLAB path or a variable visible to that function. Common causes:
- You have made a typographical error when typing a command or variable name (such as typing a 1 [one] when you intended to type an l [low-case L]).
- You have changed directories so that a function you used is no longer on MATLAB's search path.
- You have used the wrong case for a function or variable name.
- You are trying to use a function for which you are not licensed.
Stop MATLAB on the line where the error occurs. Verify that the undefined function or variable is visible to the function at that point (it is on the path or in the current workspace) and that it has been defined before this line of code executes. Check if a datatip appears when you move the mouse pointer over the variable name on that line. (Point at the following arrow for an example:
If you are certain the variable or function exists, verify the case of the function or variable name. If the undefined identifier is a function, the WHICH function can help you verify that it is visible to the function where the error occurs.
Example demonstrating this error:UndefinedFunctionOrVariable.m
Section 18: One or More Output Arguments Not Assigned During Call to Function
??? Output argument variable (and maybe others) not assigned during call to function. Explanation:One of the functions you have called is defined to return an output argument but that output argument does not exist in the function when it tries to return. Common causes:
You have misspelled the name of one of your output arguments inside your function, or you have forgotten to assign a value to one of the output arguments of your function. Alternatively, the function was originally written with one or more output arguments, but the section of the function that computed the output argument was removed or modified in such a way that the output argument is now extraneous. Solution:
Stop MATLAB on the last line of the function listed in the warning or error message. Verify that each of the output arguments listed in the function declaration line at the beginning of the function exist after that last line is executed (using the DBSTEP function or the Step button in the Editor). If the arguments do not exist, examine the function to determine where you intended the arguments to be declared. Verify that those lines of code are being executed and have no typographical errors. Example demonstrating this error:
OutputNotAssigned.m
Section 19: Subscript Indices Must Be Real Positive Integers or Logicals
??? Subscript indices must either be real positive integers or logicals. Explanation:You are attempting to index into a matrix or vector using a set of indices that include a number that is not a positive integer or a logical value. Common causes:
The most common cause of this warning or error is that you are indexing into one matrix with another matrix which contains 0's and 1's (this is called logical indexing) but the index matrix is not a logical array. Alternatively, if you compute indices using floating-point arithmetic, the indices may not be exactly an integer. If the indices are not integer, they must be rounded to the nearest integer to be used to index into the matrix. Solution:
Stop MATLAB on the line where the warning or error occurs. Verify that the indices you are using to index into the matrix are either positive integers or logical 0 values. To determine if the index vector X contains only integers, you can use the syntax
X==round(X)or examine the values in the vector X-round(X) using the HEX format. See the documentation on the FORMAT function for more information on the HEX format. Example demonstrating this error:
InvalidIndices.m
Section 20: Suppressing Warning Messages
You can selectively enable or disable specific warning messages, as long as they have been given a message identifier, with the WARNING function using the state and 'message_id' input arguments. You can suppress all warning messages using the 'off' state option, with the argument 'all' in place of a message identifier. You can suppress only the last warning message in a similar manner, replacing 'all' with 'last'. Once you have suppressed warnings, you can enable the warning messages again using the 'on' state option.
Section 21: Imaginary Parts of Complex X and/or Y Arguments Ignored
Warning: Imaginary parts of complex X and/or Y arguments ignored.
Warning: Imaginary parts of complex X, Y, and/or Z arguments ignored.
Explanation:You are attempting to plot using two complex inputs to a plotting function, like PLOT or PLOT3. In this case, MATLAB will plot using the real part of the first input as the independent variable X and the real part of the second input as the dependent variable Y. Common causes:
You have performed a square root or FFT operation on the vectors you are attempting to plot, and those operations resulted in a complex vector. Alternatively, you used the variables i and/or j in the computation of your input vectors but those variables did not exist when you performed your computation. In this case, MATLAB will treat i and/or j as the imaginary unit. Solution:
Stop MATLAB on the line where the warning occurs. Verify that the two vectors or matrices you pass to the PLOT function, or the three you pass to PLOT3, are not complex. If you want to plot the real part of a vector versus the complex part, pass the vector as a single complex vector to the PLOT function. If you want to plot the magnitude of the elements, use the ABS function on the vector before passing it to the PLOT function. Example demonstrating this warning:
ImaginaryPartIgnored.m
Section 22: Integer Operands Are Required for Colon Operator When Used as Index
Warning: Integer operands are required for colon operator when used as index. Explanation:You have used a noninteger value as one of the parameters (starting value, increment, or stopping value) for the colon (:) operator when using it to create a vector of indices to reference into a function. Common causes:
You performed computations to obtain the starting or stopping values for the indexing but the result of those computations was not exactly an integer. Solution:
Modify the index computations using the FIX, FLOOR, CEIL, or ROUND functions to ensure that the indices are integers. You can test if a variable contains an integer by comparing the variable to the output of the ROUND function operating on that variable when MATLAB is in debug mode on the line containing the variable. Example demonstrating this error:
IntegerOperandsRequired.m
Section 23: Matrix is Close to Singular or Badly Scaled
Warning: Matrix is close to singular or badly scaled. Explanation:A matrix used in a computation is singular or is very close to being singular (ill conditioned). Trying to solve a system of linear equations whose coefficient matrix is singular can lead to incorrect answers. Common causes:
A program you called is using a matrix that is badly conditioned as one of the arguments to the backslash (\) or forward slash (/) operators, which are commonly used to solve systems of linear equations. Solution:
Stop MATLAB on the line where the warning occurs. Examine the line for instances of the two slash operators. If you find one, determine the condition number of the coefficient matrix (the matrix on the left side of the backslash (\) or on the right side of the forward slash (/) using the COND function. Large results for the condition number indicate the matrix is extremely illconditioned. You should verify (using the matrix multiplication operator (*)) that the result of solving the system is numerically reliable. For more information about condition numbers of matrices, see Chapter 2: Linear Equations of the Cleve Moler's Numerical Computing with MATLAB book, which discusses solving systems of linear equations. Example demonstrating this error:
CloseToSingular.m
Section 24: OpenGL Not Available. Using ZBuffer.
Warning: OpenGL not available. Using ZBuffer.
Explanation:You are attempting to display a plot that MATLAB tries to render using OpenGL, but OpenGL is not available or is not functioning correctly on your computer. Therefore, MATLAB will use the ZBuffer renderer instead. Common causes:
The common causes for OpenGL being unavailable or functioning incorrectly on your machine are listed in Tech Note 1201: The Graphics Rendering and Troubleshooting Guide. Solution:
See Tech Note 1201: The Graphics Rendering and Troubleshooting Guide for information on how to correct this problem. Example demonstrating this error:
N/A
Section 25: Conclusion and Further Information
You should now be able to diagnose and correct some of the most frequently encountered errors experienced by authors of MATLAB programs. The techniques you have learned will serve you well in tracking down and eliminating less frequent errors that you may experience in the future when you write your own MATLAB files.
If you are receiving an error message that is not listed in this Tech Note and for which you do not know how to debug and correct, there are several ways you can obtain more information to help you deal with that error. One location from which you can obtain more information is the MathWorks Technical Support Web site, specifically the Solutions and Technical Notes. It is very likely that another MATLAB user has requested information about a similar error you may be encountering, and this information has been recorded in an online solution or a Tech Note like this one. You can search for information in the entire database or look for a solution specific to a toolbox you're using on the product page for that toolbox.
If the error message is not listed in the solution database, there are three other means by which you can request help.
- You can search the archives of the MATLAB newsgroup comp.soft-sys.matlab via the MATLAB Central, or within the Google newsgroup archive. Often, users post questions about error messages they are receiving and receive information, suggestions, corrections, and explanations from other users.
- If you cannot find an answer on the MathWorks Technical Support Web site or within the comp.soft-sys.matlab archives, you can post a question to the newsgroup yourself. You can use either one of the archive links in the previous paragraph to post your question, or you can post via your Internet provider's news server. Generally, a message describing what you have already tried to do to correct the problem along with the error message will produce a response from one of the experienced users who read the newsgroup.
- If neither the solution database nor the newsgroup archives provides a solution for the problem you are experiencing
and you do not receive a response from comp.soft-sys.matlab, you can contact the MathWorks Technical Support
department, which you can reach by phone, e-mail, or by filling out this Web form.
If you frequently experience an error related to MATLAB file programming not listed in this Tech Note and want to see it listed in the future, fill out the form shown at the bottom of this Tech Note. Include a copy of the exact error message, as well as a small (no more than 10 line) section of code that reproduces the error, the techniques you have used to determine the cause of the error, and your contact information. If you have not determined the cause of the error, check the Technical Support Knowledge Base and/or contact our Technical Support department before submitting the form below.
Once you have written and debugged your MATLAB program, you may be interested in optimizing its performance. Resources you can consult to assist you in optimizing your program are the Tech Note 1109: The Code Vectorization Guide, and Techniques for Improving Performance and Strategies for Efficient Use of Memory, of the MATLAB User Guide.
Store