regexp to return folders
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Lewis
el 9 de Jun. de 2020
I have a directory with subdirectories, and several of those have a specific naming format. I'd like to extract the folder names of only those but I can't figure out how to return them. For example, assuming I have the parent path containing those sub directories defined as parent_path:
sub_dir_names = cellstr(ls(parent_path))
this returns all of ths sub dirs. Then I'd like to filter out those that do not meet the naming convention 'sub-', where a number appears after the hyphen. I guess regexp is useful here:
regexp(sub_dir_names, 'sub-', 'match')
But this does not do the job. What should I be doing instead? Seems quite simple.
0 comentarios
Respuesta aceptada
Stephen23
el 9 de Jun. de 2020
Editada: Stephen23
el 9 de Jun. de 2020
Why not just specify the name format to only return names that start with 'sub-', e.g.:
S = dir(fullfile(parent_path,'sub-*'));
D = {S([S.isdir]).name} % only folders starting with 'sub-'
If there are other folder names which also start with sub- but don't have the trailing number, then add this:
X = cellfun(@isempty,regexpi(D,'^sub-\d+'));
D(X) = []
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!