フィルターのクリア

Get full path of directory that is on Matlab search path

31 ビュー (過去 30 日間)
Justin Solomon
Justin Solomon 2017 年 7 月 8 日
コメント済み: Justin Solomon 2017 年 7 月 9 日
Hello,
I have a folder called 'mydir' that is in the Matlab search path but not in my current directory. Thus isdir('mydir') is true. Is there a way to easily extract the full path of 'mydir' in this scenario?
Thanks, Justin

採用された回答

per isakson
per isakson 2017 年 7 月 8 日
One way
>> s = what('InUse');
>> s.path
ans =
h:\m\FEX\InUse
where InUse is a folder on my Matlab path
  3 件のコメント
per isakson
per isakson 2017 年 7 月 8 日
Yes that right. Doc says: " You do not need to specify the @ or + symbol for class and package folders."
Justin Solomon
Justin Solomon 2017 年 7 月 9 日
Thanks! This works for my situation!

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2017 年 7 月 8 日
編集済み: Walter Roberson 2017 年 7 月 8 日
Easy? Ummm, how about possible in a few lines?
tofind = 'mydir';
esctofind = regexptranslate('escape', tofind); %in case it has special characters
dirs = regexp(path, pathsep,'split'); %cell of all individual paths
temp = unique(cellfun(@(P) strjoin(P(1:find(strcmp(esctofind, P),1,'last')),filesep), regexp(dirs,filesep,'split'), 'uniform', 0)); %don't let the blue smoke escape
fullpaths = temp(~cellfun(@isempty,temp)); %non-empty results only
Notes:
  1. fullpaths will be a cell array of character vectors
  2. fullpaths might have multiple entries, as there might be multiple matches
  3. it is possible for any one path to have multiple occurrences of the same directory name; this code deliberately goes for the longest version. For example if the search was for 'matlab' then it would pick /Applications/MATLAB_R2017a.app/toolbox/matlab/datatools/inspector/matlab instead of /Applications/MATLAB_R2017a.app/toolbox/matlab
  4. the directory name to find may have special characters such as '.' which will be treated literally, not as wild-cards
  5. the code as posted will not match package directories that have '+' or '@' before the name being searched for, but that would be easy to add. The code will find such directories if you ask for them specifically.
  6. the code has been designed to work on Windows, Mac, and Linux
  1 件のコメント
Justin Solomon
Justin Solomon 2017 年 7 月 9 日
Thanks Walter, this is is great solution and seems to be robust against many situations.

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

カテゴリ

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