Azzera filtri
Azzera filtri

regexp to return folders

6 visualizzazioni (ultimi 30 giorni)
Lewis
Lewis il 9 Giu 2020
Modificato: Stephen23 il 9 Giu 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.

Risposta accettata

Stephen23
Stephen23 il 9 Giu 2020
Modificato: Stephen23 il 9 Giu 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) = []

Più risposte (0)

Categorie

Scopri di più su File Operations in Help Center e File Exchange

Tag

Prodotti


Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by