How to get all files under a specific directory in MATLAB?

Answer

Here's a function that searches recursively through all subdirectories of a given directory, collecting a list of all file names it finds:

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 filesif~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
  endend

After saving the above function somewhere on your MATLAB path, you can call it in the following way:

fileList = getAllFiles('D:\dic');

All matlab Questions

Ask your interview questions on matlab

Write Your comment or Questions if you want the answers on matlab from matlab Experts
Name* :
Email Id* :
Mob no* :
Question
Or
Comment* :
 





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---