I need to get all those files under D:\dic
and loop over them to further process individually.
Does MATLAB support this kind of operations?
It can be done in other scripts like PHP,Python...
To search for multiple files, use wildcards in the file name. For example, dir *. txt lists all files with a txt extension in the current folder. To search through folders and subfolders on the path recursively, use wildcards in the path name.
To open the Find Files tool, on the Home tab, in the File section, click Find Files. Enter your search criteria in the dialog box that opens. Use the Look in menu to specify the folders you want to search. Select Entire MATLAB Path to search all folders on the MATLAB search path.
Direct link to this commentS = dir(...); % all of the names are in the structure S anyway. N = {S.name}; % but if you really want a cell array of names, do this.
Update: 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 The MathWorks File Exchange: dirPlus.m
. You can also get the source from GitHub.
I made a number of improvements. It now gives you options to prepend the full path or return just the file name (incorporated from Doresoom and Oz Radiano) and apply a regular expression pattern to the file names (incorporated from Peter D). 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.).
NOTE: In newer versions of MATLAB (R2016b and later), the dir
function has recursive search capabilities! So you can do this to get a list of all *.m
files in all subfolders of the current folder:
dirData = dir('**/*.m');
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 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
After saving the above function somewhere on your MATLAB path, you can call it in the following way:
fileList = getAllFiles('D:\dic');
You're looking for dir to return the directory contents.
To loop over the results, you can simply do the following:
dirlist = dir('.'); for i = 1:length(dirlist) dirlist(i) end
This should give you output in the following format, e.g.:
name: 'my_file' date: '01-Jan-2010 12:00:00' bytes: 56 isdir: 0 datenum: []
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With