View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Recursive addpath

4.3 | 12 ratings Rate this file 10 Downloads (last 30 days) File Size: 3.54 KB File ID: #21086 Version: 1.6

Recursive addpath



14 Aug 2008 (Updated )

Recursively adds directories to MATLAB path, optionally ignores some

| Watch this File

File Information

This is a very simple function that will add all directories beneath a specified directory (or the current directory if none is specified), with the option to ignore a specified list of directories. Any directories that are ignored (for instance, '.svn' and 'CVS' if using version control) will not have their subdirectories added either.

This is a fast, very lightweight--and easily modifiable--function that should work on any platform (not tested explicitly). I use it to add the directory trees of toolboxes not stored in MATLAB's toolbox folder, as well as for modules checked out of SVN and CVS source control repositories.

Per reviewer suggestions and comments, this now ignores private directories, overloaded methods directories, and also hidden directories with no need to specify additional directories. It also accepts flags as in 'addpath' to place the directories at the beginning or end of the path.

An addition to this function (12/01/2008) is the ability for it to be run in 'reverse', recursively removing directories from the path. This capability can be very useful when working with multiple copies of a code repository, for instance. Also, I fixed a bug as mentioned by a reviewer that properly handles files without extensions.

Thanks to the extensive help of a user (Joe McDonnell), this function has been almost totally rewritten with better error detection, more efficient operation (much quicker run times, even for large directory trees), an extended debug mode that lists everything the function does, and better help. Also, now ignores the 'CVS' directories by default.

Another user (Jim) made a suggestion which resulted in a speed increase of approximately 3x in a moderate-sized directory tree (which should increase with larger sized trees). Also, a small change is made for Mac compatibility.


This file inspired Genpath Exclude.

MATLAB release MATLAB 7.6 (R2008a)
Other requirements Known to work on Windows, should work on Linux and Mac as well. Should work with any somewhat recent MATLAB release (especially if the assert function calls are removed or replaced with if..error..end sequences).
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (18)
21 Apr 2016 Kris Janssen

I still find this function useful. However, in it's current form it results in console warnings when e.g. package folders (those starting with a '+') are picked up.

To remedy this I inserted two quick lines (I included two lines of the original for reference):

%Build the list of directories
caAddRemDirs = {};
[caAddRemDirs] = addpath_recursively(caAddRemDirs, strStartDir, caStrsIgnoreDirs, strXorIntAddpathMode, blnRemDirs,blnDebug);

%KJ: find all module dirs (with a '+' in the name).
matches = strfind(caAddRemDirs,'+');
%Retain folders with no match for '+'
caAddRemDirs = caAddRemDirs(cellfun(@isempty,matches));

Warnings gone!

01 Oct 2013 David G


11 Jan 2012 Miguel

Miguel (view profile)

07 Apr 2010 Pearl

Pearl (view profile)

04 Nov 2009 Anthony Kendall

Thanks Jim!

I made that change for compatibility with Macs. Also, based on your first comment back in October I increased the speed of the code by a factor of 3 in a moderate-sized directory tree.

Comment only
02 Nov 2009 Jim Hokanson

Jim Hokanson (view profile)

It seems I can't figure out how to work this comment system. The main point of the last post was that the code doesn't work on a MAC as strStarDir\*. doesn't work, it only returns . and ..

Comment only
02 Nov 2009 Jim Hokanson

Jim Hokanson (view profile)

OOPS, that second part should read:
saSubDirs = dir(strStartDir);
saSubDirs = saSubDirs([saSubDirs.isdir]);

Comment only
12 Oct 2009 Jim Hokanson

Jim Hokanson (view profile)

12 Oct 2009 Jim Hokanson

Jim Hokanson (view profile)

Just to clarify on that last point, keeping a cell array of all directories to process, then using addpath(pathsOut{:},strXorIntAddpathMode) (where pathsOut is a cell array of all paths to add) works well

25 Nov 2008 Sven

Sven (view profile)

You can avoid the first bug I mentioned above by adding the following at line 59:
currDir = currDir([currDir.isdir]);

Oh, and I just checked in genpath, and see that IT uses recursive calls to 'dir'. Therefore my second suggestion doesn't actually avoid these multiple calls!

25 Nov 2008 Sven

Sven (view profile)

Thank you for this very useful function. I have two main comments:
1. I think that there's a bug that causes the function to crash if a file without ANY extension is in a directory. For example, some people submit file exchange packages that include a file called "README". Your function fails on this case (at the moment).

2. I haven't checked yet, but perhaps this could be rewritten without using recursive functions. I think that if you simply use genpath to get a list of directories, you could then filter this list all in one go. This may be a little faster.

Nice function!

25 Nov 2008 Thierry Dalon

Be aware: you are using assert function. This is not available in R14 or previous!
Moreover I am not sure if assert exit the function...

25 Nov 2008 Thierry Dalon

it is not possible to change a rating in this new FX...sorry for the previous rating.
I think now it is a very useful file. Thanks for updating!
One last suggestion: I would consider a default ignore value possible in the script. For example, if no ignore input argument use ignore = {'.svn'}, editable by the user so that users can simply call your file without passing always an ignore option if they have a predilected default value.

25 Nov 2008 Thierry Dalon

21 Nov 2008 Thierry Dalon

@Author: the idea of an ignore option is good.
Good would be if you could also directly pass through ignore filter like "all directories starting wih "@" or "#"". At the best would be generic wildcard expressions.
Small missing point:Your implementation does not allow to use addpath intrinsec option like -BEGIN and -END.
You removed some features from original Matlab GENPATH which make your version not optimized. (private and @ not ignored by default; your loop run also over all files!)
@Jesse: this can be done with a small correction in the code:
if ~any(strcmp(currDir(m).name,{'.','..',ignore{:}})) && currDir(m).isdir && ~strncmp(currDir(m).name,'@',1) && ~any(strcmp(currDir(m).name,{'private','.',,'..')) ...
(see also original GENPATH)

20 Nov 2008 Jesse Hopkins

Jesse Hopkins (view profile)

Works pretty good, but it doesn't automatically ignore "private" directories and "@" directories like genpath does. You can easily add "private" to the list of ignored directories, however being able to skip over all "@" directories requires some code change.

Comment only
15 Aug 2008 Anthony Kendall

Genpath is unable to ignore directories, which is a key feature for certain uses (like source code repositories). That's the real utility of this function.

Comment only
15 Aug 2008 B. Roossien

What is wrong with addpath(genpath())?

Comment only
21 Nov 2008 1.3

Made changes to reflect reviewer comments. Thanks!

02 Dec 2008 1.4

Added new capability to recursively remove directories, fixed a small bug.

14 Apr 2009 1.5

Extensive modifications to code, renamed variables, better input checking, error checking, a debug mode, more examples, better help.

04 Nov 2009 1.6

Optimizations and Mac compatibility changes.

Contact us