How do I check if there is a function with the same name

13 ビュー (過去 30 日間)
中祥 范
中祥 范 2024 年 6 月 4 日
回答済み: Voss 2024 年 6 月 4 日
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.

回答 (2 件)

Star Strider
Star Strider 2024 年 6 月 4 日
The which function can be helpful.
which which -all
built-in (/MATLAB/toolbox/matlab/general/which)
If there are any others in your MATLAB search path, it should display them as well.
.
  4 件のコメント
中祥 范
中祥 范 2024 年 6 月 4 日
Thank you for your answer, but what I want to do is check if there are any functions with the same name in my path.
The 'which' function seems to only check one by one.
Star Strider
Star Strider 2024 年 6 月 4 日
It will detect all of them with the -all flag.
For example, the extremely useful matrix-left-divide (the \ operator) called mldivide, \
which mldivide -all
built-in (/MATLAB/toolbox/matlab/ops/mldivide) built-in (/MATLAB/toolbox/matlab/ops/@double/mldivide) % double method built-in (/MATLAB/toolbox/matlab/ops/@int16/mldivide) % int16 method built-in (/MATLAB/toolbox/matlab/ops/@int32/mldivide) % int32 method built-in (/MATLAB/toolbox/matlab/ops/@int64/mldivide) % int64 method built-in (/MATLAB/toolbox/matlab/ops/@int8/mldivide) % int8 method built-in (/MATLAB/toolbox/matlab/ops/@logical/mldivide) % logical method built-in (/MATLAB/toolbox/matlab/ops/@single/mldivide) % single method built-in (/MATLAB/toolbox/matlab/ops/@uint16/mldivide) % uint16 method built-in (/MATLAB/toolbox/matlab/ops/@uint32/mldivide) % uint32 method built-in (/MATLAB/toolbox/matlab/ops/@uint64/mldivide) % uint64 method built-in (/MATLAB/toolbox/matlab/ops/@uint8/mldivide) % uint8 method /MATLAB/toolbox/matlab/datatypes/duration/@duration/mldivide.m % duration method /MATLAB/toolbox/matlab/timeseries/@timeseries/mldivide.m % timeseries method mldivide is a built-in method % matlab.internal.capability.Capability method mldivide is a built-in method % connector.internal.LoggerLevel method mldivide is a built-in method % matlab.lang.OnOffSwitchState method /MATLAB/toolbox/matlab/timeseries/+tsdata/@datametadata/mldivide.m % tsdata.datametadata method mldivide is a built-in method % matlab.internal.reference.property.RefEntityType method mldivide is a built-in method % matlab.internal.reference.api.EntityPrecision method mldivide is a built-in method % matlab.internal.reference.property.DeprecationStatus method mldivide is a built-in method % matlab.internal.reference.property.FunctionType method mldivide is a built-in method % matlab.internal.reference.property.SyntaxType method mldivide is a built-in method % matlab.internal.reference.api.EntityCaseSensitivity method mldivide is a built-in method % simulink.FindSystemTask.Status method mldivide is a built-in method % mf.zero.meta.Language method mldivide is a built-in method % dig.config.CommandType method mldivide is a built-in method % dig.config.HorizontalAlignment method mldivide is a built-in method % dig.config.ResourceState method mldivide is a built-in method % dig.config.ResourceType method mldivide is a built-in method % dig.model.DisplayState method mldivide is a built-in method % dig.model.EventDataType method mldivide is a built-in method % dig.model.FunctionType method mldivide is a built-in method % dig.model.ValidInBdType method mldivide is a built-in method % dig.model.ViewMode method mldivide is a built-in method % matlab.unittest.internal.fixtures.FolderScope method mldivide is a built-in method % matlab.automation.Verbosity method mldivide is a built-in method % matlab.internal.timer.CallBackTypeEnum method mldivide is a built-in method % matlab.internal.timer.BusyModeEnum method mldivide is a built-in method % matlab.internal.timer.ExecutionModeEnum method /MATLAB/toolbox/matlab/bigdata/@tall/mldivide.m % tall method built-in % gpuArray method /MATLAB/toolbox/econ/econ/@LagOp/mldivide.m % LagOp method /MATLAB/toolbox/parallel/array/distributed/@distributed/mldivide.m % distributed method /MATLAB/toolbox/parallel/array/distributed/@codistributed/mldivide.m % codistributed method /MATLAB/toolbox/parallel/gpu/gpu/@gpuArray/mldivide.m % gpuArray method /MATLAB/toolbox/shared/controllib/engine/@StaticModel/mldivide.m % StaticModel method /MATLAB/toolbox/shared/controllib/engine/@DynamicSystem/mldivide.m % DynamicSystem method /MATLAB/toolbox/wavelet/wavelet/@laurpoly/mldivide.m % laurpoly method
.

サインインしてコメントする。


Voss
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 = 3x1 struct array with fields:
name folder date bytes isdir datenum instances is_duplicate
F.name
ans = 'file1.m'
ans = 'file2.m'
ans = 'file3.m'
% D contains info about those files that have same-name duplicates
% somewhere on the path
D = F([F.is_duplicate])
D = 2x1 struct array with fields:
name folder date bytes isdir datenum instances is_duplicate
D.name
ans = 'file1.m'
ans = 'file2.m'
% D.instances tells you where the duplicates are
D.instances
ans = 3x1 cell array
{'/users/mss.system.v6zxt/file1.m' } {'/users/mss.system.v6zxt/Folder1/file1.m'} {'/users/mss.system.v6zxt/Folder2/file1.m'}
ans = 2x1 cell array
{'/users/mss.system.v6zxt/file2.m' } {'/users/mss.system.v6zxt/Folder2/file2.m'}

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

タグ

製品


リリース

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by