How do I check if there is a function with the same name
15 views (last 30 days)
Show older comments
In addition to the functions that come with matlab, I have added some additional toolboxes, so I want to make sure that there are more than one function with the same name in my directory, who can teach me.
0 Comments
Answers (2)
Star Strider
on 4 Jun 2024
which which -all
If there are any others in your MATLAB search path, it should display them as well.
.
4 Comments
Star Strider
on 4 Jun 2024
It will detect all of them with the -all flag.
which mldivide -all
.
Voss
on 4 Jun 2024
You can loop over the m-files in your folder, call which(_,'-all') on each one, and store information about what which returned:
F = dir('*.m');
for ii = 1:numel(F)
C = which(F(ii).name,'-all');
F(ii).instances = C;
F(ii).is_duplicate = numel(C) > 1;
end
Then the files that have same-name duplicates somewhere on the path are
D = F([F.is_duplicate]);
and their same-name duplicate locations are given by
D.instances
Example:
% create some folders with m-files
% Folder1 contains file1.m and file4.m
mkdir('Folder1')
fid = fopen(fullfile('Folder1','file1.m'),'w'); fclose(fid);
fid = fopen(fullfile('Folder1','file4.m'),'w'); fclose(fid);
% Folder2 contains file1.m and file2.m
mkdir('Folder2')
fid = fopen(fullfile('Folder2','file1.m'),'w'); fclose(fid);
fid = fopen(fullfile('Folder2','file2.m'),'w'); fclose(fid);
% add the folders to the path
addpath('Folder1','Folder2')
% the current folder contains file1.m, file2.m, and file3.m
fid = fopen('file1.m','w'); fclose(fid);
fid = fopen('file2.m','w'); fclose(fid);
fid = fopen('file3.m','w'); fclose(fid);
% run the code above
F = dir('*.m');
for ii = 1:numel(F)
C = which(F(ii).name,'-all');
F(ii).instances = C;
F(ii).is_duplicate = numel(C) > 1;
end
% F contains info about the files in the current folder
F
F.name
% D contains info about those files that have same-name duplicates
% somewhere on the path
D = F([F.is_duplicate])
D.name
% D.instances tells you where the duplicates are
D.instances
0 Comments
See Also
Categories
Find more on Whos 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!