9,502 views (last 30 days)

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
on 27 Sep 2019

Edited: MathWorks Support Team
on 27 Sep 2019

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)

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?

ishwarya ramesh
on 27 Mar 2018

Florian Morsch
on 27 Mar 2018

Walter Roberson
on 27 Mar 2018

Liliana Malik
on 6 Apr 2018

Walter Roberson
on 17 May 2018

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

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

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

Saadia Talay
on 21 May 2018

Edited: Walter Roberson
on 31 May 2020

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.

Francisco Santamaría
on 23 Jun 2018

Edited: 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

Waqas Waqas Ul Hussan
on 23 Aug 2018

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

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

yousra aichoun
on 6 May 2020

hi ,

can you help me please i'm having the same problem but with simulink ,once i run the simulation , it display :Undefined function 'modelRegistry' for input arguments of type 'PmSli.RunTimeModule'.

David Akin
on 1 Nov 2018

Edited: 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

David Akin
on 1 Nov 2018

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 .

Darrell
on 6 Feb 2019

Al3jandro
on 24 Jun 2019

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)

Walter Roberson
on 24 Jun 2019

clpi
on 3 Jul 2019

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 !

Regina Vivian Barli
on 8 Jul 2019

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

Ashwanth Ramesh
on 18 Oct 2019

Sara Alkhaldi
on 29 Jan 2020

Walter Roberson
on 29 Jan 2020

abood qamar
on 1 Apr 2020

can please help me to solve this problem

Image Analyst
on 1 Apr 2020

Why do you think that function SHOULD exist? Do you think it's a built-in toolbox function or did you write it? Is it on the search path? If so, this should show some existing file:

which -all guard_interval

If you still need more help, create a new question rather than continue the discussion here.

apri zulham
on 19 Apr 2020

i need help!!

Undefined function or variable 'imaghwinfo'.

Error in CAMERA_MATLAB>pushbutton1_Callback (line 81)

IAHI = imaghwinfo;

Error in gui_mainfcn (line 95)

feval(varargin{:});

Error in CAMERA_MATLAB (line 42)

gui_mainfcn(gui_State, varargin{:});

Error in

matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)CAMERA_MATLAB('pushbutton1_Callback',hObject,eventdata,guidata(hObject))

81 IAHI = imaghwinfo;

Walter Roberson
on 20 Apr 2020

The proper function name is imaghwinfo . You need to change line 81 of CAMERA_MATLAB.m

CS
on 20 Apr 2020

Undefined function or variable 'readmatrix'.

I have a basic_matrix.txt file including

6,8,3,1

5,4,7,3

1,6,7,10

4,2,8,2

2,7,5,9

I want to read the contents of this file (basic_matrix.txt). When I write

M = readmatrix('basic_matrix.txt')

MATLAB gives an error as

Undefined function or variable 'readmatrix'.

M = readmatrix('basic_matrix.txt')

Does anyone know what the reason is?

I am just trying to implement what is written on https://www.mathworks.com/help/matlab/ref/readmatrix.html in order to read the contents of a file. I am using MATLAB R2018b.

Any help would be appreciated.

Walter Roberson
on 20 Apr 2020

needs r2019a.

For your release use readtable with readvariablenames false and then table2array

Alex Backer
on 24 Apr 2020

José Moctezuma Rodríguez Santillán
on 9 May 2020

Edited: Walter Roberson
on 9 May 2020

Undefined function or variable 'isfile'. You cannot find the isfile function in MATLAB R2015. any alternative to replace? this is my code;

options = weboptions('Username', 'insertusername', 'Password', 'insertpassword');

year = 2016;

month = 03;

time = 0900;

start_jd = 011;

end_jd = 045;

for jd = start_jd:end_jd

cd = day(datetime((year-1),12,31) + days(jd));

filename = ['SST' num2str(year) '_' num2str(jd) '.nc'];

if isfile(filename)

fprintf('already have the file |%s|\n',filename);

else

url=['https://data.nodc.noaa.gov/ghrsst/L4/GLOB/JPL_OUROCEAN/G1SST/2016/' num2str(year) '/' num2str(jd)];

outname=websave(filename,url,options);

fprintf('got weather data file |%s|\n',outname);

end

end

Yinying Wang
on 16 May 2020

Help!

I received "undefined function 'string2char' for 'char' type input arguments".

This error occurs only when I use mphload('filename') in an exe file. When in a .m file this line works well. I thought maybe mphload use 'string2char' function whatever its input argument type is. I use mphload("filename") instead. It still doesn't work...

Joey Porter
on 7 Jul 2020

Hi all,

I've had this error after a newly created function would give this error even though it was in the correct path and was user-defined (so no problem with needing the correct toolbox or licencing).

The name of the function and the file name you save it as MUST BE THE SAME.

