Is there a way to make MATLAB remind a developer or warn a user that several conflicting (same name) versions of specific function m-file exists in different directories currently present in the path
? This would be useful for in large collaborative MATLAB projects.
If you have a specific function name you need to check to see if any functions with that name already exist, you can use the function WHICH. For example, if I add a new function file test.m
to my current directory (C:\Program Files\MATLAB\R2010b\bin\
), then this is what WHICH tells me:
>> which test -all
C:\Program Files\MATLAB\R2010b\bin\test.m
C:\Program Files\MATLAB\R2010b\toolbox\stats\stats\@classregtree\test.m % classregtree method
C:\Users\user\Documents\MATLAB\test.m % Shadowed
There are now three functions called test
: one is a class method for classregtree
objects, one is the function I just made (top line), and the last (bottom line) is now shadowed by the one I just made. This would mean that, if I called test
from my current directory, the first one would be called. If I change to another directory, I get this instead:
>> which test -all
C:\Users\user\Documents\MATLAB\test.m
C:\Program Files\MATLAB\R2010b\toolbox\stats\stats\@classregtree\test.m % classregtree method
The newer test
function doesn't show up now, since the previous directory wasn't saved on the MATLAB path. The previously shadowed test
function would now be the one called.
If you would like to check for all possible conflicts within your path, you can use this function from File Exchange.
The example usage:
>> [~, collisions] = name_collisions;
Found the following duplicate names:
25 x Contents.m
2 x complex2interleaved.m
2 x demosinit.m
2 x exported_values.mat
2 x gcGuiReport.mat
2 x header.m
2 x interleaved2complex.m
2 x ltfatdiditfail.m
2 x mexinit.m
2 x octinit.m
2 x ref_spreadadj_1.m
2 x signalsinit.m
2 x startup.m
2 x testinginit.m
It also returns a structure with paths to the colliding files.
>> disp(collisions)
51×1 struct array with fields:
name
folder
date
bytes
isdir
datenum