## Why do I get the error "Undefined function or variable"?

Asked by MathWorks Support Team

### MathWorks Support Team (view profile)

on 27 Feb 2012
Latest activity Commented on by Arihant Sureka

### Arihant Sureka (view profile)

on 3 Jan 2019 at 21:50
Accepted Answer by MathWorks Support Team

### MathWorks Support Team (view profile)

I am receiving the following error message, how can I resolve this issue?
??? Undefined function or variable ‹FunctionName›
??? Undefined function or method ‹FunctionName› for input arguments of type ‹ClassName›.

### MathWorks Support Team (view profile)

Answer by MathWorks Support Team

### MathWorks Support Team (view profile)

on 17 May 2018
Edited by MathWorks Support Team

### MathWorks Support Team (view profile)

on 17 May 2018

MATLAB does not recognize the specified string as the name of a function on the MATLAB path or as a variable. "Undefined function or variable" can be caused by:
1) A typographical error when typing a function or variable name (such as typing a 1 [one] when you intended to type an l [low-case L]). 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
2) The wrong case for a function or variable name. Later versions of MATLAB try to resolve these typos with “Did you mean” suggestions.
3) Changing directories so that a function you used is no longer on the MATLAB path.
4) Trying to use a function for which you are not licensed or that belongs to a 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.
5) Calling an object method without an object as the first input.
6) Using a MEX function that is compiled on a platform different from the one in use.
Solution:
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 you are certain the variable or function exists, verify the case of the function or variable name. 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.
If you are having problems with your MATLAB search path, run the following MATLAB commands and then restart MATLAB.
restoredefaultpath % This will remove any custom paths
rehash toolboxcache
savepath
Example demonstrating this error:
UndefinedFunctionOrVariable.m (attached)

Dhruba Raj Dhakal

### Dhruba Raj Dhakal (view profile)

on 30 Jun 2017
Why this error occurs when I use antenna toolbox???
Dhruba Raj Dhakal

### Dhruba Raj Dhakal (view profile)

on 2 Jul 2017
this code is copied from https://www.mathworks.com/help/antenna/examples/antenna-array-analysis.html
Walter Roberson

### Walter Roberson (view profile)

on 2 Jul 2017
The routine is new in the examples as of R2015a, but the location of the file did move as of R2017a, moving from toolbox/antenna/atexamples/dipole_tuner.m to examples/antenna/dipole_tuner.m

### Iman Tahamtan (view profile)

Answer by Iman Tahamtan

### Iman Tahamtan (view profile)

on 25 Mar 2018

I am facing this error when running y_lambda=lambda: Undefined function or variable 'lambda'.

Walter Roberson

### Walter Roberson (view profile)

on 26 Mar 2018
Where do you expect that your program should be getting a value of lambda from?

### ishwarya ramesh (view profile)

Answer by ishwarya ramesh

### ishwarya ramesh (view profile)

on 27 Mar 2018

Undefined function or variable 'drivingScenario'. why do i get this error i just need a clear explanation

Florian Morsch

### Florian Morsch (view profile)

on 27 Mar 2018
Why do you get this error? Because the variable or function 'drivingScenario' is undefined and you try to use it. With the information you gave (none at all) noone will be able to help you. Where do you use the variable/function? Which toolbox do you intend to use? If its only a part of code then you maybe have do declare the missing functions/variables yourself.
Walter Roberson

### Walter Roberson (view profile)

on 27 Mar 2018
That function is part of the Automated Driving System Toolbox, which was new as of R2017b. You probably do not have that installed or licensed. If I recall correctly it is not available for Student licences.

### Liliana Malik (view profile)

Answer by Liliana Malik

### Liliana Malik (view profile)

on 6 Apr 2018

why do i get Undefined function or variable 'pixelLabelDatastore' and Undefined function or variable 'batchNormalizationLayer'

Walter Roberson

### Walter Roberson (view profile)

on 17 May 2018
No, there is no replacement for pixelLabelDatastore in earlier versions.
Akib Rahman

### Akib Rahman (view profile)

on 22 Jun 2018
@Walter, batchNormalizationLayer also need Matlab R2017b?
Walter Roberson

### Walter Roberson (view profile)

on 22 Jun 2018
Yes, batchNormalizationLayer needs R2017b.

### Alex Alex (view profile)

Answer by Alex Alex

### Alex Alex (view profile)

on 10 Apr 2018

Undefined function or variable 'shaperead'. On the description page for this function it says "Introduced before R2006a". I use MATLAB R2015b- academic use. Does that mean that the function is not available for student license? How can I perform this kind of check in the future? Many thanks, Alex.

Florian Morsch

### Florian Morsch (view profile)

on 11 Apr 2018
Type "ver" into the Matlab Command Window. shaperead is part of the Mapping Toolbox, if you dont own it you cant use the function. Normaly it should be included in the academic license, so check if you have downloaded it. If not you may try to load it.
If its not for free in your version you may try to contact your IT so it can be included into the license.
Otherwise, if you have newly created the code try to restart matlab once, sometimes that helps, too.
Best regards

### tim jelly (view profile)

Answer by tim jelly

### tim jelly (view profile)

on 12 Apr 2018

When trying to make a GUI i get the error:
Undefined function or variable 'radioChanged'
Error while evaluating ButtonGroup SelectionChangedFcn.
I dont have "radioChanged" in my code so how do I fix this, thanks

