フィルターのクリア

regexp to return folders

25 ビュー (過去 30 日間)
Lewis
Lewis 2020 年 6 月 9 日
編集済み: Stephen23 2020 年 6 月 9 日
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.

採用された回答

Stephen23
Stephen23 2020 年 6 月 9 日
編集済み: Stephen23 2020 年 6 月 9 日
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 件)

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

タグ

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by