0% found this document useful (0 votes)
43 views

MATLAB Common Errors Guide

This document discusses common types of errors that may occur when using MATLAB, including: - Syntax errors that occur due to incorrect function calls or matrix operations. - Array indexing errors from using invalid indices. - Assignment errors from mismatches in variable dimensions. - Function errors from incorrect number of inputs/outputs. - Undefined variables or functions not found in the workspace. - Algorithmic errors where the code runs without errors but produces incorrect results. Troubleshooting tips are provided like checking intermediate results.

Uploaded by

Nilana
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

MATLAB Common Errors Guide

This document discusses common types of errors that may occur when using MATLAB, including: - Syntax errors that occur due to incorrect function calls or matrix operations. - Array indexing errors from using invalid indices. - Assignment errors from mismatches in variable dimensions. - Function errors from incorrect number of inputs/outputs. - Undefined variables or functions not found in the workspace. - Algorithmic errors where the code runs without errors but produces incorrect results. Troubleshooting tips are provided like checking intermediate results.

Uploaded by

Nilana
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

(Document created by Tony Vo)

1. Syntax errors
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 of the wrong shape, size, and/or type, or are
otherwise not valid for the function in question. A typo could be considered as a syntax error.

a. Matrix operation errors

Error using ​ *
Incorrect dimensions for matrix multiplication. Check that the number of columns in
the first matrix matches the number of rows in the second matrix. To perform
elementwise multiplication, use '.*'

If matrix A has dimensions (r1,c1) and matrix B has dimensions (r2,c2), A*B will return an
error if c1 ​≠ ​r2. Ensure that your matrices are defined correctly by inspecting the size of
the matrices in the Workspace.

Error using .*
Matrix dimensions must agree.

If ​matrix ​A has dimensions (r1,c1) and ​matrix ​B has dimensions (r2,c2), A.*B will return
an error if r1 ​≠ r2 AND ​c1​ ≠ c2.

If ​vector ​A has dimensions (r1,c1) and ​matrix ​B has dimensions (r2,c2), A.*B will return
an error if r1 ​≠ r2 OR ​c1​ ≠ c2.

b. Array indexing errors

Index exceeds matrix dimensions.

Row or column vectors: if matrix A has dimensions (1,m) or (m,1), A(i) will return an error if
i > m.

2D matrix: if matrix A has dimensions (n,m), A(i) will return an error if i > n*m where n*m is
the number of array elements in A. Furthermore, A(i,j) will return an error if i>n OR j>m.

Subscript indices must either be real positive integers or logicals.

For a matrix A with dimensions (n,m), A(i) will return an error if i is not an integer (e.g
1.25), has a complex component (e.g. 1+2i), or is negative (e.g. -3). Ensure that you are
using only real positive integers or logicals when indexing matrices. A logical is either 0 or
1.
c. Assignment errors

The expression to the left of the equals sign is not a valid target for an assignment.

OR

Error: Incorrect use of '=' operator. To assign a value to a variable, use '='. To
compare values for equality, use '=='

This error occurs when the single equal sign is used incorrectly. This is most common
when using if statements and logicals. Consider the following script:

a = [1 2 3];
for ​i = length(a)
if ​a = 1
disp(​"yes"​)
else
disp(​"no"​)
end
end

The script will encounter this error at line 3, where the intention is to check if the current
value of ‘a’ is equal to ‘1’. The script uses a single equal sign, which means that we are
actually trying to assign the value ‘1’ to the variable ‘a’, we cannot do this in an ‘if’
statement, so the script returns an error. Replace ‘=’ with ‘==’.

For a logical statement, the following script will return the same error at line 3:

a = [1 2 3];
b = [2 3 4];
c = b>1 & a=2;

Here, we are trying to create a logical with the variable name ‘c’ that satisfies two
conditions: the element in ‘b’ is greater than 1 and the corresponding element in ‘a’ is
equal to 2. The expected result is c = [0 1 0]. Again, replace ‘=’ with ‘==’.

In an assignment A(:) = B, the number of elements in A and B must be the same.

OR

Subscripted assignment dimension mismatch.

OR

Unable to perform assignment because the left and right sides have a different
number of elements.
This error occurs when you are trying to assign a matrix or vector to part of another matrix
in which it does not fit. Consider the following script:

a = ones(3);
a(1,1) = [1 2 3];
a(1,:) = [1 2 3];

Line 1 creates a 3x3 matrix of ones.

Line 2 will return an error because we are trying to assign a vector into a single element.

Line 3 will return a result because the dimensions on either side of the equal sign match,
they are both 1x3.

d. Function or variable errors


Not enough input arguments.

OR

Too many input arguments.

OR

Too many output arguments.

These errors occur when you have not called a function correctly. Consider the following
function script:

function ​[x,y] = myfucntion(a,b,c)


%TEST Summary of this function goes here
% a, b, c are all integers.
% Detailed explanation goes here
x = a + b + c;
y = a*b*c;
end

1. [x,y] = myfucntion(1,2)​ ​will return the first error message because it needs
one more input variable.
2. [x,y] = myfucntion(1,2,3,4) ​will return the second error message because it
has one too many input variables.
3. [x,y,z] = myfucntion(1,2,3)​ ​will return the third error message because it has
one too many output variables.
Undefined function or variable 'x'

This error occurs when you use a variable name or call a function in a script when it does
not exist in the Workspace.

For variables, check for the following:

● The variable(s) exist in the Workspace and are compatible for operations or
functions.
● The line(s) containing the variable(s) is not commented with %.

The command ​which <variable name>​ ​will check if the variable exists. If it exists,
MATLAB will return ​<variable name> is a variable​, otherwise it will return ‘​<variable
name>’ not found​.

For functions, check for the following:

● The function exists in the same working directory as the script that is trying to call
the function.
● The function you are trying to call has an m-file with the same name as in the function
declaration line.

The command ​which <function name>​ will check if the function exists in the working
directory. If it exists, MATLAB will return the file path, otherwise it will return ‘​<function
name>’ not found​.

Attempt to execute SCRIPT test as a function

This error occurs when you try to make a call to a function that has not yet been defined,
but there is an m-file in the working directory with the same name. Check that the function
is defined correctly in it’s m-file, i.e. it has a function declaration line: ​function ​[x,y] =
myfunction(a,b,c)​.

2. Algorithmic 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.

There are various techniques for troubleshooting algorithmic errors:

1. Compute the answer by some other means (Wolfram, excel, or hand calculation).
2. Debug your script.
a. Place sequential breakpoints along the code.
b. Manually compute the output you expect at each breakpoint.
c. Compare this with the MATLAB output at each breakpoint by inspecting the
Workspace (assign temporary outputs to variables so they appear in the
workspace.
3. Consult a demonstrator if after trying techniques 1 and 2, the code is still producing
an erroneous result.

When the program is paused at a breakpoint, it is possible to check the state of the program
where the breakpoint is set. By making sure whether the program computes the correct
results at each step, one can eventually locate the error precisely.

You might also like