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

108 ビュー (過去 30 日間)
Robert Moss
Robert Moss 2013 年 9 月 9 日
編集済み: Jan 2021 年 7 月 20 日
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?

採用された回答

Jan
Jan 2013 年 9 月 10 日
編集済み: Jan 2021 年 7 月 20 日
pathCell = regexp(path, pathsep, 'split');
if ispc % Windows is not case-sensitive
onPath = any(strcmpi(Folder, pathCell));
else
onPath = any(strcmp(Folder, pathCell));
end
[EDITED] A faster version - beside the faster strsplit since Matlab R2013a:
s = pathsep;
pathStr = [s, path, s];
onPath = contains(pathStr, [s, Folder, s], 'IgnoreCase', ispc);
  6 件のコメント
Sean de Wolski
Sean de Wolski 2013 年 9 月 10 日
This is correct @Robert. So why not add the parent path blindly? This is what you're checking anyway right?
Chris Eguires
Chris Eguires 2021 年 7 月 20 日
For slightly more speed, you can just use strsplit(path,pathsep). Shouldnt make a big difference, but if you have thousands of paths, maybe a fraction of a second...

サインインしてコメントする。

その他の回答 (5 件)

Arthur
Arthur 2013 年 9 月 9 日
exist('dirname','dir')
Returns 7 if the dir is on your path, 0 if not.
  3 件のコメント
Thomas
Thomas 2013 年 9 月 9 日
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 2013 年 9 月 9 日
Ah yes you're right. I thought it check if it's on the searchpath, but it doesn't.

サインインしてコメントする。


Sean de Wolski
Sean de Wolski 2013 年 9 月 9 日
編集済み: Sean de Wolski 2013 年 9 月 9 日
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 件のコメント
Jan
Jan 2013 年 9 月 10 日
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]))
Sean de Wolski
Sean de Wolski 2013 年 9 月 10 日
Good to learn!

サインインしてコメントする。


Brett
Brett 2016 年 6 月 14 日
編集済み: Brett 2016 年 6 月 14 日
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\".

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 9 月 9 日
isdir('your_folder_name')
  1 件のコメント
Adam Danz
Adam Danz 2020 年 8 月 16 日
isdir() or the newer isfolder() returns true if the input is a directory whether it's on-path or not (for full paths).

サインインしてコメントする。


Image Analyst
Image Analyst 2013 年 9 月 9 日
編集済み: Image Analyst 2013 年 9 月 9 日
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.

カテゴリ

Help Center および File ExchangeSearch Path についてさらに検索

タグ

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by