MATLAB Answers


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

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›.

12 Answers

Answer by MathWorks Support Team on 17 May 2018
Edited by MathWorks Support Team on 17 May 2018
 Accepted Answer

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;
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.
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
Example demonstrating this error:
UndefinedFunctionOrVariable.m (attached)


Why this error occurs when I use antenna toolbox???
this code is copied from
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

Sign in to comment.

Answer by Iman Tahamtan on 25 Mar 2018

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

  1 Comment

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

Sign in to comment.

Answer by ishwarya ramesh on 27 Mar 2018

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


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.
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.

Sign in to comment.

Answer by Liliana Malik on 6 Apr 2018

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


No, there is no replacement for pixelLabelDatastore in earlier versions.
@Walter, batchNormalizationLayer also need Matlab R2017b?
Yes, batchNormalizationLayer needs R2017b.

Sign in to comment.

Answer by Alex Alex 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.

  1 Comment

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

Sign in to comment.

Answer by tim jelly 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

  1 Comment

What shows up if you use
radios = findall(0, '-property', 'SelectionChangedFcn');
get(radios, 'SelectionChangedFcn')

Sign in to comment.

Answer by Saadia Talay 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:
The lgemri is a file in nrrd format.


lgemri should be a character vector that contains the name of the file.
Thanks! That was stupid of me.

Sign in to comment.

Answer by Francisco Santamaría on 23 Jun 2018
Edited by Francisco Santamaría on 23 Jun 2018

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

  1 Comment

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

Sign in to comment.

Answer by Waqas Waqas Ul Hussan on 23 Aug 2018
Edited by Walter Roberson
on 24 Aug 2018

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.


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
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.
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.
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.

Sign in to comment.

Answer by David Akin on 1 Nov 2018
Edited by David Akin 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);
Located here:
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
Any suggestions?
>> dos('cat /opt/software/MATLAB/2018a/toolbox/local/fact.m');
function f = fact(n)
f = prod(1:n);


Show 1 older comment
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
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);
>> path
>> y=fact(5);
Undefined function or variable 'fact'.
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.
Consider storing your files in your userpath folder instead.

Sign in to comment.

Answer by David Akin 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.


Sign in to comment.

Answer by 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

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 .

Sign in to comment.