matlabpath - order not respected?

4 views (last 30 days)
In one of the toolboxes I have downloaded there are some functions (erf, gamma) that are shading the corresponding built-in functions, even when I add that directory to the end of the matlab-path:
>> which gamma -all
built-in (/usr/local/MATLAB/R2020a/toolbox/matlab/specfun/@double/gamma) % double method
built-in (/usr/local/MATLAB/R2020a/toolbox/matlab/specfun/@single/gamma) % single method
/usr/local/MATLAB/R2020a/toolbox/symbolic/symbolic/@sym/gamma.m % sym method
/usr/local/MATLAB/R2020a/toolbox/parallel/parallel/@codistributed/gamma.m % codistributed method
/usr/local/MATLAB/R2020a/toolbox/parallel/gpu/@gpuArray/gamma.m % gpuArray method
>> addpath /home/bgu001/matlab/Local/Numerics/Polpack/ -end
>> which gamma -all
/home/bgu001/matlab/Local/Numerics/Polpack/gamma.m
built-in (/usr/local/MATLAB/R2020a/toolbox/matlab/specfun/@double/gamma) % double method
built-in (/usr/local/MATLAB/R2020a/toolbox/matlab/specfun/@single/gamma) % single method
/usr/local/MATLAB/R2020a/toolbox/symbolic/symbolic/@sym/gamma.m % sym method
/usr/local/MATLAB/R2020a/toolbox/parallel/parallel/@codistributed/gamma.m % codistributed method
/usr/local/MATLAB/R2020a/toolbox/parallel/gpu/@gpuArray/gamma.m % gpuArray method
>> rmpath('/home/bgu001/matlab/Local/Numerics/Polpack/')
>> which gamma -all
built-in (/usr/local/MATLAB/R2020a/toolbox/matlab/specfun/@double/gamma) % double method
built-in (/usr/local/MATLAB/R2020a/toolbox/matlab/specfun/@single/gamma) % single method
/usr/local/MATLAB/R2020a/toolbox/symbolic/symbolic/@sym/gamma.m % sym method
/usr/local/MATLAB/R2020a/toolbox/parallel/parallel/@codistributed/gamma.m % codistributed method
/usr/local/MATLAB/R2020a/toolbox/parallel/gpu/@gpuArray/gamma.m % gpuArray method
This is not what I have learnt to expect. Matlab should use the first function on the path with the matching name, shouldn't it? Is this something others have encountered? Is it a new behaviour? What can I have done to cause me this?
The versions this definitely appears on are R2020a and 2022b (Update 3)

Accepted Answer

Walter Roberson
Walter Roberson on 5 Oct 2023
Matlab should use the first function on the path with the matching name, shouldn't it?
All of those method that are listed are object functions, which are priority 7
Functions elsewhere on the path, in order of appearance are priority 11.
When you addpath that directory, the gamma there becomes the only regular function on the search path with name gamma . The other gamma are only invoked if one of the parameters is the correct data type -- and if the parameters are the correct datatype then those have higher priority than the function you added on the path.
Why does MATLAB list the path first before the methods? I do not know -- but that does not affect resolution priority. The path is not searched until after it is determined that the function call is not an object method for class of the parameters.
  1 Comment
Bjorn Gustavsson
Bjorn Gustavsson on 5 Oct 2023
Thanks, that explains this.
For others that might stumble on this answer this might be a useful advice:
Run which with an input argument of the correct type:
which erf(pi) -all
For an explicitly correct and understandable listing, instead of
which erf -all

Sign in to comment.

More Answers (0)

Categories

Find more on Special Functions in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!