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.
Try using genpath. Given the base directory as input, genpath returns that base directory plus all subdirectories, recursive.
rmpath(genpath(base_directory));
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With