How to identify such functions as addpath that are coming with MATLAB?

64 views (last 30 days)
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
Stephen23
Stephen23 on 1 Dec 2024 at 19:16
"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.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 2 Dec 2024 at 1:10
@Henning Søgaard and @Paul, try this:
% Demo by Image Analyst to notify user if one of their m-files
% is also a built-in function.
% Initialization steps.
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
clearvars; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
% Specify the top-level folder where the m-files live.
myFolder = pwd; % or 'C:\Users\yourUserName\Documents\whatever you want\';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', myFolder);
uiwait(warndlg(errorMessage));
myFolder = uigetdir(); % Ask for a new one.
if myFolder == 0
% User clicked Cancel
return;
end
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '**\*.m'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
shadowCount = 0;
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now checking %s\n', fullFileName);
s = which(fullFileName);
% See if s contains a string that indicates it's a built-in function.
if contains(s, 'built-in', IgnoreCase=true) || ...
contains(s, 'C:\Program Files\MATLAB\R2024b\toolbox\matlab', IgnoreCase=true)
fprintf('WARNING: That is also a built-in function!\n')
shadowCount = shadowCount + 1;
end
end
fprintf('Found %d shadowed functions.\n', shadowCount);

More Answers (2)

John D'Errico
John D'Errico on 1 Dec 2024 at 14:10
Edited: John D'Errico on 1 Dec 2024 at 14:53
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
Henning Søgaard on 1 Dec 2024 at 16:24
Edited: Henning Søgaard on 1 Dec 2024 at 16:43
Thank you very much for your helpful advice. I have always followed your Rule #1, as well as Rule #2. Since I began programming in MATLAB in 1984, I have never broken the rules.
I apologize if my question gave the impression that I store my own files in directories provided by MathWorks. As a non-native English speaker, I struggled somewhat with formulating the question as clearly as possible. I’m sorry if I failed to express myself properly.

Sign in to comment.


Image Analyst
Image Analyst on 1 Dec 2024 at 14:54
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
Henning Søgaard on 1 Dec 2024 at 16:34
If I am trying to find out what functions are called in my code so I can determine what toolboxes are needed, and which of my m-files are needed, in order to share the code with someone else
Yes, I am trying to find out what functions are called in my code. However, the simple purpose is to make a list of all built-in functions that are called. I will share the list with my students so that they will know what MATLAB functions I expect them to be familiar with.
Thanks you for your suggested code.
Paul
Paul on 1 Dec 2024 at 18:54
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
flist = 0x0 empty cell array
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"))
ans = logical
1

Sign in to comment.

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!