Asked by MathWorks Support Team
on 27 Feb 2012

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

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;

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)

Answer by Iman Tahamtan
on 25 Mar 2018

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

Walter Roberson
on 26 Mar 2018

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

Florian Morsch
on 27 Mar 2018

Walter Roberson
on 27 Mar 2018

Sign in to comment.

Answer by Liliana Malik
on 6 Apr 2018

Walter Roberson
on 17 May 2018

No, there is no replacement for pixelLabelDatastore in earlier versions.

Akib Rahman
on 22 Jun 2018

@Walter, batchNormalizationLayer also need Matlab R2017b?

Walter Roberson
on 22 Jun 2018

Yes, batchNormalizationLayer needs R2017b.

Sign in to comment.

Answer by Alex Alex
on 10 Apr 2018

Florian Morsch
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

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

Walter Roberson
on 12 Apr 2018

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:

[X,meta]=nrrdread(lgemri);

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
on 21 May 2018

lgemri should be a character vector that contains the name of the file.

Saadia Talay
on 23 May 2018

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

Stephen Cobeldick
on 23 Jun 2018

Sign in to comment.

Answer by Waqas Waqas Ul Hussan
on 23 Aug 2018

Edited by Walter Roberson
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
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
on 29 Nov 2018

Arihant Sureka
on 3 Jan 2019

Hi

Please try out these following steps -

- 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

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);

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

Walter Roberson
on 2 Nov 2018

David Akin
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
on 2 Nov 2018

Sign in to comment.

Answer by David Akin
on 1 Nov 2018

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

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 .

Sign in to comment.

Answer by Darrell
on 6 Feb 2019

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 3 Comments

## Dhruba Raj Dhakal (view profile)

Direct link to this comment:https://it.mathworks.com/matlabcentral/answers/96005-why-do-i-get-the-error-undefined-function-or-variable#comment_465540

## Dhruba Raj Dhakal (view profile)

Direct link to this comment:https://it.mathworks.com/matlabcentral/answers/96005-why-do-i-get-the-error-undefined-function-or-variable#comment_465918

## Walter Roberson (view profile)

Direct link to this comment:https://it.mathworks.com/matlabcentral/answers/96005-why-do-i-get-the-error-undefined-function-or-variable#comment_465919

Sign in to comment.