MATLAB: How to search a file in MATLAB 7.9 (R2009b)

dosfilefindMATLABsubfolders

I would like to look for a particular file in a subdirectory. I only know the root level directory which contains multiple subdirectories. I would like to know how to look for this file recursively and find whcih subdirectory it resides in.

Best Answer

To search for file that are not on MATLAB path, use the DIR command in MS-DOS. For this, use the CD command to the top level directory, and use the DIR command within the DOS command from MATLAB this way:
 
[a,b] = dos('dir /s /b <myfile>*');
filenames = textscan(b,'%s','delimiter',char(10));
filenames = filenames{1};
Here the /s option looks inside subfolder and <myfile> is the file that you are searching for. The /b option ensures that the output is in bare format.
To read more about the DOS command, visit here:
<http://www.computerhope.com/dirhlp.htm>