regexp to return folders
5 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Stephen23
on 9 Jun 2020
Edited: Stephen23
on 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) = []
0 Comments
More Answers (0)
See Also
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!