MATLAB Answers

5

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

MathWorks Support Team さんによって質問されました 2012 年 2 月 27 日
最新アクティビティ Regina Vivian Barli さんによって 回答されました 2019 年 7 月 8 日
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›.

16 件の回答

回答者: MathWorks Support Team 2018 年 5 月 17 日
編集済み: MathWorks Support Team 2018 年 5 月 17 日
 採用された回答

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)

  3 件のコメント

Dhruba Raj Dhakal 2017 年 6 月 30 日
Why this error occurs when I use antenna toolbox???
Dhruba Raj Dhakal 2017 年 7 月 2 日
this code is copied from https://www.mathworks.com/help/antenna/examples/antenna-array-analysis.html
Walter Roberson
2017 年 7 月 2 日
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

サインイン to comment.


回答者: Iman Tahamtan 2018 年 3 月 25 日

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

  1 件のコメント

Walter Roberson
2018 年 3 月 26 日
Where do you expect that your program should be getting a value of lambda from?

サインイン to comment.


回答者: ishwarya ramesh 2018 年 3 月 27 日

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

  2 件のコメント

Florian Morsch 2018 年 3 月 27 日
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
2018 年 3 月 27 日
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.

サインイン to comment.


回答者: Liliana Malik 2018 年 4 月 6 日

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

  5 件のコメント

Walter Roberson
2018 年 5 月 17 日
No, there is no replacement for pixelLabelDatastore in earlier versions.
Akib Rahman 2018 年 6 月 22 日
@Walter, batchNormalizationLayer also need Matlab R2017b?
Walter Roberson
2018 年 6 月 22 日
Yes, batchNormalizationLayer needs R2017b.

サインイン to comment.


Alex Alex
回答者: Alex Alex
2018 年 4 月 10 日

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 件のコメント

Florian Morsch 2018 年 4 月 11 日
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

サインイン to comment.


tim jelly
回答者: tim jelly
2018 年 4 月 12 日

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 件のコメント

Walter Roberson
2018 年 4 月 12 日
What shows up if you use
radios = findall(0, '-property', 'SelectionChangedFcn');
get(radios, 'SelectionChangedFcn')

サインイン to comment.


回答者: Saadia Talay 2018 年 5 月 21 日

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.

  2 件のコメント

Walter Roberson
2018 年 5 月 21 日
lgemri should be a character vector that contains the name of the file.
Saadia Talay 2018 年 5 月 23 日
Thanks! That was stupid of me.

サインイン to comment.


回答者: Francisco Santamaría 2018 年 6 月 23 日
編集済み: Francisco Santamaría 2018 年 6 月 23 日

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 件のコメント

Stephen Cobeldick
2018 年 6 月 23 日
@Francisco Santamaría: have you defined/downloaded a function named CostfunctA3? Is it on the MATLAB path?

サインイン to comment.


回答者: Waqas Waqas Ul Hussan 2018 年 8 月 23 日
編集済み: Walter Roberson
2018 年 8 月 24 日

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.

  6 件のコメント

Arihant Sureka 2018 年 9 月 21 日
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 2018 年 11 月 29 日
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 2019 年 1 月 3 日
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

サインイン to comment.


回答者: David Akin 2018 年 11 月 1 日
編集済み: David Akin 2018 年 11 月 2 日

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

  4 件のコメント

Walter Roberson
2018 年 11 月 2 日
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 2018 年 11 月 2 日
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
2018 年 11 月 2 日
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.

サインイン to comment.


回答者: David Akin 2018 年 11 月 1 日

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.

  0 件のコメント

サインイン to comment.


michael
回答者: michael
2018 年 11 月 14 日

