function paths = addgenpath(path, varargin) %ADDGENPATH add path, +children, -special dirs to MATLAB path. % % Adds path and its subdirectories, but excluding some special directories used % by version control or for testing. The remaining arguments are passed on to % Matlab's own addpath function. For example '-END' is often a good idea. % Iain Murray, November 2007 % cellmap() is also provided by my Matlab utilities, but that version not be on % the Matlab path yet. cellmap = @(fn, x) cellfun(fn, x, 'UniformOutput', false); % These directories shouldn't pollute the Matlab path: exclude = {'CVS', '.svn', '.bzr', 'test(ing|)(|_)(|stuff|rig)'}; if ~exist('octave_config_info') % I put code only needed by Octave in their own directories. % Otherwise adding them to the path can lead to name-clash warnings. exclude = {exclude{:}, 'octave'}; end % All sub-directories paths = genpath(path); % Get all the paths between the PATHSEP character pat = ['([^', pathsep, ']+)', pathsep, '?']; paths = cellmap(@(x) x{1}, regexp(paths, pat, 'tokens')); % Filter out unwanted directories exclude_pat = cellmap(@(x) ['(/',x,'(/|$))|'], exclude); exclude_pat = cell2mat(exclude_pat); exclude_pat = exclude_pat(1:end-1); % remove final erroneous '|' mask = cellfun(@isempty, regexp(paths, exclude_pat, 'start')); paths = paths(mask); % Octave doesn't seem to find private directories yet exludes them from % genpath!? Find any .../private directories and add them back in: if exist('octave_config_info') privates = cellmap(@(x) [x, filesep, 'private'], paths); mask = find(cellfun(@(x) exist(x, 'dir'), privates)); paths = {paths{:}, privates{mask}}; end % Finally add the paths addpath(paths{:}, varargin{:});