Using regex to find the part in pwd that should have a common project.m is a pretty reliable way:
P = regexp(pwd, ['^.*root' filesep], 'match'); if isempty(P) error('project:globaldir_missing',... 'Could not find global data directory.'); end newPath = [P{1} 'DATA' filesep 'GLOBAL']; if ~exist(newPath , 'dir') error('project:pathing_error',... 'Global data directory does not seem to exist.'); end genpath(newPath);
Using filesep , you make it independent of the features of the OS, for example, it will work on any OS.
Note that you are creating a dependency on the specific directory tree of your project, but well, this is the MATLAB path.
source share