Resolve Error: Undefined Function or Variable
You may encounter the following error message, or something similar, while working with functions or variables in MATLAB®:
Undefined function or variable 'x'.
These errors usually indicate that MATLAB cannot find a particular variable or MATLAB program file in the current directory or on the search path.
Verify Spelling of Function or Variable Name
One of the most common causes is misspelling the function or variable name.
Especially with longer names or names containing similar characters (such as the
l and numeral one), it is easy to make mistakes and
hard to detect them.
Often, when you misspell a MATLAB function, a suggested function name appears in the Command Window. For example, this command fails because it includes an uppercase letter in the function name:
Undefined function or variable 'accumArray'. Did you mean: >> accumarray
When this happens, press Enter to execute the suggested command or Esc to dismiss it.
Verify Inputs Correspond to the Function Syntax
Object methods are typically called using function syntax: for instance
method(object,inputs). Alternatively, they can be called
using dot notation: for instance
common error is to mix these syntaxes. For instance, you might call the method
using function syntax, but to provide inputs following dot notation syntax and
leave out the object as an input: for instance,
method(inputs). To avoid this, when calling an object
method, make sure you specify the object first, either through the first input
of function syntax or through the first identifier of dot notation.
Make Sure Function Name Matches File Name
When you write a function, you establish its name when you write its function
definition line. This name should always match the name of the file you save it
to. For example, if you create a function named
function curveplot(xVal, yVal) - program code -
then you should name the file containing that function
curveplot.m. If you create a
pcode file for the function,
then name that file
curveplot.p. In the case of conflicting
function and file names, the file name overrides the name given to the function.
In this example, if you save the
curveplot function to a file
curveplotfunction.m, then attempts to invoke the
function using the function name will fail:
curveplot Undefined function or variable 'curveplot'.
If you encounter this problem, change either the function name or file name so that they are the same.
To Locate the file that defines this function, use the MATLAB Find Files utility as follows:
On the Home tab, in the File section, click Find Files.
Under Find files named, enter
Under Find files containing text, enter the function name.
Click the Find button
Make Sure Necessary Toolbox Is Installed and Correct Version
If you are unable to use a built-in function from MATLAB or its toolboxes, make sure that the function is installed and is the correct version.
If you do not know which toolbox contains the function you need, search for
the function documentation at
https://www.mathworks.com/help. The toolbox name
appears at the top of the function reference page. Alternatively, for steps to
identify toolboxes that a function depends on, see Identify Program Dependencies.
Once you know which toolbox the function belongs to, use the
ver function to see which
toolboxes are installed on the system from which you run MATLAB. The
ver function displays a list of all
currently installed MathWorks® products. If you can locate the toolbox you need in the output
ver, then the toolbox is installed. If you
cannot, you need to install it in order to use it. For help with installing
MathWorks products, see Install License Manager on License Server.
Verify Path Used to Access Function Toolbox
If you have a custom file path, this step will delete it.
The MATLAB search path is a subset of all the folders in the file system. MATLAB uses the search path to locate files used with MathWorks products efficiently. For more information, see What Is the MATLAB Search Path?.
If the function you are attempting to use is part of a toolbox, then verify
that the toolbox is available using
Because MATLAB stores the toolbox information in a cache file, you need to first update this cache and then reset the path.
On the Home tab, in the Environment section, click Preferences.
The Preference dialog box appears.
On the MATLAB > General page, select Update Toolbox Path Cache.
On the Home tab, in the Environment section, select Set Path.
The Set Path dialog box opens.
A small dialog box opens warning that you will lose your current path settings if you proceed. Select Yes if you decide to proceed.
ver to see if the toolbox is installed. If not, you
may need to reinstall this toolbox to use this function. For more information
about installing a toolbox, see How do I install additional toolboxes into an existing installation of
ver shows your toolbox, run the following command to
see if you can find the function:
which -all <functionname>
with the name of the function. If MATLAB finds your function file, it presents you with the path to it. You
can add that file to the path using the
addpath function. If
it does not, make sure the necessary toolbox is installed, and that it is the
Confirm The License Is Active
If you are unable to use a built-in function from a MATLAB toolbox and have confirmed that the toolbox is installed, make
sure that you have an active license for that toolbox. Use
license to display currently active licenses.
For additional support for managing licenses, see Manage Your Licenses.