Read files from multiple folders

75 views (last 30 days)
Lets say I have some 100 .dat files in each subfolder A, B , C , D that is placed inside the folder named Data. Here, I would like to perform operation in a one go, rather than running sperately for each subfolder. Is there a way to do this, does it requires parallel processing for this task ??
For e.g.
read data from subfolder A
for i = 1:1:100
some operation
read data from subfolder B
for i = 1:1:100
some operation

Accepted Answer

Image Analyst
Image Analyst on 13 Apr 2022
Use a fileDatastore:
topLevelFolder = pwd; % folder in which your images exists
filePattern = fullfile(topLevelFolder, '*.dat');
ds = fileDatastore(filePattern, 'ReadFcn', @readmatrix) % Creates a datastore for all images in your folder
allFileNames = {ds.Files{:}}'
for k = 1 : numel(allFileNames)
thisFileName = allFileNames{k};
fprintf('Processing %s.\n', thisFileName);
% Do something with it.
If you have the Parallel Processing Toolbox you can use parfor instead of for.
Turbulence Analysis
Turbulence Analysis on 13 Apr 2022
It's working perfectly with two stars !!

Sign in to comment.

More Answers (0)


Find more on Scope Variables and Generate Names in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by