4.0

4.0 | 6 ratings Rate this file 260 downloads (last 30 days) File Size: 3 KB File ID: #21086

Recursive addpath

by Anthony Kendall

 

14 Aug 2008 (Updated 02 Dec 2008)

Recursively adds directories to MATLAB path, optionally ignores some

Download Now | Watch this File

File Information
Description

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.  
 
The latest 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.

Acknowledgements
This submission has inspired the following:
, , , 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  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (9)
15 Aug 2008 B. Roossien What is wrong with addpath(genpath())?
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.
20 Nov 2008 Jesse Hopkins 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.
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) 
25 Nov 2008 Thierry Dalon  
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 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 Sven 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 Sven 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!
Please login to add a comment or rating.
Updates
15 Aug 2008 Fixed small error in initial submission
21 Nov 2008 Made changes to reflect reviewer comments. Thanks!
02 Dec 2008 Added new capability to recursively remove directories, fixed a small bug.

Public Submission Policy

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.

Contact us at files@mathworks.com