Walter Roberson

### Walter Roberson (view profile)

on 12 Apr 2018
What shows up if you use
radios = findall(0, '-property', 'SelectionChangedFcn');

### Saadia Talay (view profile)

on 21 May 2018

Undefined function or variable 'lgemri' when I enter the following:
The nrrdread function has been taken from the matlab file exchange: https://www.mathworks.com/matlabcentral/fileexchange/34653-nrrd-format-file-reader
The lgemri is a file in nrrd format.

Walter Roberson

### Walter Roberson (view profile)

on 21 May 2018
lgemri should be a character vector that contains the name of the file.

### Saadia Talay (view profile)

on 23 May 2018
Thanks! That was stupid of me.

### Francisco Santamaría (view profile)

Answer by Francisco Santamaría

### Francisco Santamaría (view profile)

on 23 Jun 2018
Edited by Francisco Santamaría

### Francisco Santamaría (view profile)

on 23 Jun 2018

for i=1:(npop+1)
dron(i,:)=rand(1,nvar).*(xmax-xmin)+xmin;
cost(i)=CostfunctA3(dron(i,:));
dron_cost(i,:)=[dron(i,:) cost(i)]
end
When trying to make a run i get the following error:
Undefined function or variable 'CostfunctA3'.

#### 1 Comment

Stephen Cobeldick

### Stephen Cobeldick (view profile)

on 23 Jun 2018
@Francisco Santamaría: have you defined/downloaded a function named CostfunctA3? Is it on the MATLAB path?

### Waqas Waqas Ul Hussan (view profile)

Answer by Waqas Waqas Ul Hussan

### Waqas Waqas Ul Hussan (view profile)

on 23 Aug 2018
Edited by Walter Roberson

### Walter Roberson (view profile)

on 24 Aug 2018

hi
I am getting this problem in Matlab when plotting the graphs with shaded area.
Undefined function or variable 'jbfill'.
These below are my code lines. error is in line 127 below.

Arihant Sureka

### Arihant Sureka (view profile)

on 21 Sep 2018
Hi Walter
Appreciate you for your assistance. The solution was calling the Initialization script in the function callback GUI of Simulink (PreFcn and InitFcn).
Earlier the solution didn't work because of the bug in 2017b. Had to clear all the slrpj stuff.
Thanks and Cheers
RW Student

### RW Student (view profile)

on 29 Nov 2018
Hi Arihant, I am currently struggling with the same error using parsim as you described here. Can you please elaborate on how to solve this problem? I am using 2017b as well.
Arihant Sureka

### Arihant Sureka (view profile)

on 3 Jan 2019 at 21:50
Hi
Please try out these following steps -
1. After your parsim commands at the out command line use the following -
out = parsim(in,'TransferBaseWorkspaceVariables',on 'ShowProgress', 'on') as also mentioned on help page.
or
2. Use function callbacks which can be accesed in simulink from File-> Model Properties -> Model Properties -> Callbacks.
Then punch name(s) of your initialization script in the PreLoadFcn and InitFcn without .m at the end of the name.
For example - If your script is call "Init_SIM", then it would just go as Init_SIM in the callbacks and not as Init_SIM.m.
Regards

### David Akin (view profile)

Answer by David Akin

### David Akin (view profile)

on 1 Nov 2018
Edited by David Akin

### David Akin (view profile)

on 2 Nov 2018

Same error but using a Mathworks example. Here's the contents of fact.m
function f = fact(n)
f = prod(1:n);
end
Located here:
/opt/software/MATLAB/2018a/toolbox/local/fact.m
When trying to use:
>> y=fact(5);
Undefined function or variable 'fact'.
I cd'ed to the directory containing the file before starting MATLAB and it's in the search path:
>> path
MATLABPATH
/opt/software/MATLAB/2018a/toolbox/local
.
.
.
Any suggestions?
>> dos('cat /opt/software/MATLAB/2018a/toolbox/local/fact.m');
function f = fact(n)
f = prod(1:n);
end

Show 1 older comment
Walter Roberson

### Walter Roberson (view profile)

on 2 Nov 2018
Your path is /opt/software/MATLAB/2018a/toolbox/local but the fact.m is stored in /opt/Matlab/2018a/toolbox/local/fact.m which is a different directory
David Akin

### David Akin (view profile)

on 2 Nov 2018
I've fixed that. For some reason I've still getting the error.
>> dos('cat /opt/software/MATLAB/2018a/toolbox/local/fact.m');
function f = fact(n)
f = prod(1:n);
end
>> path
MATLABPATH
/opt/software/MATLAB/2018a/toolbox/local
...
>> y=fact(5);
Undefined function or variable 'fact'.
>>
Steven Lord

### Steven Lord (view profile)

on 2 Nov 2018
Do not store your own files in a directory under the matlabroot directory. See this documentation page for some of the reasons why. If you must do so, that documentation page will also tell you how you can.

### David Akin (view profile)

Answer by David Akin

### David Akin (view profile)

on 1 Nov 2018

It's an example only, as I've got some code from a colleage (a collection of .m files) I'd like to use. However it's not finding the functions. Thank you though.

### michael (view profile)

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

Walter Roberson

### Walter Roberson (view profile)

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 .