(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 件のコメント

Walter Roberson
2018 年 11 月 14 日
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 .

サインイン to comment.


Darrell
回答者: Darrell
2019 年 2 月 6 日

I have seen this issue before with other functions. As stated before, first check that the function name is spelled correctly and that the function is located in the matlab search path. Assuming those two things check, then delete the path where the function is located, then reset the path. I would also will restart Matlab. I'm not sure why, but this seems to correct the problem.

  0 件のコメント

サインイン to comment.


Al3jandro
回答者: Al3jandro
2019 年 6 月 24 日

Hi.
I'm making this rutine, but i cant get values of K, how can I resolve this issue?
clear;
clc;
A=xlsread('ANÁLISIS DE ESTRUCTURAS _ INPUT2','BARRAS','B1:B6');
I=xlsread('ANÁLISIS DE ESTRUCTURAS _ INPUT2','BARRAS','C1:C6');
E=xlsread('ANÁLISIS DE ESTRUCTURAS _ INPUT2','BARRAS','D1:D6');
W=xlsread('ANÁLISIS DE ESTRUCTURAS _ INPUT2','BARRAS','u1:u6');
L=xlsread('ANÁLISIS DE ESTRUCTURAS _ INPUT2','BARRAS','Q1:Q6');
a=xlsread('ANÁLISIS DE ESTRUCTURAS _ INPUT2','BARRAS','R1:R6');
n=xlsread('ANÁLISIS DE ESTRUCTURAS _ INPUT2','BARRAS','V3');
nGDL=xlsread('ANÁLISIS DE ESTRUCTURAS _ INPUT2','NUDOS','J2');
GDLG=xlsread('ANÁLISIS DE ESTRUCTURAS _ INPUT2','BARRAS','K2:P4');
for i=1:n
A=A(i);
I=I(i);
E=E(i);
L=L(i);
a=a(i);
KL(i)=[E*A/L 0 0 -E*A/L 0 0;0 12*E*I/L^3 6*E*I/L^2 0 -12*E*I/L^3 6*E*I/L^2;0 6*E*I/L^2 4*E*I/L 0 -6*E*I/L^2 2*E*I/L;-E*A/L 0 0 E*A/L 0 0;0 -12*E*I/L^3 -6*E*I/L^2 0 12*E*I/L^3 -6*E*I/L^2;0 6*E*I/L^2 2*E*I/L 0 -6*E*I/L^2 4*E*I/L];
T(i)=[cos(a) sin(a) 0 0 0 0;-sin(a) cos(a) 0 0 0 0;0 0 1 0 0 0;0 0 0 cos(a) sin(a) 0;0 0 0 -sin(a) cos(a) 0;0 0 0 0 0 1];
KG(i)=T(i)'*KL(i)*T(i);
G=[GDLG(i,1) GDLG(i,2) GDLG(i,3) GDLG(i,4) GDLG(i,5) GDLG(i,6)];
KT=zeros(nGDL,nGDL);
KT(G,G)=KG(i);
if i==1
K=zeros(nGDL,nGDL);
end
K=K+KT;
end
disp(K)

  3 件のコメント

Walter Roberson
2019 年 6 月 24 日
What is the value of n?
Al3jandro
2019 年 6 月 24 日
n=3
Walter Roberson
2019 年 6 月 24 日
A=A(i) replaces the array A with a single scalar value. After the statement, A is size 1x1 and it is not possible to access A(2)

サインイン to comment.


clpi
回答者: clpi
2019 年 7 月 3 日

Hello !
I have a matlab function which at a certain point calculates sin(2*pi*freq*t_array) (t_array in an array of size (1,2000).
I tried to call this function via matlab.engine but I got the error message: "Undefined function 'sin' for input argument of type 'int64' "
I wanted to add the file 'sin.m' to my working directory but it is not a function script, it is a simple text.
I would be very grateful of any help
Thank you !

  0 件のコメント

サインイン to comment.


回答者: Regina Vivian Barli 2019 年 7 月 8 日

Hello, I happen to stumble upon similar problem.
So I have been trying to use matlab for video stabilising, but keep getting this error:
Undefined function or variable 'cvexEstStabilizationTform'.
even though I have followed Matlab's instruction by running a command by clicking
edit cvexEstStabilizationTform.m
Can anyone please suggest me what to do?
Kind regards
Vivian

  0 件のコメント

サインイン to comment.



Translated by