I need to get all those files under D:\dic
and loop over them to further process individually.
Does MATLAB support this kind of operations?
It can be done in other scripts like PHP,Python...
I need to get all those files under D:\dic
and loop over them to further process individually.
Does MATLAB support this kind of operations?
It can be done in other scripts like PHP,Python...
You can use regexp or strcmp to eliminate
.
and..
Or you could use theisdir
field if you only want files in the directory, not folders.or combine the last two lines:
For a list of folders in the directory excluding . and ..
From this point, you should be able to throw the code in a nested for loop, and continue searching each subfolder until your dirnames returns an empty cell for each subdirectory.
Update: Given that this post is quite old, and I've modified this utility a lot for my own use during that time, I thought I should post a new version. My newest code can be found on The MathWorks File Exchange:
dirPlus.m
. You can also get the source from GitHub.I made a number of improvements. It now gives you options to prepend the full path or return just the file name (incorporated from Doresoom and Oz Radiano) and apply a regular expression pattern to the file names (incorporated from Peter D). In addition, I added the ability to apply a validation function to each file, allowing you to select them based on criteria other than just their names (i.e. file size, content, creation date, etc.).
NOTE: In newer versions of MATLAB (R2016b and later), the
dir
function has recursive search capabilities! So you can do this to get a list of all*.m
files in all subfolders of the current folder:Old code: (for posterity)
Here's a function that searches recursively through all subdirectories of a given directory, collecting a list of all file names it finds:
After saving the above function somewhere on your MATLAB path, you can call it in the following way:
This is a handy function for getting filenames, with the specified format (usually
.mat
) in a root folder!In your case, you can use the following snippet :)
I used the code mentioned in this great answer and expanded it to support 2 additional parameters which I needed in my case. The parameters are file extensions to filter on and a flag indicating whether to concatenate the full path to the name of the file or not.
I hope it is clear enough and someone will finds it beneficial.
Example for running the code:
This answer does not directly answer the question but may be a good solution outside of the box.
I upvoted gnovice's solution, but want to offer another solution: Use the system dependent command of your operating system:
Positive:
*.wav
files.Negative:
With little modification but almost similar approach to get the full file path of each sub folder