7

I have been looking for a simple way to remove a bunch of paths from matlab. I am working with a fairly large program and it includes many paths in its directory. I also work with svn version handling and I use many branches, which in general contains some functions that are the same, some that are modified and some that only exists on one branch.

The problem is that when I set the path for one branch (using custom function) and then want to change directory to another path, the first part is annoying to remove. I have used

rmpath(path1,path2,...);

However, this requires typing each path by hand. Since all paths have a common base directory I wonder, are there anyway to use wild cards to remove the complete directory from the path? I use a windows machine.

patrik
  • 4,506
  • 6
  • 24
  • 48

4 Answers4

13

Try using genpath. Given the base directory as input, genpath returns that base directory plus all subdirectories, recursive.

rmpath(genpath(base_directory));
Steve Osborne
  • 680
  • 4
  • 12
2

There's no wildcard support. You can just write your own Matlab function to add and remove all the paths in your project, or to support regexp matching. It's convenient to make that part of the project itself, so it can be aware of all the dirs that need to be added or removed, and do other library initialization stuff if that ever becomes necessary.

Andrew Janke
  • 23,508
  • 5
  • 56
  • 85
1

The genpath answer works for fragment* cases, but not a *fragment* case.

Clunky, but works:

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
Denise Skidmore
  • 2,286
  • 22
  • 51
1

I have a shorter answer:

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
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • I am not completely sure if this removes all the subpaths. It was a long time ago since I dealt with this issue, but the main issue as far as I know it was that the subfolders could also consist of subfolders. The accepted answer will create a recursive tree of directories and then `rmpath` will remove them all. – patrik Oct 31 '16 at 12:43