Find duplicates in Matlab path

Duplicates in the matlab path is a problem because you cannot control which one is running. The first step for processing duplicates is to find them. How to find duplicate .m files in my matlab path?

+3
source share
2 answers

Well, that in itself is not a Herculean task. We just have to list all the .m files in the path and find several occurrences of the same file. We can use a combination of functions path, whatand unique.

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

Instead of processing the full Matlab path, you can limit the search for duplicates in one own folder. To do this, just uncomment the third line and replace the directory name of your choice.

+3
source

(), .

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

, . ( , strsplit):

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
0

All Articles