删除matlab中的路径和子路径

时间:2014-03-27 14:05:36

标签: windows matlab svn path

我一直在寻找一种从matlab中删除一堆路径的简单方法。我正在使用一个相当大的程序,它在其目录中包含许多路径。我也使用svn版本处理,我使用了很多分支,它们通常包含一些相同的功能,一些被修改,一些只存在于一个分支上。

问题在于,当我为一个分支设置路径(使用自定义函数),然后想要将目录更改为另一个路径时,第一部分很难删除。我用过

rmpath(path1,path2,...);

但是,这需要手动输入每条路径。因为我想知道所有路径都有一个共同的基本目录,无论如何使用通配符从路径中删除整个目录?我使用的是Windows机器。

4 个答案:

答案 0 :(得分:11)

尝试使用genpath。给定基目录作为输入,genpath返回基目录加上所有子目录,递归。

rmpath(genpath(base_directory));

答案 1 :(得分:1)

没有通配符支持。您可以编写自己的Matlab函数来添加和删除项目中的所有路径,或者支持正则表达式匹配。创建项目本身的这一部分很方便,因此它可以知道需要添加或删除的所有目录,并在必要时执行其他库初始化工作。

答案 2 :(得分:1)

genpath答案适用于fragment*个案例,但不适用于*fragment*个案例。

Clunky,但有效:

pathlist = path;
pathArray = strsplit(pathlist,';');
numPaths = numel(pathArray);
for n = 1:numPaths
    testPath = char(pathArray(n))
    isMatching = strfind(testPath,'pathFragment')
    if isMatching
        rmpath(testPath);
    end
end

答案 3 :(得分:1)

我的答案较短:

function rmpathseb(directory)

% Retrieve the subfolders
folders = dir(directory);

% Remove folders one by one
% The first two are '.' and '..'
for i = 3 : length(folders);

    rmpath([pwd , '\' , directory , '\' , folders(i).name]);

end

end