MATLAB: How to run the WHAT command recursively in MATLAB R2007b

MATLABpathsemicolonseparator

I want the output of WHAT on a folder and all its subfolders. Currently WHAT only provides the output for the specified folder.

Best Answer

There is no single MATLAB command that will give you all the MATLAB related files in a directory and its subdirectories, but it is possible to process the
output of the GENPATH command to produce the desired result.
The MATLAB command GENPATH returns a string consisting of a folder and all its subfolders separated by the PATHSEP:
mkdir a
mkdir a\b
p = genpath('a')
produces the output:
p =
a;a\b;
Running REGEXP on p with the 'split' option produces a cell array of
the individual subdirectories:
s = regexp(p, pathsep, 'split')
which produces the output:
s =
'a' 'a\b' ''
(Note that there is an empty string at the end because of that final trailing semicolon in p)
We can now loop through the cell array s and run the WHAT command on each folder:
for k=1:length(s)-1 % subtract one to skip over that last empty string
what(s{k})
end
The file recursive_what.m combines the above steps into a MATLAB function.
Caveats: the 'split' option to REGEXP is new in 2007b.
You can get more information about GENPATH, PATHSEP and REGEXP from inside MATLAB by typing
doc genpath
or
doc pathsep
or
doc regexp
or by going to the following URLs
Related Question