MATLAB Answers

How to list files with multiple extensions?

96 views (last 30 days)
Kevin
Kevin on 24 Oct 2012
Answered: ANKUR WADHWA on 6 Nov 2019
Suppose I want mp3 or avi files from a folder, on Unix I could do
ls *.{mp3|avi}
Is there a clean one-liner way to do this in matlab? Presently, I can do it in two lines by doing the following:
f = dir(folder)
f(~cellfun(@isempty,regexpi({f.name},'.*(mp3|avi)'))).name
However, I find this rather ugly compared to the simplicity of the first expression.
Thanks, Kevin
  1 Comment
Matt J
Matt J on 24 Oct 2012
If you find it ugly, why not just wrap it in an mfile of your own?

Sign in to comment.

Answers (3)

Doug Hull
Doug Hull on 24 Oct 2012
Can you run the command twice with two filters? The results would be two structures of the same format that could then be appended together.
  2 Comments
Sean de Wolski
Sean de Wolski on 24 Oct 2012
@Kevin, a for-loop over a cell array containing the extentions would allow it to scale, though concatenating structures is ugly.

Sign in to comment.


Sean de Wolski
Sean de Wolski on 24 Oct 2012
You could just farm that command out to Unix if you are on a unix system...
  1 Comment
Kevin
Kevin on 24 Oct 2012
You mean, something like:
[~, sysout] = system(['ls ' fullfile(folder,'*{mp3,avi}')]);
fileNames = regexp(sysout,'\n','split');
While this would work, cross comparability with windows is a must for me. Thanks for your input, though!

Sign in to comment.


ANKUR WADHWA
ANKUR WADHWA on 6 Nov 2019
Though the thread is very old still for users who are looking for an easy way a very simple existing solution on Matlbcentral
https://in.mathworks.com/matlabcentral/answers/16173-dir-search-multiple-files

Community Treasure Hunt

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

Start Hunting!