Find files according to a pattern that are on the Matlab path?

30 ビュー (過去 30 日間)
Monika Jaskolka
Monika Jaskolka 2021 年 11 月 11 日
編集済み: Walter Roberson 2021 年 11 月 12 日
Let's say I have 2 folders of generated code. One is on my Matlab path, and one is not. I would like to find all C header files that end in a number between 1 and 3 that are on the path. What is the best way of doing this? I feel like what I have below is clunky.
folder = 'C:\code';
found_files = dir([folder filesep '**' filesep '*.h']); % Finds all 10 files
within_range_idx = ~cellfun(@isempty, (regexp({}, '[1-3]\.h$')));
found_files = found_files(within_range_idx);
pathCell = regexp(path, pathsep, 'split');
found_files = found_files(contains({found_files.folder}, pathCell)):


Walter Roberson
Walter Roberson 2021 年 11 月 12 日
編集済み: Walter Roberson 2021 年 11 月 12 日
I would say your general approach is probably about the best that you could hope for. You need to determine which files exist, and you need to determine which ones are on the path.
You could make some efficiency improvements. For example you could unique() the folder information before doing the contains(). You could use ismember() instead of contains().
Unfortunately, dir() does not itself recognize using '[1-3]' so you cannot combine the restriction with the dir()
Depending on your operating system, the search might potentially be more efficient if you system() out to a system path search. For example on MacOS or Linux, you might be able to use
[status, results] = system('find ~/code -name \*[123].h -print')
I do not have enough experience with WIndows do know if you can do something like
[status, results] = system('dir /s *[123].h')
but in a way that restricts to C:\code as the starting directory.

その他の回答 (0 件)


Find more on Search Path in Help Center and File Exchange





Community Treasure Hunt

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

Start Hunting!

Translated by