问题
I would like to list available directories and text files in specific directories recursively in Matlab command window (and ultimately in an m-file). I know commands like ls are available, but I would like to know the text files available in a string or vector before I recursively read each text file in the following file system structure:
master (contains A and B, all directories)
A contains A1 and A2 (all directories)
A1 contains A11, A12, A13, A14 (all directories)
A11 contains 1.txt, 2.txt, ...
Would be great to hear some feedback! Thanks in advance!
回答1:
You can use DIR recursively.
The output of dir is a structure where the first and the second element are current and parent directory, respectively, and the rest are the contents of the folder listed. Step through those. If it's a directory (dirOutput.isdir == 1), call dir on it. If it's a file, add it to the list.
If you don't want to code this yourself, have a look around the Matlab File Exchange. Here's one of many solutions.
来源:https://stackoverflow.com/questions/3356624/listing-available-files-and-directories-in-matlab