Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Update:</strong> Given that this post is quite old, and I've modified this utility a lot for my own use during that time, I thought I should post a new version. My newest code can be found on <a href="http://www.mathworks.com/matlabcentral/fileexchange/?s_tid=gn_mlc_fx" rel="noreferrer">The MathWorks File Exchange</a>: <a href="http://www.mathworks.com/matlabcentral/fileexchange/60716-dirPlus" rel="noreferrer"><code>dirPlus.m</code></a>. You can also get the source from <a href="https://github.com/kpeaton/dirPlus" rel="noreferrer">GitHub</a>.</p> <p>I made a number of improvements. It now gives you options to prepend the full path or return just the file name (incorporated from <a href="https://stackoverflow.com/questions/2652630/how-to-get-all-files-under-a-specific-directory-in-matlab/2654459#comment2671016_2654459">Doresoom</a> and <a href="https://stackoverflow.com/a/26449095/52738">Oz Radiano</a>) and apply a regular expression pattern to the file names (incorporated from <a href="https://stackoverflow.com/questions/2652630/how-to-get-all-files-under-a-specific-directory-in-matlab/2654459#comment15362987_2654459">Peter D</a>). In addition, I added the ability to apply a validation function to each file, allowing you to select them based on criteria other than just their names (i.e. file size, content, creation date, etc.).</p> <hr> <p><strong>NOTE:</strong> In newer versions of MATLAB (R2016b and later), the <a href="https://www.mathworks.com/help/matlab/ref/dir.html" rel="noreferrer"><code>dir</code></a> function has recursive search capabilities! So you can do this to get a list of all <code>*.m</code> files in all subfolders of the current folder:</p> <pre><code>dirData = dir('**/*.m'); </code></pre> <hr> <h2>Old code: (for posterity)</h2> <p>Here's a function that searches recursively through all subdirectories of a given directory, collecting a list of all file names it finds:</p> <pre><code>function fileList = getAllFiles(dirName) dirData = dir(dirName); %# Get the data for the current directory dirIndex = [dirData.isdir]; %# Find the index for directories fileList = {dirData(~dirIndex).name}'; %'# Get a list of the files if ~isempty(fileList) fileList = cellfun(@(x) fullfile(dirName,x),... %# Prepend path to files fileList,'UniformOutput',false); end subDirs = {dirData(dirIndex).name}; %# Get a list of the subdirectories validIndex = ~ismember(subDirs,{'.','..'}); %# Find index of subdirectories %# that are not '.' or '..' for iDir = find(validIndex) %# Loop over valid subdirectories nextDir = fullfile(dirName,subDirs{iDir}); %# Get the subdirectory path fileList = [fileList; getAllFiles(nextDir)]; %# Recursively call getAllFiles end end </code></pre> <p>After saving the above function somewhere on your MATLAB path, you can call it in the following way:</p> <pre><code>fileList = getAllFiles('D:\dic'); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload