Create an array of file names produced by system('dir /S *.ext')
Ältere Kommentare anzeigen
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...
Akzeptierte Antwort
Weitere Antworten (1)
Sven Mesecke
am 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
5 Kommentare
Mujhaid
am 6 Sep. 2014
I want to ask how i can access each individual element of structure array?
franco otaola
am 7 Okt. 2016
is there any possibility of extract the name without the extension? thanks
Walter Roberson
am 7 Okt. 2016
Use fileparts() to extract the name without the extension.
nvmnghia
am 23 Feb. 2020
Why can't we use
b = [a.name]
here?
@nvmnghia you can try it yourself on your computer. But now MATLAB supports something like Jupyter notebook on a virtual environment:
pwd
unix('echo"" > thisfile1.txt'); unix('echo"" > ThatFile-1234.txt');
a = dir('*.txt')
b = [a.name]
This will create a very wide character array by horizontally concatenating cated character arrays (i.e., file names). Then it becomes another problem to access each file. You usually can't do vertical concatenation because often the file names have different number of characters.
b = cat(1,a.name)
So, this is why you want to cast them into a cell array, instead of a character array, as @Sven Mesecke wrote above.
Kategorien
Mehr zu File Operations finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!