Reading all the files in a folder for processing
240 views (last 30 days)
Show older comments
Bibek Dhami
on 21 Jul 2020
Commented: Image Analyst
on 3 Jun 2022
I have to process 400 files within a folder and I know how to process each of them individually for the analysis part. But I don't want to choose them manually and do the processing. So I looked for matlab code and found some, but I could only find the structure of file not the data is loaded. Can someone please guide me on this? Here is my code so far.
projectdir = 'C:\Desktop\Research\EXPERIMENT\Spectra';
dinfo = dir(fullfile(projectdir));
dinfo([dinfo.isdir]) = []; %get rid of all directories including . and ..
nfiles = length(dinfo);
for j = 1 : nfiles
filename = fullfile(projectdir, dinfo(j).name);
f1 = fopen(filename, 'r');
...
fclose(f1);
end
1 Comment
Rik
on 21 Jul 2020
Apart from the missing formatting, the code looks fine. You edited away the part from which we could tell if you are overwriting the results, but otherwise I don't see any major issues.
What exactly is your issue?
Accepted Answer
Image Analyst
on 21 Jul 2020
Looks much like what is in the FAQ, which has code for three scenarios:
Maybe you want to check that out. What sort of guidance do you need beyond that? We don't know what "the structure of file not the data is loaded." means. Please explain that phrase.
1 Comment
Image Analyst
on 22 Jul 2020
Regarding your "Answer" below...your script does not get file listings of any unix executables, it gets only one file : "'wavelength_calbr.dat'". So the FAQ should help you, you just need to use it, like this:
% Specify the folder where the files live.
myFolder = pwd; % or 'C:\Users\yourUserName\Documents\';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', myFolder);
uiwait(warndlg(errorMessage));
myFolder = uigetdir(); % Ask for a new one.
if myFolder == 0
% User clicked Cancel
return;
end
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.exe'); % Change to whatever pattern you need.
% filePattern = fullfile(myFolder, '**/*.exe'); % To look in subfolders too, add **/.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf('Now processing %s\n', fullFileName);
% Now do whatever you want with this file name,
% such as running the program with system() or whatever.
end
More Answers (2)
MUHAMMAD Shoaib
on 3 Jun 2022
I want to convert my wave files to their spectrum (images). For this purpose I want to covert whole folder which contains 96 files and then save their spectrum images, any one help me plzzz
See Also
Categories
Find more on Startup and Shutdown 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!