Reputation: 121
Duplicates in the matlab path are a hassle, because you can not control which one gets executed. A first step to handle duplicates is to find them. How can I find duplicate .m files in my matlab path ?
Upvotes: 4
Views: 1583
Reputation: 121
Well, in itself it's not a herculean task. We juste have to list all .m files in the path and find multiple occurrences of the same file. We can use a mix of the path
, what
, and unique
functions.
function find_duplicate()
P=path;
P=strsplit(P, pathsep());
% mydir='/home/myusername/matlabdir';
% P=P(strncmpi(mydir,P,length(mydir)));
P=cellfun(@(x) what(x),P,'UniformOutput',false);
P=vertcat(P{:});
Q=arrayfun(@(x) x.m,P,'UniformOutput',false); % Q is a cell of cells of strings
Q=vertcat(Q{:});
R=arrayfun(@(x) repmat({x.path},size(x.m)),P,'UniformOutput',false); % R is a cell of cell of strings
R=vertcat(R{:});
[C,ia,ic]=unique(Q);
for c=1:numel(C)
ind=strcmpi(C{c},Q);
if sum(ind)>1
fprintf('duplicate %s at paths\n\t',C{c});
fprintf('%s\n\t',R{ind});
fprintf('\n');
end
end
end
Rather than handling the complete Matlab path, one can restrict the search for duplicates to one's own folder. To do that, just uncomment the third line and replace the directory name by one of your choice.
Upvotes: 6
Reputation: 112759
To analyze a given folder (recursively), you can proceed as follows.
folder = 'C:\Users\Luis\Desktop'; %// folder to be analyzed
[ success files id ] = fileattrib(['.' filesep '*']); %// this is recursive
[fullNames{1:numel(files)}] = deal(files.Name);
isMFile = cellfun(@(s) all(s(end-1:end)=='.m'), fullNames);
fullNames = fullNames(isMFile); %// keep only m-files
F = numel(fullNames);
start = cellfun(@(s) find(s==filesep,1,'last'), fullNames);
names = arrayfun(@(k) fullNames{k}(start(k)+1:end), 1:F, 'uni', 0); %// file name
[ii jj] = ndgrid(1:F); %// generate all pairs
equal = arrayfun(@(n) strcmp(names{ii(n)},names{jj(n)}), 1:F^2); %// test each
%// pair of files
equal = reshape(equal,F,F) - eye(F); %// equality with oneself doesn't count
isDuplicate = any(equal); %// it is a duplicate if it has some equal file
duplicates = fullNames(isDuplicate); %// cell array with full names of duplicates
To test the whole path, use the above code in a loop over all folders in the path. You can do it along the following lines (I haven't tested it, as I don't have the strsplit
function):
p = path;
p = strsplit(p,';');
duplicates = {};
for kk = numel(p)
folder = p{kk};
[ success files id ] = fileattrib(['.' filesep '*']);
[fullNames{1:numel(files)}] = deal(files.Name);
isMFile = cellfun(@(s) all(s(end-1:end)=='.m'), fullNames);
fullNames = fullNames(isMFile);
F = numel(fullNames);
start = cellfun(@(s) find(s==filesep,1,'last'), fullNames);
names = arrayfun(@(k) fullNames{k}(start(k)+1:end), 1:F, 'uni', 0);
[ii jj] = ndgrid(1:F);
equal = arrayfun(@(n) strcmp(names{ii(n)},names{jj(n)}), 1:F^2);
equal = reshape(equal,F,F) - eye(F);
isDuplicate = any(equal);
duplicates = {duplicates, fullNames(isDuplicate)}; %// add previous ones
end
Upvotes: 0