patrik
patrik

Reputation: 4558

remove path and subpaths in matlab

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.

Upvotes: 7

Views: 8822

Answers (4)

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

Upvotes: 0

Denise Skidmore
Denise Skidmore

Reputation: 2416

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

Upvotes: 1

Steve Osborne
Steve Osborne

Reputation: 680

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

rmpath(genpath(base_directory));

Upvotes: 13

Andrew Janke
Andrew Janke

Reputation: 23908

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.

Upvotes: 2

Related Questions