DIR multiple file extensions

조회 수: 88 (최근 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 CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by