Alternative to using "unix(sprintf('ls %s/*/*/*.out',pathname))" ?
Show older comments
I am using the following command to gather a list of files with the same extension (*.out) down a couple directories:
[~,List] = unix(sprintf('ls %s/*/*/*.out',pathname));
This works for what I am doing when I run from Matlab, but as soon as I make a standalone executable for any .m file that has a "unix('ls something')" it will not work. In the past I was able to get around this by using Matlab's "dir" function, but that only works if I have the following situation:
DirResult = dir(sprintf('%s/*.out',pathname));
not for
DirResult = dir(sprintf('%s/*/*.out',pathname));
And I can't use:
List = ls(sprintf('%s/*/*/*.out',pathname));
because if the path with wild cards does not exist, then I get an error (instead of allowing me to deal with the fact that it doesn't exist in a more convenient way).
Any ideas or am I doing something "silly"?
Accepted Answer
More Answers (2)
Wannabegeek
on 8 Apr 2012
I found your question after reading a great answer you gave about fft in MATLAB...thanks, so to attempt a return on the favor I have some ideas about an answer for you.
I run MATLAB in a linux environment myself and just use:
dir(path_with_bash_wildcards)
I don't see any reason to use 'unix' with 'dir' unless I've missed something.
!find root_path -type f -iname *.out > file_list
file_list = load('file_list.txt');
hope that helps... wbg
Walter Roberson
on 8 Apr 2012
1 vote
Use dir(pathname) and check the isdir() field to find the first level of directories under the path. For each of those directories, dir() [pathname '/' directoryname1] and check the isdir() field, to find the second level of directories. And in each resulting second level directory, dir() [pathname '/' directoryname1 '/' directoryname2 '/*.out') -- and just to be safe, use isdir() and this time throw away any of the *.out that happened to be directories.
There is a File Exchange contribution that will do recursive matches and process each match.
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!