37 views (last 30 days)
Show older comments
中祥 范 on 4 Jun 2024 at 14:23
-
-
Link
Direct link to this question
https://www-ah.mathworks.com/matlabcentral/answers/2125381-how-do-i-check-if-there-is-a-function-with-the-same-name
Answered: Voss on 4 Jun 2024 at 15:34
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 Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
Answers (2)
Star Strider on 4 Jun 2024 at 14:28
Open in MATLAB Online
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 Comments Show 2 older commentsHide 2 older comments
Show 2 older commentsHide 2 older comments
John D'Errico on 4 Jun 2024 at 14:31
Direct link to this comment
https://www-ah.mathworks.com/matlabcentral/answers/2125381-how-do-i-check-if-there-is-a-function-with-the-same-name#comment_3179216
Edited: John D'Errico on 4 Jun 2024 at 14:32
Which is the first choice of course, but exist is another option.
Star Strider on 4 Jun 2024 at 14:36
Direct link to this comment
https://www-ah.mathworks.com/matlabcentral/answers/2125381-how-do-i-check-if-there-is-a-function-with-the-same-name#comment_3179221
@John D'Errico — Noted. Thank you.
中祥 范 on 4 Jun 2024 at 14:53
Direct link to this comment
https://www-ah.mathworks.com/matlabcentral/answers/2125381-how-do-i-check-if-there-is-a-function-with-the-same-name#comment_3179246
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 on 4 Jun 2024 at 15:09
Direct link to this comment
https://www-ah.mathworks.com/matlabcentral/answers/2125381-how-do-i-check-if-there-is-a-function-with-the-same-name#comment_3179276
⋮
Open in MATLAB Online
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 methodbuilt-in (/MATLAB/toolbox/matlab/ops/@int16/mldivide) % int16 methodbuilt-in (/MATLAB/toolbox/matlab/ops/@int32/mldivide) % int32 methodbuilt-in (/MATLAB/toolbox/matlab/ops/@int64/mldivide) % int64 methodbuilt-in (/MATLAB/toolbox/matlab/ops/@int8/mldivide) % int8 methodbuilt-in (/MATLAB/toolbox/matlab/ops/@logical/mldivide) % logical methodbuilt-in (/MATLAB/toolbox/matlab/ops/@single/mldivide) % single methodbuilt-in (/MATLAB/toolbox/matlab/ops/@uint16/mldivide) % uint16 methodbuilt-in (/MATLAB/toolbox/matlab/ops/@uint32/mldivide) % uint32 methodbuilt-in (/MATLAB/toolbox/matlab/ops/@uint64/mldivide) % uint64 methodbuilt-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 methodmldivide is a built-in method % matlab.internal.capability.Capability methodmldivide is a built-in method % connector.internal.LoggerLevel methodmldivide is a built-in method % matlab.lang.OnOffSwitchState method/MATLAB/toolbox/matlab/timeseries/+tsdata/@datametadata/mldivide.m % tsdata.datametadata methodmldivide is a built-in method % matlab.internal.reference.property.RefEntityType methodmldivide is a built-in method % matlab.internal.reference.api.EntityPrecision methodmldivide is a built-in method % matlab.internal.reference.property.DeprecationStatus methodmldivide is a built-in method % matlab.internal.reference.property.FunctionType methodmldivide is a built-in method % matlab.internal.reference.property.SyntaxType methodmldivide is a built-in method % matlab.internal.reference.api.EntityCaseSensitivity methodmldivide is a built-in method % simulink.FindSystemTask.Status methodmldivide is a built-in method % mf.zero.meta.Language methodmldivide is a built-in method % dig.config.CommandType methodmldivide is a built-in method % dig.config.HorizontalAlignment methodmldivide is a built-in method % dig.config.ResourceState methodmldivide is a built-in method % dig.config.ResourceType methodmldivide is a built-in method % dig.model.DisplayState methodmldivide is a built-in method % dig.model.EventDataType methodmldivide is a built-in method % dig.model.FunctionType methodmldivide is a built-in method % dig.model.ValidInBdType methodmldivide is a built-in method % dig.model.ViewMode methodmldivide is a built-in method % matlab.unittest.internal.fixtures.FolderScope methodmldivide is a built-in method % matlab.automation.Verbosity methodmldivide is a built-in method % matlab.internal.timer.CallBackTypeEnum methodmldivide is a built-in method % matlab.internal.timer.BusyModeEnum methodmldivide is a built-in method % matlab.internal.timer.ExecutionModeEnum method/MATLAB/toolbox/matlab/bigdata/@tall/mldivide.m % tall methodbuilt-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
.
Sign in to comment.
Voss on 4 Jun 2024 at 15:34
Open in MATLAB Online
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'}
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office