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

123 ビュー (過去 30 日間)
orsijafdsoij
orsijafdsoij 2025 年 8 月 18 日 14:53
コメント済み: orsijafdsoij 2025 年 8 月 20 日 20:57
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 日 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
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 日 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
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 日 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
  1 件のコメント
orsijafdsoij
orsijafdsoij 2025 年 8 月 18 日 15:04
編集済み: orsijafdsoij 2025 年 8 月 18 日 15:12
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 Center および File ExchangeNaming Conventions についてさらに検索

タグ

製品


リリース

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by