DIR multiple file extensions

25 ビュー (過去 30 日間)
Lisa Loh
Lisa Loh 2023 年 2 月 5 日
コメント済み: Voss 2023 年 2 月 6 日
Hi, is there a simpler and shorter way for writing this code?
The code is working fine so I assume I'm writing it correctly, just that if there are more extensions to be added, it will be long and repetitive.
extensions = [...
dir(fullfile(folder,'*.txt'));
dir(fullfile(folder, '*.py'));
dir(fullfile(folder, '*.mp3'));
dir(fullfile(folder, '*.exe'));
dir(fullfile(folder,'*.jpg'));
];

採用された回答

Voss
Voss 2023 年 2 月 5 日
You can put those dir() calls in cellfun:
ext = {'*.txt','*.py','*.mp3','*.exe','*.jpg'};
extensions = cellfun(@(x)dir(fullfile(folder,x)),ext,'UniformOutput',false);
extensions = vertcat(extensions{:});
Then when you need to add more extensions, just include them in the 'ext' variable.
  4 件のコメント
Lisa Loh
Lisa Loh 2023 年 2 月 6 日
Thank you very much for the deep explanation!
Voss
Voss 2023 年 2 月 6 日
You're welcome!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

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

タグ

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by