File Exchange

image thumbnail


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


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 "#"

Cite As

Jesse Hopkins (2020). genpath_exclude (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (10)



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'))

Jesse Hopkins

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));

Jesse Hopkins

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.

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).


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?

Marcel Rüland

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'))

Oliver Woodford

Works a treat.


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

Jesse Hopkins

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.

Thierry Dalon

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?


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

Updated description.

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.

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