Alternative to using "unix(sprintf('ls %s/*/*/*.o​ut',pathna​me))" ?

4 visualizzazioni (ultimi 30 giorni)
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"?

Risposta accettata

Dr. Seis
Dr. Seis il 9 Apr 2012
I ran a test...
Running this from Matlab:
[~,List] = unix(sprintf('ls %s/*/*/*.out',pathname));
And then doing some manipulation to List to strip out the EOL and put the list of files into a cell array (e.g. "ListCell")... I get:
numel(List) equal to 35200 and numel(ListCell) = 400.
When I compile the code and run as a standalone executable I get:
numel(List) equal to 39208 and numel(ListCell) = 400.
So... just over 4000 extra characters in "List" from the compiled version than the Matlab version. You would think something like deblank or strtrim would help remove the extra characters... but it does nothing to help the situation. Displaying the result of:
ListCell{1}
at the command line appears to be exactly the same in either Matlab or the stand alone version. So does:
fprintf('>%s<\n',ListCell{1})
However, displaying the length of "ListCell{1}" gives a completely different result in Matlab than in the stand-alone version. Where are all the extra characters? I still have no idea.
Anyhow... changing ls to find in my UNIX call above did the trick. It was the solution that led to the least amount of other changes to my code.
Thanks for the suggestions.
  1 Commento
Walter Roberson
Walter Roberson il 9 Apr 2012
regexprep() can do the blank supression. Use anchor-to-line mode and replace ' +$' with ''

Accedi per commentare.

Più risposte (2)

Wannabegeek
Wannabegeek il 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
Walter Roberson il 8 Apr 2012
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.

Categorie

Scopri di più su File Operations in Help Center e File Exchange

Tag

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by