Special case of function not found even when in current directory or on path
123 ビュー (過去 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:
- 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 Version 24.1 (R2024a)
Signal Processing Toolbox Version 24.1 (R2024a)
Statistics and Machine Learning Toolbox Version 24.1 (R2024a)
2 件のコメント
採用された回答
Matt J
2025 年 8 月 18 日 15:22
編集済み: Matt J
2025 年 8 月 18 日 15:31
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
2025 年 8 月 20 日 20:28
Instead of
if ~exist('matlabbugfun1.m','file')
use
if ~isfile('matlabbugfun1.m')
その他の回答 (2 件)
Walter Roberson
2025 年 8 月 18 日 15:28
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
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()
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
2025 年 8 月 18 日 15:02
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
参考
カテゴリ
Help Center および File Exchange で Naming Conventions についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!