Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 如何检查文件夹是否位于搜索路径上_Matlab_Path - Fatal编程技术网

Matlab 如何检查文件夹是否位于搜索路径上

Matlab 如何检查文件夹是否位于搜索路径上,matlab,path,Matlab,Path,我在外置驱动器上有一个文件夹,有50多个文件夹,每个文件夹有2000多个文件。50个文件夹中的每个文件夹都没有子文件夹。我想在MATLAB搜索路径上添加所有文件,因此 我执行了addpath(genpath(…)。大约需要5分钟。如果文件夹位于搜索路径上,我不想再次重复该操作。我如何确定这一点 我知道我可以使用which测试文件是否在搜索路径上,但我想看看主文件夹(有50个子文件夹)和子文件夹是否在搜索路径上。我该怎么做 我甚至尝试过使用exist命令,但即使文件夹不在搜索路径上,它也会给我非零

我在外置驱动器上有一个文件夹,有50多个文件夹,每个文件夹有2000多个文件。50个文件夹中的每个文件夹都没有子文件夹。我想在MATLAB搜索路径上添加所有文件,因此 我执行了
addpath(genpath(…)
。大约需要5分钟。如果文件夹位于搜索路径上,我不想再次重复该操作。我如何确定这一点

我知道我可以使用
which
测试文件是否在搜索路径上,但我想看看主文件夹(有50个子文件夹)和子文件夹是否在搜索路径上。我该怎么做


我甚至尝试过使用
exist
命令,但即使文件夹不在搜索路径上,它也会给我非零值。

单目录搜索案例

%%// 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,下面的代码将尝试查找每个子目录的匹配项,以及缺少的basepath和add。因此,即使您有选择地从路径中删除了任何子目录,甚至是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

单一目录搜索案例

%%// 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,下面的代码将尝试查找每个子目录的匹配项,以及缺少的basepath和add。因此,即使您有选择地从路径中删除了任何子目录,甚至是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

老问题,但这里有另一种使用单元格数组的方法,通过拆分
path()
字符串。对于大量文件夹,不确定它是否比
regexp
方法慢/快,但我发现它在概念上更简单

首先,创建一个用于检查单个路径的函数。函数使用
p_array=strsplit(path(),pathsep)
创建单元格数组,然后
any(strcmp(p\u数组,文件夹\u to\u search\u for))
检查您要查找的文件夹是否在单元格数组中。它将只匹配完整的字符串

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
因此,要检查存储在
top\u level\u dir
中的顶级目录的所有子目录,并添加缺少的子目录:

% 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 

老问题,但这里有另一种使用单元格数组的方法,通过拆分
path()
字符串。对于大量文件夹,不确定它是否比
regexp
方法慢/快,但我发现它在概念上更简单

首先,创建一个用于检查单个路径的函数。函数使用
p_array=strsplit(path(),pathsep)
创建单元格数组,然后
any(strcmp(p\u数组,文件夹\u to\u search\u for))
检查您要查找的文件夹是否在单元格数组中。它将只匹配完整的字符串

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
因此,要检查存储在
top\u level\u dir
中的顶级目录的所有子目录,并添加缺少的子目录:

% 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 

在您可以使用的子目录中是否有m文件<代码>存在对于文件夹是相同的,但是如果其中有一个函数,您可以检查该函数是否在您的路径上。没有.m文件。它的一个文件夹->50个文件夹->每50个文件夹中有2000个文件。下面给出的答案非常有效。您可以使用其中一个子目录中的m文件吗<代码>存在对于文件夹是相同的,但是如果其中有一个函数,您可以检查该函数是否在您的路径上。没有.m文件。它的一个文件夹->50个文件夹->每50个文件夹中有2000个文件。下面给出的答案非常有效。谢谢你的代码。我正在通过编辑您的代码添加Windows版本。如果你觉得合适就留着吧。另外,我建议您修改代码。代替
any(ismember(…)
,您可以反转输入参数并将其写入
(ismember(path\u to\u be\u search,path\u list\u cell))
。这样,您将获得与第一个参数相同的长度数组。这使得这个过程更容易。我会等一小会儿接受你的回答。@ParagS.Chandakkar一些编辑-1。添加了
pathsep
,它必须解决Linux和Windows之间的冒号和分号问题。2.我已经添加了您的建议,使用
path\u to\u be\u search
作为
ismember
的第一个参数,它在这里非常适合。3.添加了
主目录和子目录搜索案例
,我认为这是您的主要目标,这也让您可以添加缺少的路径。希望这能解决你所有的麻烦!谢谢你的代码。我正在通过编辑您的代码添加Windows版本。如果你觉得合适就留着吧。另外,我建议您修改代码。代替
any(ismember(…)
,您可以反转输入参数并将其写入
(ismember(path\u to\u be\u search,path\u list\u cell))
。这样,您将获得与第一个参数相同的长度数组。这使得这个过程更容易。我会等一小会儿接受你的回答。@ParagS.Chandakkar一些编辑-1。添加了
pathsep
,它必须解决Linux和Windows之间的冒号和分号问题。2.我已经添加了您的建议,使用
path\u to\u be\u search
作为
ismember
的第一个参数,它在这里非常适合。3.添加了
主目录和子目录搜索案例
,我认为这是您的主要目标,这也让您可以添加缺少的路径。希望这能解决你所有的麻烦!