regexp to return folders

19 Ansichten (letzte 30 Tage)
Lewis
Lewis am 9 Jun. 2020
Bearbeitet: Stephen23 am 9 Jun. 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.

Akzeptierte Antwort

Stephen23
Stephen23 am 9 Jun. 2020
Bearbeitet: Stephen23 am 9 Jun. 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) = []

Weitere Antworten (0)

Kategorien

Mehr zu File Operations finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by