How to identify such functions as addpath that are coming with MATLAB?
Show older comments
If I want to test whether a given function name corresponds to one of MATLAB's built-in functions, I usually use the exist function. For example, calling exist('sin') returns 5, indicating that sin is a built-in MATLAB function. However, the function addpath, which also comes with MATLAB, returns 2 when checked with exist('addpath'). This is the same value returned for user-defined functions.
Is there a way to reliably identify that functions like addpath are part of MATLAB's standard functions, distinguishing them from user-defined functions?
5 Comments
Steven Lord
on 1 Dec 2024
Why do you want to perform this test? How/why is your code going to behave differently if the function is a built-in function, a function file included as part of MATLAB, or a function file written by someone other than MathWorks?
Henning Søgaard
on 1 Dec 2024
Edited: Henning Søgaard
on 1 Dec 2024
Hi Henning,
Keep in mind that a "built-in" function will not be implemented in an m-function file. Because addpath is implemented in an m-function
which -all addpath
exist returns 2, not 5
exist('addpath')
w = which('sin')
If you already have a list of all of the functions that your code calls, then maybe one way to distinguish MathWorks-provided functions is check that for /MATLAB/ (or whatever it is for your system) in the path to the function that is returned by which. Of course, this won't work if you have your user-defined functions in a folder with /MATLAB/ in the folder name.
However, there may be other complications. For example, suppose you have have your list of function calls as you've found from searching for "<MATLAB-name>(", (which I guess is somehow distinguishable from an array indexing operation). What happens if you've found a function that is user-defined and also has the same name as a MathWorks-provided function? Maybe you've defined your own sim() function. If all you know is that the code calls a function that starts with "sim(", how would you know if the code is calling yours or any of these
which -all sim
or an anonymous function named sim?
Stephen23
on 1 Dec 2024
"Is there a way to reliably identify that functions like addpath are part of MATLAB's standard functions, distinguishing them from user-defined functions?"
Their path.
Henning Søgaard
on 4 Dec 2024
Edited: Henning Søgaard
on 4 Dec 2024
Accepted Answer
More Answers (2)
John D'Errico
on 1 Dec 2024
Edited: John D'Errico
on 1 Dec 2024
0 votes
Follow two rules when you are writing or providing functions to run in MATLAB.
Rule # 1: NEVER put your own functions in the MathWorks supplied directories.
Rule #2: See Rule #1.
There are good reasons to follow these rules, by the way. For example, code placed in those directories is treated differently by MATLAB, in terms of how it is cached at startup. You don't want to put your code there, because if you change it, you won't see the changes. And then you will anxiously post a question on Answers asking why MATLAB does not see the changes you made to you code. (That happens oh so frequently.)
If you follow those directions, then you will never have any question about what was supplied to you by MathWorks, and what you provided. If it is in your directories, it got there only because you wrote it, or because you downloaded it from someone else.
Why this matters to you is not clear. If you wrote code you don't trust to be correct, then why are you using it? If you do trust it, then why does it matter?
So all you need to do now is look at which directory the function lives in. The directory tells you everything, and no test is needed.
I suppose there is a circumstance where you (foolishly/incorrectly) put your own functions in the supplied directories, and now you want to know which functions you put there? Again, that was just a bad idea in the first place. But you could look to see which functions have MathWorks copyright protection lines in them. And then? REMOVE THEM!
1 Comment
Henning Søgaard
on 1 Dec 2024
Edited: Henning Søgaard
on 1 Dec 2024
Image Analyst
on 1 Dec 2024
Are you trying to find out what functions are called in your code so you can determine what toolboxes are needed, and which of your m-files are needed, in order to share the code with someone else? If so, I don't think you need to do what you're trying to do (find all functions and check if they're built-in or not). You can call a special function to list the dependencies (matlab.codetools.requiredFilesAndProducts). See my wrapper function for it:
% List required files and toolboxes. Displays them in the command window or console window (if deployed).
% Sample call
% fullFileName = [mfilename('fullpath'), '.m'];
% DisplayRequiredFunctions(fullFileName)
% It takes a long time to run so that's why I only do it in the development environment.
function DisplayRequiredFunctions(fullFileName)
try
if ~isdeployed
[~, baseFileNameNoExt, ext] = fileparts(fullFileName);
baseFileName = [baseFileNameNoExt, '.m'];
[requiredFileList, toolboxList] = matlab.codetools.requiredFilesAndProducts(fullFileName);
fprintf('Required m-files for %s:\n', baseFileName);
for k = 1 : length(requiredFileList)
fprintf(' %s\n', requiredFileList{k});
end
fprintf('Required MATLAB Toolboxes for %s:\n', baseFileName);
for k = 1 : length(toolboxList)
fprintf(' %s\n', toolboxList(k).Name);
end
end
catch ME
end
2 Comments
Henning Søgaard
on 1 Dec 2024
From what I understand, in this context the term "buit-in" functions really means any function provided by MathWorks, both built-in and those implemented as m-functions.
However, matlab.codetools.requiredFilesAndProducts only returns the latter, not the former.
[flist,plist]=matlab.codetools.requiredFilesAndProducts('pzplot.m');
flist
So pzplot.m doesn't call any other m-function, but it certainly does call other built-in functions, such as numel
s = readlines('pzplot.m');
any(contains(s,"numel"))
Categories
Find more on Search Path 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!