單目錄搜索的情況下
%%// 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
與子目錄搜索的情況下沿着主目錄中添加選項
對於基本路徑非常久遠分目錄搜索,下面的代碼會嘗試找到每個子目錄的匹配項以及basepath並添加缺失的項目。因此,即使您已選擇性地刪除路徑中的任何子目錄或基路徑,此代碼也會照顧添加路徑中缺少的所有內容。
%%// 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
在您可以使用的子目錄之一中是否有m文件? 'exist'對於文件夾是相同的,但是如果在那裏有一個函數,你可以檢查函數是否在你的路徑上。 – Raab70
不,沒有.m文件。它的一個文件夾 - > 50個文件夾 - > 2000個文件夾中的每個文件夾。下面給出的答案完美。 –