2

I have a directory at '../../My_Dir' relative to the Matlab working directory. This directory itself has several sub-directories in it. Each sub-directory then has several files in it.

I want to create two-dimensional array, or a matrix, of strings. Each row represents one of the sub-directories. The first column in the row is the full path to the sub-directory itself, and the other columns are the full paths to the files within that sub-directory.

Can anyone show me some code that will help me to implement this? Thank you!

herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
Karnivaurus
  • 22,823
  • 57
  • 147
  • 247
  • 1
    Duplicate of [this](http://stackoverflow.com/questions/2652630/how-to-get-all-files-under-a-specific-directory-in-matlab) question. – Autonomous Mar 13 '14 at 16:29

3 Answers3

0

You can first get all the sub-folders by

d = dir(pathFolder);
isub = [d(:).isdir];
subFolders = {d(isub).name}';

Note, you further need to remove . and .. from it:

subFolders(ismember(subFolders,{'.','..'})) = [];

And then get files in each of them by using (from this post):

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
Community
  • 1
  • 1
herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
  • Thanks - this is a help, but I am now getting the following error when I loop through subFolders, and call dir() on each element: `Error using dir. Function is not defined for 'cell' inputs` – Karnivaurus Mar 13 '14 at 17:47
  • @Karnivaurus Try use something like `dir(dirName{1,1})` instead. – herohuyongtao Mar 13 '14 at 17:50
0

To get the folders FULL PATH:

d = dir(baseDir);
d(~[d.isdir])= []; %Remove all non directories.
names = setdiff({d.name},{'.','..'});

filesFullPath = names;

for i=1:size(names,2)
    filesFullPath{i} = fullfile(baseDir,names{1,i});
end

Matlab... "thanks" for this s...
Pedro77
  • 5,176
  • 7
  • 61
  • 91
0

As an example, let's say I have three subfolders in 'My_Dir' called 'A' (containing 'a1.txt' and 'a2.txt'), 'B' (containing 'b1.txt'), and 'C' (containing 'c1.txt', 'c2.txt', and 'c3.txt'). This will illustrate how to handle a case with different numbers of files in each subfolder...

For MATLAB versions R2016b and later, the dir function supports recursive searching, allowing us to collect a list of files like so:

dirData = dir('My_Dir\*\*.*');        % Get structure of folder contents
dirData = dirData(~[dirData.isdir]);  % Omit folders (keep only files)
fileList = fullfile({dirData.folder}.', {dirData.name}.');  % Get full file paths

fileList =

  6×1 cell array

    '...\My_Dir\A\a1.txt'
    '...\My_Dir\A\a2.txt'
    '...\My_Dir\B\b1.txt'
    '...\My_Dir\C\c1.txt'
    '...\My_Dir\C\c2.txt'
    '...\My_Dir\C\c3.txt'

As an alternative, in particular for earlier versions, this can be done using a utility I posted to the MathWorks File Exchange: dirPlus. It can be used as follows:

dirData = dirPlus('My_Dir', 'Struct', true, 'Depth', 1);
fileList = fullfile({dirData.folder}.', {dirData.name}.');

Now we can format fileList in the way you specified above. First we can use unique to get a list of unique subfolders and an index. That index can then be used with mat2cell and diff to break fileList up by subfolder into a second level of cell array encapsulation:

[dirList, index] = unique({dirData.folder}.');
outData = [dirList mat2cell(fileList, diff([index; numel(fileList)+1]))]

outData =

  3×2 cell array

    '...\My_Dir\A'    {2×1 cell}
    '...\My_Dir\B'    {1×1 cell}
    '...\My_Dir\C'    {3×1 cell}
gnovice
  • 125,304
  • 15
  • 256
  • 359