MATLAB Answers

How can I determine if a directory is on the MATLAB path programmatically?

337 views (last 30 days)
Robert Moss
Robert Moss on 9 Sep 2013
Edited: Brett on 14 Jun 2016
I want to be able to programmatically determine if an arbitrary directory is on the MATLAB path. I want to do this because the publish command will complain if you give it a file name that is not on the path. Is there a clever way to determine if a directory is on the MATLAB path without having to parse the path myself?

  0 Comments

Sign in to comment.

Accepted Answer

Jan
Jan on 10 Sep 2013
Edited: Jan on 10 Sep 2013
pathCell = regexp(path, pathsep, 'split');
if ispc % Windows is not case-sensitive
onPath = any(strcmpi(Folder, pathCell));
else
onPath = any(strcmp(Folder, pathCell));
end

  5 Comments

Show 2 older comments
Robert Moss
Robert Moss on 10 Sep 2013
@Sean, In many cases that would probably be fine, but in my specific instance the file that I am publishing is an m file that is in a class subdirectory, and I don't think that type of subdirectory should actually be on the path. (The directory within which a class directory resides should be on the path, but not the class directory itself).
Image Analyst
Image Analyst on 10 Sep 2013
Then use genpath() to get subfolders added to the path also:
addpath(genpath(theFolder));
Sean de Wolski
Sean de Wolski on 10 Sep 2013
This is correct @Robert. So why not add the parent path blindly? This is what you're checking anyway right?

Sign in to comment.

More Answers (5)

Arthur
Arthur on 9 Sep 2013
exist('dirname','dir')
Returns 7 if the dir is on your path, 0 if not.

  3 Comments

Matt J
Matt J on 9 Sep 2013
This assumes that dirname is not the full system path name. If it is a full path, 7 will always be returned, even if that directory is not on the MATLAB path.
Thomas
Thomas on 9 Sep 2013
Not sure this is correct. It will return 7 if the directory exists and 0 if the directory does not..
I do not think it has to do with the directory being on the path or not.
Arthur
Arthur on 9 Sep 2013
Ah yes you're right. I thought it check if it's on the searchpath, but it doesn't.

Sign in to comment.


Sean de Wolski
Sean de Wolski on 9 Sep 2013
Edited: Sean de Wolski on 9 Sep 2013
Parsing the path isn't so bad!
This seems like a simple check:
if ispc
% Windows is not case-sensitive
onPath = ~isempty(strfind(lower(path),lower('H:\Documents\MATLAB;')))
else
onPath = ~isempty(strfind(path,'H:\Documents\MATLAB;'))
end
Of course for your use-case it's not too much work to just add the folder in question to the path regardless, addpath takes care of redundancy:
addpath H:\documents\MATLAB
addpath H:\documents\MATLAB
addpath H:\documents\MATLAB
addpath H:\documents\MATLAB
addpath H:\documents\MATLAB
clc
path

  2 Comments

Jan
Jan on 10 Sep 2013
Instead of relying on the semicolon as a separator, using the command pathsep is more flexible because it considers the OS-specific separator automatically:
onPath = ~isempty(strfind(path, ['H:\Documents\MATLAB', pathsep]))

Sign in to comment.



Image Analyst
Image Analyst on 9 Sep 2013
Edited: Image Analyst on 9 Sep 2013
Why can't you just give it the full filename? Use
fullFileName = fullfile(folder, baseFilename);
I mean, you already have the folder because you were going to use that to try to figure out if it was on the search path. But instead of doing that, just construct the full filename like I showed you above.
Alternatively, just try to open the file and check if the file ID indicates it didn't find it.
fid = fopen(baseFileName); % Don't use folder or full file name.
if fid == -1 .... etc.

  0 Comments

Sign in to comment.


Brett
Brett on 14 Jun 2016
Edited: Brett on 14 Jun 2016
This made the most sense to me...
ismember(some_path, strsplit(path, pathsep))
These days I'm more familiar with Python than Matlab, so I'm sorta doing the Matlab equivalent of something like "file in pathstr.split(';')". I assume that checking membership is cheaper than searching... this logic may not be true for Matlab cell arrays, I'm not sure. My next assumption was that strsplit should always be preferred if your pattern is a known constant expression (vs. using regex for simple splitting which is an expensive operation). This avoids all the pitfalls of string matching, like false positives of "C:\some_path" being accidentally found inside "C:\some_path\sub_path\subsub\".

  0 Comments

Sign in to comment.

Sign in to answer this question.

Tags

Products