Quantcast

Documentation Center

  • Trial Software
  • Product Updates

Common Errors When Calling Functions

Conflicting Function and Variable Names

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:

Undefined Functions or Variables

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.

Verify the Spelling of the Function Name

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.

Make Sure the Function Name Matches the File Name

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:

  1. On the Home tab, in the File section, click Find Files.

  2. Under Find files named: enter *.m

  3. Under Find files containing text: enter the function name.

  4. Click the Find button

Make Sure the Toolbox Is Installed

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 http://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.

Verify the Path Used to Access the Function

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,

  1. On the Home tab, in the Environment section, click Preferences.

    The Preference dialog box appears.

  2. Under the MATLAB > General node, click the Update Toolbox Path Cache button.

  3. On the Home tab, in the Environment section, click Set Path....

    The Set Path dialog box opens.

  4. 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. See the Related Solution 1-1CBD3, "How do I install additional toolboxes into my existing MATLAB" for more information about installing a toolbox.

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.

Verify that Your License Covers The Toolbox

If you receive the error message "Has no license available", there is a licensing related issue preventing you from using the function. To find the error that is occurring, you can use the following command:

license checkout <toolbox_license_key_name>

replacing <toolbox_license_key_name> with the proper key name for the toolbox that contains your function. To find the license key name, look at the INCREMENT lines in your license file. For information on how to find your license file see the related solution: 1-63ZIR6, "Where are the license files for MATLAB located?"

The license key names of all the toolboxes are located after each INCREMENT tag in the license.dat file. For example:

INCREMENT MATLAB MLM 17 00-jan-0000 0 k
B454554BADECED4258 \HOSTID=123456 SN=123456

If your license.dat file has no INCREMENT lines, refer to your license administrator for them. For example, to test the licensing for Symbolic Math Toolbox™, you would run the following command:

license checkout Symbolic_Toolbox

A correct testing gives the result "ANS=1". An incorrect testing results in an error from the license manager. You can either troubleshoot the error by looking up the license manager error here:

http://www.mathworks.com/support/install.html

or you can contact the Installation Support Team with the error here:

http://www.mathworks.com/support/contact_us/index.html

When contacting support, provide your license number, your MATLAB version, the function you are using, and the license manager error (if applicable).

Was this topic helpful?