How to identify such functions as addpath that are coming with MATLAB?
69 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Henning Søgaard
le 1 Déc 2024 à 12:24
Commenté : Henning Søgaard
le 4 Déc 2024 à 7:00
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 commentaires
Stephen23
le 1 Déc 2024 à 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.
Réponse acceptée
Image Analyst
le 2 Déc 2024 à 1:10
% 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);
Plus de réponses (2)
John D'Errico
le 1 Déc 2024 à 14:10
Modifié(e) : John D'Errico
le 1 Déc 2024 à 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 commentaire
Image Analyst
le 1 Déc 2024 à 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 commentaires
Paul
le 1 Déc 2024 à 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
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"))
Voir également
Catégories
En savoir plus sur Search Path dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!