MATLAB: using genpath with partial paths

I have the following directory structure:

β”œβ”€β”€ root β”‚ β”œβ”€β”€ DATA β”‚ β”‚ β”œβ”€β”€ GLOBAL β”‚ β”œβ”€β”€ project1 β”‚ β”‚ β”œβ”€β”€ branches β”‚ β”‚ β”‚ └── dev β”‚ β”‚ β”‚ └── project.m β”‚ β”‚ └── trunk β”‚ β”‚ └── project.m 

How can I elegantly move on to adding DATA / GLOBAL to the path from both project.m files?

Essentially, I'm looking for genpath work for partial paths. exist('DATA/GLOBAL') returns 7 (a directory was found), but genpath('DATA/GLOBAL') returns '' (no path was created).

+4
source share
1 answer

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.

+1
source

Source: https://habr.com/ru/post/1491536/


All Articles