How to find a function in the path - securely?
Show older comments
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 Files\MATLAB\R2018b\toolbox\matlab\codetools\@mtree\mtree.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: "toolbox\matlab\depfun\+matlab\+depfun\+internal\+which\callWhich.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.
1 Comment
Dan Dolan
on 22 Apr 2026 at 16:42
I also miss the simplicity of depfun...
Answers (2)
The complexity explodes, such that a simple task like searching a function file needs a pile of exceptions and indirections.
Sometimes a question that's simple to ask is not so simple to answer. Consider "For any integer n > 2, the equation a^n + b^n = c^n has no positive integer solutions." Simple enough mathematical statement, right? Fermat's Last Theorem was first stated in about 1637. In 1994-95 (over 350 years later) Andrew Wiles proved it to be true.
In simple cases, "Where is the function named X defined?" is straightforward to answer. But the devil is in the details. Some of those details are:
methods
Where is the function named plot defined? If you asked me that I'd ask you "Which plot function?" There are dozens of overloads for various classes.
d = which('-all', 'plot');
numberOfDefinitions = numel(d)
That's one reason which can accept not just a function name but also the text of a function call.
f = figure(Visible='off');
ax = axes(Parent=f, Visible='off');
g = graph(bucky);
which('plot(ax, g)') % Overloaded graph object method, compared to
which('plot(ax, 1:10, 1:10)') % version for numeric data
scoping
The specific directory you're in (or sometimes which file you're running) can affect what function of a specific name gets called (private folders, nested functions, local functions, methods with various Access attributes, etc.)
import and class loading
Other code that's been called prior to the command asking the question can change the answer (calling import to import a function from a namespace, loading a class definition to make its methods visible to MATLAB, etc.) In fact, based on the documentation page linked below, the exact syntax with which you call import to import a namespace function or class (explicitly specified or with a wildcard) determines where it falls in the precedence order!
Determining which function gets called in a certain situation is complicated enough there's a whole documentation page that gets into (some of) the details.
"Where is the function named X?" is not as complicated as Fermat's Last Theorem to answer, but it's not as straightforward as it seems at first glance either.
2 Comments
Complicating things:
It is not just which file you are in: it is also which function you are in within the file.
try_it()
function try_it
inner1()
inner2()
function inner1
plot()
function plot()
disp('inner1::plot')
end
end
function inner2
plot()
function plot()
disp('inner2::plot')
end
end
end
Perhaps as follows?
safeWhich('num2cell')
safeWhich('varargin')
safeWhich('R')
safeWhich('p')
safeWhich('queryName')
safeWhich('tmp')
safeWhich('queryName2')
safeWhich('Reply')
function Reply = safeWhich(queryName)
tmp=which(queryName);
if ~strcmp(tmp,"variable")
[~,p]=fileparts(tmp);
if ~strcmp(p,queryName), Reply=''; return; end
Reply=tmp;
return
end
queryName2=queryName; clear queryName tmp
tmp2=which(queryName2);
[~,p]=fileparts(tmp2);
if ~strcmp(p,queryName2), Reply=''; return; end
Reply=tmp2;
end
2 Comments
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!