How do I check if there is a function with the same name
조회 수: 26 (최근 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!