Special case of function not found even when in current directory or on path

조회 수: 46 (최근 30일)
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:\Users\taasher\tmp\matlabbug
Change the MATLAB current folder or add its folder to the MATLAB path.
Error in matlabbugfun2 (line 5)
out=matlabbugfun1();
Additional notes:
  1. Calling matlabbugfun1 from the command line (or a script) works just fine.
  2. Calling matlabbugfun2 from a script fails the same as is does on the command line.
  3. Putting a break in while running it shows that the if statement returns false and is not evaulated, as would be expected.
  4. Putting something like strfind(path,fileparts(which('matlabbugfun1'))) inside matlabbugfun2 will also show that during execution, Matlab thinks it IS on the path.
  5. 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 Version 24.1 (R2024a)
Signal Processing Toolbox Version 24.1 (R2024a)
Statistics and Machine Learning Toolbox Version 24.1 (R2024a)
  댓글 수: 2
Matt J
Matt J 2025년 8월 18일 21:31
편집: Matt J 2025년 8월 18일 21:32
Seeing the behavior confirmed by others, I just submitted a bug report, Case 08020464.
I'd be interested to hear if they confirm it is a bug (but I don't think they will).
orsijafdsoij
orsijafdsoij 2025년 8월 20일 16:21
They said it's not an error, responded similarly to others here. My main request would be a better error message, as it was quite confusing and counterintuitive even though I have considerable Matlab experience.

댓글을 달려면 로그인하십시오.

채택된 답변

Matt J
Matt J 2025년 8월 18일
편집: Matt J 2025년 8월 18일
It's because the file is preparsed to see if matlabbugfun1 is being used as a variable name or as a function name. You can avoid this as follows,
function out=matlabbugfun2
matlabbugfun1=@matlabbugfun1;
if ~exist('matlabbugfun1.m','file')
matlabbugfun1=@()(4);
end
out=matlabbugfun1();
end
  댓글 수: 3
Image Analyst
Image Analyst 2025년 8월 20일 20:28
Instead of
if ~exist('matlabbugfun1.m','file')
use
if ~isfile('matlabbugfun1.m')
orsijafdsoij
orsijafdsoij 2025년 8월 20일 20:57
@Image Analyst that wouldn't suit the broader problem here because isfile only checks if the file is in the current folder, it doesn't check the search path.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Walter Roberson
Walter Roberson 2025년 8월 18일
This is not a bug.
MATLAB traces assignments to try to deduce whether a given name is a function or a variable. Any name that is assigned to (even conditionally) is assumed to be a variable, and using such a name before it is defined is assumed to be a programming bug.
  댓글 수: 1
Steven Lord
Steven Lord 2025년 8월 18일 17:31
You can see this with the following code. The attempt to call the plot() function on the first line of canWeCallPlot will error. The assignment to the identifier plot on the second line makes MATLAB decide "plot is a variable" and so that attempt on the first line is an attempt to index into a variable that doesn't exist yet.
canWeCallPlot()
Unrecognized function or variable 'plot'.

Error in solution>canWeCallPlot (line 3)
plot(1:10)
^^^^^^^^^^
function canWeCallPlot()
plot(1:10)
plot = 42;
end
If I'd reversed the order of the lines I'd get a different error, because the index vector 1:10 includes an index beyond the number of elements in the variable being indexed into.

댓글을 달려면 로그인하십시오.


Ron
Ron 2025년 8월 18일
You can put the function in the same script and then see first if there is any error with the function. Please try this and see if it helps.
clear all; clc; close all;
out=matlabbugfun1()
% Now from the command line:
matlabbugfun2
function out=matlabbugfun1
out=2;
% Second function, saved to current directory:
end
function out=matlabbugfun2
if exist('matlabbugfun1.m')~=2
matlabbugfun1=@()(4);
end
end
  댓글 수: 1
orsijafdsoij
orsijafdsoij 2025년 8월 18일
편집: orsijafdsoij 2025년 8월 18일
Note your matlabbugfun2 is missing the line where it calls matlabbugfun1. If you add that in, it errors in the same way as in my example. So it appears the bug persists for subfunctions.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Naming Conventions에 대해 자세히 알아보기

태그

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by