How to find a function in the path – securely?
What is a safe method to check, where a specific function exists in Matlab’s path? It should not matter, if the function is an M, P or MEX file.
Actually, this is a job for which() . To avoid collisions with local variables, it should be hidden in a function:
function Reply = safeWhich(varargin)
Reply = which(varargin{:});
end
This fails for ‘varargin’, as expected:
safeWhich(‘varargin’)
But there are further traps
safeWhich(‘R’) % Same for ‘P’
In Matlab R2018b under Windows I get:
‘C:Program FilesMATLABR2018btoolboxmatlabcodetools@mtreemtree.m % mtree method’
The appended comment allowed to exclude the output by checking, if a corrensponding file exists:
function Reply = safeWhich_2(varargin)
Reply = which(varargin{:});
Reply = cellstr(Reply);
Reply = Reply(isfile(Reply)); % Worked in R2018b, no effect in R2025a
end
safeWhich_2(‘R’)
The exist() command does have the power to find functions properly, but it does not reveal where.
The old function depfun() was replaced by the ugly matlab.codetools.requiredFilesAndProducts(). I could search in the corresponding code, couldn’t I? This function uses the code find here internally: "toolboxmatlabdepfun+matlab+depfun+internal+whichcallWhich.m". Here I find 91 functions and 11.3 MB of code. One function looks like this:
% 10^(-18) is effective zero in terms of possibility, which requries
% log(10^18)/log(26+10+1) = 12 random letters, digits, or underscores.
function klno6phn_9faskf_na = callWhich(asm_foyan_knaouie8)
klno6phn_9faskf_na = which(asm_foyan_knaouie8);
end
Obviously MathWorks struggles with comparable problems.
The profiler shows, that matlab.codetools.requiredFilesAndProducts() calls 207 subfunctions (R2018b).
See the discussion: https://www.mathworks.com/matlabcentral/discussions/ideas/887763-i-have-been-a-matlab-loyalist-for-25-years . The complexity explodes, such that a simple task like searching a function file needs a pile of exceptions and indirections.What is a safe method to check, where a specific function exists in Matlab’s path? It should not matter, if the function is an M, P or MEX file.
Actually, this is a job for which() . To avoid collisions with local variables, it should be hidden in a function:
function Reply = safeWhich(varargin)
Reply = which(varargin{:});
end
This fails for ‘varargin’, as expected:
safeWhich(‘varargin’)
But there are further traps
safeWhich(‘R’) % Same for ‘P’
In Matlab R2018b under Windows I get:
‘C:Program FilesMATLABR2018btoolboxmatlabcodetools@mtreemtree.m % mtree method’
The appended comment allowed to exclude the output by checking, if a corrensponding file exists:
function Reply = safeWhich_2(varargin)
Reply = which(varargin{:});
Reply = cellstr(Reply);
Reply = Reply(isfile(Reply)); % Worked in R2018b, no effect in R2025a
end
safeWhich_2(‘R’)
The exist() command does have the power to find functions properly, but it does not reveal where.
The old function depfun() was replaced by the ugly matlab.codetools.requiredFilesAndProducts(). I could search in the corresponding code, couldn’t I? This function uses the code find here internally: "toolboxmatlabdepfun+matlab+depfun+internal+whichcallWhich.m". Here I find 91 functions and 11.3 MB of code. One function looks like this:
% 10^(-18) is effective zero in terms of possibility, which requries
% log(10^18)/log(26+10+1) = 12 random letters, digits, or underscores.
function klno6phn_9faskf_na = callWhich(asm_foyan_knaouie8)
klno6phn_9faskf_na = which(asm_foyan_knaouie8);
end
Obviously MathWorks struggles with comparable problems.
The profiler shows, that matlab.codetools.requiredFilesAndProducts() calls 207 subfunctions (R2018b).
See the discussion: https://www.mathworks.com/matlabcentral/discussions/ideas/887763-i-have-been-a-matlab-loyalist-for-25-years . The complexity explodes, such that a simple task like searching a function file needs a pile of exceptions and indirections. What is a safe method to check, where a specific function exists in Matlab’s path? It should not matter, if the function is an M, P or MEX file.
Actually, this is a job for which() . To avoid collisions with local variables, it should be hidden in a function:
function Reply = safeWhich(varargin)
Reply = which(varargin{:});
end
This fails for ‘varargin’, as expected:
safeWhich(‘varargin’)
But there are further traps
safeWhich(‘R’) % Same for ‘P’
In Matlab R2018b under Windows I get:
‘C:Program FilesMATLABR2018btoolboxmatlabcodetools@mtreemtree.m % mtree method’
The appended comment allowed to exclude the output by checking, if a corrensponding file exists:
function Reply = safeWhich_2(varargin)
Reply = which(varargin{:});
Reply = cellstr(Reply);
Reply = Reply(isfile(Reply)); % Worked in R2018b, no effect in R2025a
end
safeWhich_2(‘R’)
The exist() command does have the power to find functions properly, but it does not reveal where.
The old function depfun() was replaced by the ugly matlab.codetools.requiredFilesAndProducts(). I could search in the corresponding code, couldn’t I? This function uses the code find here internally: "toolboxmatlabdepfun+matlab+depfun+internal+whichcallWhich.m". Here I find 91 functions and 11.3 MB of code. One function looks like this:
% 10^(-18) is effective zero in terms of possibility, which requries
% log(10^18)/log(26+10+1) = 12 random letters, digits, or underscores.
function klno6phn_9faskf_na = callWhich(asm_foyan_knaouie8)
klno6phn_9faskf_na = which(asm_foyan_knaouie8);
end
Obviously MathWorks struggles with comparable problems.
The profiler shows, that matlab.codetools.requiredFilesAndProducts() calls 207 subfunctions (R2018b).
See the discussion: https://www.mathworks.com/matlabcentral/discussions/ideas/887763-i-have-been-a-matlab-loyalist-for-25-years . The complexity explodes, such that a simple task like searching a function file needs a pile of exceptions and indirections. which, exist, required products, file MATLAB Answers — New Questions









