Find files according to a pattern that are on the Matlab path?
10 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Monika Jaskolka
el 11 de Nov. de 2021
Editada: Walter Roberson
el 12 de Nov. de 2021
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)):
0 comentarios
Respuesta aceptada
Walter Roberson
el 12 de Nov. de 2021
Editada: Walter Roberson
el 12 de Nov. de 2021
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 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre File Operations en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!