Why do I get the error "Unrecognized function or variable"?
5,100 views (last 30 days)
Show older comments
MathWorks Support Team
on 27 Feb 2012
Edited: MathWorks Support Team
on 1 Jun 2023
I am receiving one of the following error messages. How can I resolve this issue?
Undefined function or variable ‹Name›.
Unrecognized function or variable ‹Name›.
Undefined function or method ‹Name› for input arguments of type ‹ClassName›.
Accepted Answer
MathWorks Support Team
on 22 Aug 2023
Edited: MathWorks Support Team
on 1 Jun 2023
MATLAB does not recognize the specified string as the name of a function on the MATLAB path or as a variable. The above error messages can be caused by:
1) Trying to use a variable that has not been defined before this line of code executes.
>> x=1:10;
>> t=x.^2;
>> plot(x,y)
Undefined function or variable 'y'.
% Possible corrections:
% Change line 3 to "plot(x,t)"
% or Change line 2 from "t=x.^2;" to "y=x.^2;"
2) A typographical error when typing a function or variable name. However, later versions of MATLAB try to resolve these typos with “Did you mean” suggestions. For example,
>> foo = 42;
>> fo0
Undefined function or variable 'fo0'.
Did you mean:
>> foo
3) The wrong case for a function or variable name. Later versions of MATLAB try to resolve these typos with “Did you mean” suggestions.
4) Changing directories so that a function you used is no longer on the MATLAB path.
5) Trying to use a function for which you are not licensed or that belongs to a MathWorks toolbox that isn’t installed. In later versions of MATLAB, this is not an “Undefined function or variable” error, and MATLAB lets you know that you are either not licensed to use the function or the appropriate toolbox is not installed.
6) Trying to use a function that belongs to a third-party toolbox that isn’t installed.
7) Trying to use a function that does not yet exist in your version of MATLAB.
8) Trying to use a function that has been removed from your version of MATLAB. In later versions of MATLAB, this is not an “Undefined function or variable” error, and MATLAB lets you know the new, preferred function to use.
9) Trying to use a variable that gets cleared from the workspace because your script or function contains "clear all" or "clearvars".
10) Calling an object method without an object as the first input.
11) Using a MEX function that is compiled on a platform different from the one in use.
12) The function or script M-file's name ends with a capital ".M" extension instead of a lowercase ".m".
1) Verify that the undefined function or variable is visible (it is on the path or in the current workspace) and that it has been defined before this line of code executes. 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:https://www.mathworks.com/help/matlab/ref/which.html
2) Verify that the function that you are trying to use is available in your version of MATLAB using the built-in documentation (>> doc). If you cannot find it in our documentation, the function may have been added in a later release of MATLAB, or it may be part of a third-party toolbox that is external to MathWorks.
3) If you are trying to use a function that should be available in your version of MATLAB, from a MathWorks toolbox that you have installed and licensed for, there may be a problem with your MATLAB search path. Run the following MATLAB commands to restore it:
>> restoredefaultpath % This will remove any custom paths
>> rehash toolboxcache
>> savepath
See our documentation for more tips:
3 Comments
Walter Roberson
on 25 Oct 2022
There is a related but slightly different message that can easily be mistaken for this message.
In new enough versions of MATLAB, inside a function, if you call a function and you then assign to a variable with the same name as the function, and you then use that name, then MATLAB will know that the function is out of scope (because the variable has that name), but it will also have locked-in the idea that the name is a function rather than a variable. The reference to the name then generates an "Undefined function" message. Notice that the message does not continue on to "or variable".
This behavior can be difficult to understand, as the name might clearly have been assigned to.
For example
function test
a = sum(1:10); %use as function
sum = ones(1,5); %now a variable
sum(2:3)
This can generate undefined function on the reference to sum.
If you had assigned to sum before calling sum as a function, and then you cleared the variable and then tried to use sum as a function, then you would get a related error message.
Inside a function it is no longer permitted to change a name between function and variable.
More Answers (1)
michael
on 14 Nov 2018
(Matlab R14)
Something strange is that when I try to call some function from toolbox (communication) I'm getting that it is not existing.
Even when I'm going to %MATLABROOT%\toolbox\comm\comm where the m file is existing, I still can't run it.
Please suggest what is the issue
1 Comment
Walter Roberson
on 14 Nov 2018
restoredefaultpath
rehash toolboxcache
If that does not work then please show us the complete error message and also the results of using the "which" command with the -all option and the name of the function .
See Also
Categories
Find more on Entering Commands in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!