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

조회 수: 12 (최근 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({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