I had saved my function a different name and therefore gave this error. This tip isn't in the accepted answer's response so give this a try.

Joey Porter
on 8 Jul 2020

I've just checked my source (https://uk.mathworks.com/help/matlab/matlab_prog/calling-functions.html) and you are correct, apologies!

I have rechecked my file names and they are slightly different (not sure how I solved this error then?).

Thank you for you correction and I'll delete this post after you've seen this to avoid confusion.

soufiane kabiri
on 18 Sep 2020

Edited: Walter Roberson
on 30 Nov 2020

hi everyone, i hope you'll help me with that

i have the same error "Undefined function or variable 'HMMem'"

this the code:

function [Q, g, l] = HMMbaumwelch(y, nu, tol, maxIt, Q, g)

% HMMbaumwelch compute maximum likehood estimate using Expectation-Maximization

% iterations

%

% in : y = vector of observations

% nu = initial distribution of the hidden chain

% tol = tolerance for the stopping criterion

% maxIt = maximal number of iterations

% out : Q = estimate of the transition matrix of the hidden markov process

% g = estimated probabilities of transition: gh(x,y) = estimate of P(Y=y | X=x) for 1<=x<=k

% l = log-likelihood of y for parameters Q and g

%

% Example :

n = 10000;

nu = [0, 1];

Q = [0.8, 0.2; 0.1, 0.9];

g = [0.25 0.25 0.25 0.25; 0.05 0.05 0.45 0.45];

[x,y] = HMMsample(nu, Q, g, n);

[Qh, gh] = HMMem(y, nu);

% % compare estimates with truth: note that the order of the hidden

% % states may not be preserved

Q, Qh

g, gh

% References: Hidden Markov Models by Cappe, Moulines, Rydden

% Springer series in statistics

% by Aurelien Garivier, CNRS & Telecom ParisTech

% last revision February 7, 2012

global myfilter mysmoother % should be either HMMfilter/HMMsmoother, or HMMfilter_C/HMMsmoother_C

if nargin<4, maxIt = 100; end

if nargin<3, tol = 1e-4; end

k = length(nu); r = max(y); n = length(y);

Y = zeros(n, r); Y(sub2ind([n, r], 1:n, y))=1;

% if they are not provided, sample random initial transition and emission matrices

if nargin<5, Q = rand(k); Q = Q ./ (sum(Q, 2)*ones(1, k)); end

if nargin<6, g = rand(k, r); g = g ./ (sum(g, 2)*ones(1, r)); end

it = 0; oldQ = Q; oldg = g+tol+1;

while ((norm(oldQ(:)-Q(:), 1) + norm(oldg-g, 1) > tol) && (it<maxIt))

it = it + 1;

% compute the posterior distribution for the current parameters

[phi, c] = myfilter(y, nu, Q, g);

beta = mysmoother(y, Q, g, c);

post = phi .* beta;

% expectation of the number of transitions under the current parameters

