File Exchange

image thumbnail

genpath_exclude

version 1.4.0.0 (2.22 KB) by Jesse Hopkins
Executes like genpath, but can ignore specified directories.

0 Downloads

Updated 27 Oct 2009

View License

Executes like genpath, but can ignore directories. Useful to add a directory hierarchy to your path, but ignore CM directories such as "CVS" or ".svn"

Directories may be excluded using regular-expressions as well.

Tested on windows only, but should work on any platform.

Example usage:
genpath_exclude('C:\myDir',{'CVS'}) %<--- simple usage to ignore CVS direcotries

genpath_exclude('C:\myDir',{'\.svn'}) %<--- simple usage to ignore .svn (note that "." must be escaped for proper handling in the regexp)

genpath_exclude('C:\myDir',{'CVS','#.*'}) %<----more advanced usage to ignore CVS directories and any directory starting with "#"

Comments and Ratings (10)

Barbara

David

The default exclude string for this function is missing an entry for package directories '+packagename'.

The missing pattern results in warnings on import if you attempt to include a package dir.

The full exclude path should be:

if ~any(regexp(dirname,['^\.$|^\.\.$|^\@.*|^\+.*|^private$|' excludeStr ],'start'))

craq - sorry I think the original version is long lost. However here's a hint from my memory:

dirs_str = genpath();
dirs_cell = regexp(dirstr,';','split');
matchstr = 'a[\\/]c|d[\\/]c'
dirmatch = regexp(dirs_cell,matchstr,'start','once');
matchidx = find(cellfun(@(x) isempty(x),dirmatch));
addpath(dirs_cell{matchidx});

craq - I'll do a little digging and see if I can find my original version. On another note, one way I've done something similar to yours in the past is via 'private'. If there were folders that I didn't want to end up on the path, but that I might want to dynamically add later, i would encapsulate those directories in private.

i.e.
a/private/b, a/c, d/c.

Using this method, the directories under "private" won't be added by default, because genpath won't look underneath private, however if you can explicitly add "a/private/b" dynamically if you choose to do so later. Beware however, that right-clicking a folder in the file-browser and selecting "add folder and subfolders" WILL recurse through private directories. (I have submitted a bug report and apparently this behavior will be changed to be consistent w/ genpath in a future release).

craq

Thanks for this, it works exactly as advertised. One thing I'd like to do is match for a sequence of 2 directories (i.e. exclude a/b but include a/c and d/b). This doesn't seem possible from within genpath, but might be possible by parsing the output from genpath. According to the comments, you already did this in an older version. Would you be able to put this on the file exchange please? Or does anybody know how to access file history here?

Thanks. Very simple and nice. To avoid warnings with package folders, i propose to ignore package folders by default. To do so replace line 81 with the following code:
if ~any(regexp(dirname,['^\.$|^\.\.$|^\@.*|^\+.*|^private$|' excludeStr ],'start'))

Works a treat.

Sebastian

Does what it says on the tin. Excellent subfolder exclusion.

Thanks for the comment Thierry. I did look at modifying genpath, and for some reason on my first attempt, the excluded path would still be added, but not the tree beneath it. That is when I went off looking into the regexp business to remove the offending paths.

Taking a second look, I realize that it is possible to modify genpath directly accomplish this goal, which should operate faster since it wouldn't recurse on the ignored directories.

I think regexp with option 'split' is quite new.=>This may not work with R14 or previous.
For optimization why don't you modify directly genpath to not go into excluded directories?

Updates

1.4.0.0

fixed regexp handling. wrapped each expression in ^ and $ chars. Added more help comments

1.3.0.0

Updated description.

1.2.0.0

Made input more robust so that if user enters a string as 'excudeDir' rather than a cell array of strings this function will still work.

1.1.0.0

Updated file so that it is a modified version of genpath, rather than post-processing the output of genpath.

MATLAB Release Compatibility
Created with R2007b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags