MATLAB® throws an error if a variable and function have been given the same name and there is insufficient information available for MATLAB to resolve the conflict. You may see an error message something like the following:
Error: <functionName> was previously used as a variable, conflicting with its use here as the name of a function or command.
where <functionName>
is the name of
the function.
Certain uses of the eval
and load
functions can also result in a similar
conflict between variable and function names. For more information,
see:
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. The root cause is likely to be one of the following:
The name of the function has been misspelled.
The function name and name of the file containing the function are not the same.
The toolbox to which the function belongs is not installed.
The search path to the function has been changed.
The function is part of a toolbox that you do not have a license for.
Follow the steps described in this section to resolve this situation.
One of the most common errors is misspelling the function name.
Especially with longer function names or names containing similar
characters (e.g., letter l
and numeral one), it
is easy to make an error that is not easily detected.
If 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:
accumArray
Undefined function or variable 'accumArray'. Did you mean: >> accumarray
Press Enter to execute the suggested command or Esc to dismiss it.
You establish the name for a function 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 curveplot
,
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 named 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. If you have difficulty locating the file that uses 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
*.m
Under Find files containing text: enter the function name.
Click the Find button
If you are unable to use a built-in function from MATLAB or its toolboxes, make sure that the function is installed.
If you do not know which toolbox supports 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.
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
displayed by ver
, then the toolbox is installed. For help
with installing MathWorks products, see the Installation Guide documentation.
If you do not see the toolbox and you believe that it is installed, then perhaps the MATLAB path has been set incorrectly. Go on to the next section.
This step resets the path to the default.
Because MATLAB stores the toolbox information in a cache file, you will need to first update this cache and then reset the path. To do this,
On the Home tab, in the
Environment section, click
Preferences.
The Preference dialog box appears.
Under the MATLAB > General node, click the Update Toolbox Path Cache button.
On the Home tab, in the
Environment section, click
Set Path....
The Set Path dialog box opens.
Click Default.
A small dialog box opens warning that you will lose your current path settings if you proceed. Click Yes if you decide to proceed.
(If you have added any custom paths to MATLAB, you will need to restore those later)
Run ver
again 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
MATLAB.
Once ver
shows your toolbox, run the following command to
see if you can find the function:
which -all <functionname>
replacing <functionname>
with the name of the
function. You should be presented with the path(s) of the function file. If you
get a message indicating that the function name was not found, you may need to
reinstall that toolbox to make the function active.