How do I check if there is a function with the same name
15 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
Star Strider
2024-6-4
which which -all
If there are any others in your MATLAB search path, it should display them as well.
.
Voss
2024-6-4
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!