Special case of function not found even when in current directory or on path
Seeing the behavior confirmed by others, I just submitted a bug report, Case 08020464.
Matlab (v2024a or 2024b) is unable to identify a function in my current directory and/or on my path, if called from another function that has an if-statement like the one shown in the example below. First function, saved to current directory:
function out=matlabbugfun1
out=6;
end
Second function, saved to current directory:
function out=matlabbugfun2
if exist(‘matlabbugfun1.m’)~=2
matlabbugfun1=@()(4);
end
out=matlabbugfun1();
end
Now from the command line:
matlabbugfun2
And I get:
Unrecognized function or variable ‘matlabbugfun1’.
Error in matlabbugfun2 (line 5)
out=matlabbugfun1();
I have validated this on 2 computers, one with 2024a, the other with 2024b. Note that if the functions are not on your current folder but are on your path:
a=pwd;
addpath(pwd)
cd ..
matlabbugfun2
Then you get a slightly different error:
matlabbugfun1 is not found in the current folder or on the MATLAB path, but exists in:
C:Userstaashertmpmatlabbug
Change the MATLAB current folder or add its folder to the MATLAB path.
Error in matlabbugfun2 (line 5)
out=matlabbugfun1();
Additional notes:
Calling matlabbugfun1 from the command line (or a script) works just fine.
Calling matlabbugfun2 from a script fails the same as is does on the command line.
Putting a break in while running it shows that the if statement returns false and is not evaulated, as would be expected.
Putting something like strfind(path,fileparts(which(‘matlabbugfun1’))) inside matlabbugfun2 will also show that during execution, Matlab thinks it IS on the path.
As noted in my response to Ron’s answer, if the two functions are made subfunctions of a main script that calls matlabbugfun2, the same error occurs. However the script can call matlabbugfun1 without issue.
Output from ver:
—————————————————————————————————–
MATLAB Version: 24.1.0.2653294 (R2024a) Update 5
MATLAB License Number: ••••••••
Operating System: Microsoft Windows 11 Enterprise Version 10.0 (Build 26100)
Java Version: Java 1.8.0_202-b08 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
—————————————————————————————————–
MATLAB Version 24.1 (R2024a)
Signal Processing Toolbox Version 24.1 (R2024a)
Statistics and Machine Learning Toolbox Version 24.1 (R2024a)Seeing the behavior confirmed by others, I just submitted a bug report, Case 08020464.
Matlab (v2024a or 2024b) is unable to identify a function in my current directory and/or on my path, if called from another function that has an if-statement like the one shown in the example below. First function, saved to current directory:
function out=matlabbugfun1
out=6;
end
Second function, saved to current directory:
function out=matlabbugfun2
if exist(‘matlabbugfun1.m’)~=2
matlabbugfun1=@()(4);
end
out=matlabbugfun1();
end
Now from the command line:
matlabbugfun2
And I get:
Unrecognized function or variable ‘matlabbugfun1’.
Error in matlabbugfun2 (line 5)
out=matlabbugfun1();
I have validated this on 2 computers, one with 2024a, the other with 2024b. Note that if the functions are not on your current folder but are on your path:
a=pwd;
addpath(pwd)
cd ..
matlabbugfun2
Then you get a slightly different error:
matlabbugfun1 is not found in the current folder or on the MATLAB path, but exists in:
C:Userstaashertmpmatlabbug
Change the MATLAB current folder or add its folder to the MATLAB path.
Error in matlabbugfun2 (line 5)
out=matlabbugfun1();
Additional notes:
Calling matlabbugfun1 from the command line (or a script) works just fine.
Calling matlabbugfun2 from a script fails the same as is does on the command line.
Putting a break in while running it shows that the if statement returns false and is not evaulated, as would be expected.
Putting something like strfind(path,fileparts(which(‘matlabbugfun1’))) inside matlabbugfun2 will also show that during execution, Matlab thinks it IS on the path.
As noted in my response to Ron’s answer, if the two functions are made subfunctions of a main script that calls matlabbugfun2, the same error occurs. However the script can call matlabbugfun1 without issue.
Output from ver:
—————————————————————————————————–
MATLAB Version: 24.1.0.2653294 (R2024a) Update 5
MATLAB License Number: ••••••••
Operating System: Microsoft Windows 11 Enterprise Version 10.0 (Build 26100)
Java Version: Java 1.8.0_202-b08 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
—————————————————————————————————–
MATLAB Version 24.1 (R2024a)
Signal Processing Toolbox Version 24.1 (R2024a)
Statistics and Machine Learning Toolbox Version 24.1 (R2024a) Seeing the behavior confirmed by others, I just submitted a bug report, Case 08020464.
Matlab (v2024a or 2024b) is unable to identify a function in my current directory and/or on my path, if called from another function that has an if-statement like the one shown in the example below. First function, saved to current directory:
function out=matlabbugfun1
out=6;
end
Second function, saved to current directory:
function out=matlabbugfun2
if exist(‘matlabbugfun1.m’)~=2
matlabbugfun1=@()(4);
end
out=matlabbugfun1();
end
Now from the command line:
matlabbugfun2
And I get:
Unrecognized function or variable ‘matlabbugfun1’.
Error in matlabbugfun2 (line 5)
out=matlabbugfun1();
I have validated this on 2 computers, one with 2024a, the other with 2024b. Note that if the functions are not on your current folder but are on your path:
a=pwd;
addpath(pwd)
cd ..
matlabbugfun2
Then you get a slightly different error:
matlabbugfun1 is not found in the current folder or on the MATLAB path, but exists in:
C:Userstaashertmpmatlabbug
Change the MATLAB current folder or add its folder to the MATLAB path.
Error in matlabbugfun2 (line 5)
out=matlabbugfun1();
Additional notes:
Calling matlabbugfun1 from the command line (or a script) works just fine.
Calling matlabbugfun2 from a script fails the same as is does on the command line.
Putting a break in while running it shows that the if statement returns false and is not evaulated, as would be expected.
Putting something like strfind(path,fileparts(which(‘matlabbugfun1’))) inside matlabbugfun2 will also show that during execution, Matlab thinks it IS on the path.
As noted in my response to Ron’s answer, if the two functions are made subfunctions of a main script that calls matlabbugfun2, the same error occurs. However the script can call matlabbugfun1 without issue.
Output from ver:
—————————————————————————————————–
MATLAB Version: 24.1.0.2653294 (R2024a) Update 5
MATLAB License Number: ••••••••
Operating System: Microsoft Windows 11 Enterprise Version 10.0 (Build 26100)
Java Version: Java 1.8.0_202-b08 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
—————————————————————————————————–
MATLAB Version 24.1 (R2024a)
Signal Processing Toolbox Version 24.1 (R2024a)
Statistics and Machine Learning Toolbox Version 24.1 (R2024a) bug, matlab MATLAB Answers — New Questions