MATLAB Answers

K E
0

Looking within search path for a directory containing a chosen string

K E
さんによって質問されました 2015 年 10 月 13 日
最新アクティビティ dpb
さんによって 回答されました 2015 年 10 月 13 日
dpb
さんの 回答が採用されました
I have many directories on my search path and would like to search for path directories that contain a certain string, say 'matlab_programs'. Here is what I've tried (based on this answer and this answer). Is there a better/faster way?
strToFind = 'matlab_programs'; % Return all search path directories containing this string
dirs = regexp(path,['[^;]*'],'match'); % List all paths in search path and break them into a cell array
% Index to cell entries containing the desired string
whichCellEntry = find(cellfun(@(dirs) ~isempty( strfind(dirs, strToFind) ), dirs) == 1);
dirs(whichCellEntry)' % Display all directories containing chosen string
% Make Matlab window wide before running, or the directory listings will be truncated

  0 件のコメント

サインイン to comment.

タグ

1 件の回答

dpb
回答者: dpb
2015 年 10 月 13 日
 採用された回答

Probably not a whole lot unless...
IF the subdirectories were to be on a given higher level path branch, then one could probably get the desired result directly via a shell dir() command with the suitable wild card. Would depend on the shell for specific switches, of course, as well as the above condition. On Winders CMD.EXE, the following seems to work...
[~,dirs]=dos(['dir /s /a:d /b *' strToFind '*'];
Whether it would be faster/better would likely be in the eye of the beholder and how high up in a location would have to be to cover the whole chain desired. Of course, one will need to ensure the starting path is where it needs must be in the running shell so likely need to create a batch file that takes care of that detail and dispatch it instead...
I haven't ever understood why there isn't an extension to a cellstr of strfind that "just works"; doesn't seem that big a deal.

  0 件のコメント

サインイン to comment.



Translated by