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

10 次查看(过去 30 天)
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({found_files.name}, '[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 个)

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

标签

产品


版本

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by