필터 지우기
필터 지우기

How to list files with multiple extensions?

조회 수: 43 (최근 30일)
Kevin
Kevin 2012년 10월 24일
편집: Cris LaPierre 2024년 1월 9일
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
Matt J
Matt J 2012년 10월 24일
편집: Matt J 2012년 10월 24일
If you find it ugly, why not just wrap it in an mfile of your own?

댓글을 달려면 로그인하십시오.

답변 (3개)

Doug Hull
Doug Hull 2012년 10월 24일
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
Kevin
Kevin 2012년 10월 24일
Specifically, do you mean something like this?
f = [dir(fullfile(folder,'*mp3')); dir(fullfile(folder,'*avi'))];
I considered this, and it can certainly do for my present situation. However, it doesn't scale as well to more than a couple of extensions (at least the way I presented it) as the cellfun thing I put previously.
Thanks for the suggestion, though!
Sean de Wolski
Sean de Wolski 2012년 10월 24일
@Kevin, a for-loop over a cell array containing the extentions would allow it to scale, though concatenating structures is ugly.

댓글을 달려면 로그인하십시오.


Sean de Wolski
Sean de Wolski 2012년 10월 24일
You could just farm that command out to Unix if you are on a unix system...
  댓글 수: 1
Kevin
Kevin 2012년 10월 24일
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!

댓글을 달려면 로그인하십시오.


ANKUR WADHWA
ANKUR WADHWA 2019년 11월 6일
편집: Cris LaPierre 2024년 1월 9일
Though the thread is very old still for users who are looking for an easy way a very simple existing solution on Matlbcentral

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by