Reputation: 9075
I have a folder on the external drive which have 50+ folders and each folder has 2000+ files. Each of the 50 folders has no subfolder. I want to add all the files on the MATLAB search path and hence
I performed addpath(genpath(...))
. It takes around 5 minutes. I don't want to repeat the operation again if the folders are on the search path. How do I determine that?
I know I can test if a file is on the search path using which
, but I want to see if the main folder (which has 50 subfolders) and the subfolders are on the search path. How do I do that?
I have even tried using exist
command but it gives me non-zero value even if the folder is not on the search path.
Upvotes: 3
Views: 4353
Reputation: 1322
Old question, but here's another way using cell arrays by splitting the path()
string. Not sure if it's slower/faster than the regexp
approach for a large number of folders, but I find it conceptually simpler.
First, create a function for checking a single path. The function uses p_array=strsplit(path(),pathsep);
to create the cell array, and then any(strcmp(p_array,folder_to_search_for))
to check if the folder you're looking for is in the cell array. It will only match full strings.
function folder_in_path=checkPath(folder_to_search_for)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% folder_in_path=checkPath(folder_to_search_for)
%
% Input:
% folder_to_search_for string, the folder to check
%
% Output:
% folder_in_path 1 if in path, 0 if not in path
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% create a cell array with each folder as a cell
p_array=strsplit(path(),pathsep);
% search the cell array for your folder_to_search_for
if any(strcmp(p_array,folder_to_search_for))
folder_in_path=1;
else
folder_in_path=0;
end
end
So to check all the subdirectories of a top level directory stored in top_level_dir
and add the missing subdirectories:
% generate cell array of all the subdirs in top_level_dir:
allthesubdirs=strsplit(genpath(top_level_dir),pathsep);
% check each one and add to path if not there
for i_fo=1:numel(allthesubdirs)
if ~checkPath(allthesubdirs{i_fo})
disp(['adding ',allthesubdirs{i_fo},' to the path'])
addpath(allthesubdirs{i_fo})
else
disp([allthesubdirs{i_fo},' is already in the path'])
end
end
Upvotes: 0
Reputation: 221614
Single directory search case
%%// path_to_be_searched is the folder or directory to be detected
%%// to be in path or not
%%// colon is the separator used for paths under Linux.
%%// For Windows and others, it needs to be investigated.
path_list_cell = regexp(path,pathsep,'Split')
if any(ismember(path_to_be_searched,path_list_cell))
disp('Yes, this directory is in MATLAB path');
else
disp('No, this directory is not in MATLAB path');
end
Main directory along with sub-directories search case with adding option
For a basepath alongwith sub-directory search, the following code would try to find match for each sub-directory and also the basepath and add which ever is missing. So even if you have selectively removed any sub-directory or even the basepath from the path, this code would take care of adding everything that's missing from the path.
%%// basepath1 is the path to the main directory with sub-directories that
%%// are to detected for presence
basepath_to_be_searched = genpath(basepath1)
basepath_list_cell = regexp(basepath_to_be_searched,pathsep,'Split')
%%// Remove empty cells
basepath_list_cell = basepath_list_cell(~cellfun(@isempty,basepath_list_cell))
path_list_cell = regexp(path,pathsep,'Split');
ind1 = ismember(basepath_list_cell,path_list_cell)
%%// Add the missing paths
addpath(strjoin(strcat(basepath_list_cell(~ind1),pathsep),''))
%%// strjoin is a recent MATLAB addition and is also available on file-exchange -
%%// http://www.mathworks.in/matlabcentral/fileexchange/31862-strjoin
Upvotes: 4