N =Q.*(phi(:, 1:(end-1))*(beta(:, 2:end).*g(:, y(2:end))./(ones(k, 1)*c(2:end)))');

% expectation of the number of emissions

M = post * Y;

% re-estimation

oldQ = Q; oldg = g;

Q = N ./ (sum(N, 2) * ones(1, k));

g = M ./ (sum(M, 2) * ones(1, r));

end

l = sum(log(c));

Walter Roberson
on 30 Nov 2020

Your code does not show any function definition for HMMen .

Your code looks like a modified version of https://github.com/ryanrhymes/ehmm/blob/master/%2Bhmm/HMMbaumwelch.m

Nicole Yousef
on 29 Nov 2020

I'm having the same issue for trying to read a Java file (TimeSeriesTableVec3) from a MATLAB function:

Unrecognized function or variable 'TimeSeriesTableVec3'.

TimeSeriesTableVec3 is a Java file that is present in the same folder.

(Additional information: resetting the default path caused MATLAB to try to create a .m file out of the Java file, which then fails to run.)

Nicole Yousef
on 30 Nov 2020

I don't think it came with a .jar file, should I be looking for one? I don't have any Java background, only MATLAB.

TimeSeriesTableVec3.java is provided by OpenSim, and I can't find any .jar files where I would have installed it. Here's the GitHub showing the contents of the folder containing TimeSeriesTableVec3.java:

https://github.com/opensim-org/opensim-gui/tree/master/Gui/opensim/modeling/src/org/opensim/modeling

Also to make sure I am being clear, I am running a MATLAB .m file that calls the MATLAB function osimTableFromStruct. (Also linked in case seeing the code might help.) The osimTableFromStuct.m file then calls the TimeSeriesTableVec3.java file in line 80.

Walter Roberson
on 30 Nov 2020

I suspect that you need to convert them to .class

https://www.mathworks.com/help/matlab/matlab_external/accessing-your-own-java-classes.html

Nicole Yousef
on 1 Dec 2020

I have gotten this code to work without compiling it once before on a Macbook just like this, so it is unlikely that it is necessary to compile it. I tried compiling it using an online compiler and with my Mac's terminal. So far have run into 57 errors both times converting it online and in my Mac's terminal. The errors look like this:

error: cannot find symbol

public class TimeSeriesTableVec3 extends DataTableVec3 {

^

symbol: class DataTableVec3

TimeSeriesTableVec3.java:48: error: cannot find symbol

public TimeSeriesTableVec3(StdVectorDouble indVec, MatrixVec3 depData, StdVectorString labels) {

^

symbol: class StdVectorDouble

location: class TimeSeriesTableVec3

TimeSeriesTableVec3.java:48: error: cannot find symbol

public TimeSeriesTableVec3(StdVectorDouble indVec, MatrixVec3 depData, StdVectorString labels) {

^

symbol: class MatrixVec3

location: class TimeSeriesTableVec3

TimeSeriesTableVec3.java:48: error: cannot find symbol

public TimeSeriesTableVec3(StdVectorDouble indVec, MatrixVec3 depData, StdVectorString labels) {

^

... and so on, where every class they point out is listed in the next symbol.

I think it might require having the "package org.opensim.modeling;" available in the same space or text box where I am trying to compile the Java file?

(Also thank you so much for your help!)

islam dib
on 3 Dec 2020

Undefined function or variable 'seriallist'.

release 2013b

what's wrong ?

Walter Roberson
on 3 Dec 2020

AMAR Abdelhamid
on 6 Dec 2020

hello

i have this problem :

Error using mphload

Cannot find COMSOL server

how can i sole it, please.

Walter Roberson
on 6 Dec 2020

Have you installed the third-party product COMSOL ?

Eamon Devlin
on 21 Jan 2021 at 12:48

I have a probelm:

I have a list of varibles defined at the top of my script but when I am trying to create a function the defined variables (which are in the workspace too) are not being recognised.

%% Damped Oscillating Spring System

m_c = 2; %Container mass in kg

s1 = 16.0; % Spring constant, spring 1, N/m

s2 = 16.0; % Spring constant, spring 2, N/m

c = 2.0; % Damping Coefficient, Ns/m

d = 0.6; % Initial gap between mass and spring 2, m

t_total = 20.0; % Total interation time, s

dt = 0.01; % Interation Step, s

x =1; % Initial displacement, m

g = -9.81; % Gravitational Acceleration, m/s^-2

t = 0: dt: t_total; % Total Time for plotting graphs

r_m = 0.1; % Rate of mass of fluid change in kg/s

m_f_max = 4; % Maximum Fluid mass

%Then there is a choice menu and other calculations where the cases are all set out:

case {4} %Select if the containter is filled with fluid duing the simulation

[r_m] = fluidcalcs();

function [r_m] = fluidcalcs()

r_m = input('Rate of Change of Fluid Mass (kg/s)?\n\n');

if (r_m * t_total) > 4

disp('Invalid Entry - Maximum Fluid Mass is 4kg\n\n');

r_m = input('Rate of Change of Fluid Mass (kg/s)?\n\n');

end

end

In this case the 't_total' variable is not being recognised. The code works fine if I replace the variable name with the value. But the varables need to be changed each time so having a set value isn't what I want.

Any solutions?

Walter Roberson
on 21 Jan 2021 at 17:05

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
## 7 Comments

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/96005-why-do-i-get-the-error-undefined-function-or-variable#comment_465540

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/96005-why-do-i-get-the-error-undefined-function-or-variable#comment_465540

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/96005-why-do-i-get-the-error-undefined-function-or-variable#comment_465918

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/96005-why-do-i-get-the-error-undefined-function-or-variable#comment_465918

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/96005-why-do-i-get-the-error-undefined-function-or-variable#comment_465919

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/96005-why-do-i-get-the-error-undefined-function-or-variable#comment_465919

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/96005-why-do-i-get-the-error-undefined-function-or-variable#comment_876659

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/96005-why-do-i-get-the-error-undefined-function-or-variable#comment_876659

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/96005-why-do-i-get-the-error-undefined-function-or-variable#comment_876911

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/96005-why-do-i-get-the-error-undefined-function-or-variable#comment_876911

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/96005-why-do-i-get-the-error-undefined-function-or-variable#comment_969318

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/96005-why-do-i-get-the-error-undefined-function-or-variable#comment_969318

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/96005-why-do-i-get-the-error-undefined-function-or-variable#comment_1188968

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/96005-why-do-i-get-the-error-undefined-function-or-variable#comment_1188968

Sign in to comment.