How do I check if there is a function with the same name
13 ビュー (過去 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.
.
4 件のコメント
Star Strider
2024 年 6 月 4 日
It will detect all of them with the -all flag.
which mldivide -all
.
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!