MATLAB Answers

3

Create an array of file names produced by system('dir /S *.ext')

Asked by bugguts99 on 27 Apr 2011
Latest activity Commented on by Walter Roberson
on 7 Oct 2016
Hi there,
I know that you can search directories and sub-directories to provide a list of file names that meet a particular criteria using the code below:
[status,list]=system('dir /S *.mp3');
My question is: is it possible to create an array from the file names generated this way in order to create a loop for further processing??
Thanks in advance...

  0 Comments

Sign in to comment.

2 Answers

Answer by Richard Alcock on 27 Apr 2011
 Accepted Answer

You can use textscan to split the multiple lines of output from the system command into a cell array.
[status, list] = system( 'dir /B /S *.mp3' );
result = textscan( list, '%s', 'delimiter', '\n' );
fileList = result{1}
Note that I've added /B to the dir command to make the output easier to parse.

  2 Comments

is there any possibility of extract the name without the extension? thanks

Sign in to comment.


Answer by Sven Mesecke on 27 Apr 2011

What's wrong with the MATLAB function 'dir'? You can add wildcards like .mp3 and get a struct array in return, whose name field can easily be converted into a cell array of filenames:
a=dir('S/*.ext');
b={a.name}
Otherwise there's a nice tool on MATLAB Central that uses regular expressions for dir: http://www.mathworks.com/matlabcentral/fileexchange/16216-regexpdir

  3 Comments

I want to ask how i can access each individual element of structure array?
is there any possibility of extract the name without the extension? thanks
Use fileparts() to extract the name without the extension.

Sign in to comment.