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