Clear Filters
Clear Filters

Reading all the files in a folder for processing

240 views (last 30 days)
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
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?

Sign in to comment.

Accepted Answer

Image Analyst
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
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

Sign in to comment.

More Answers (2)

Bibek Dhami
Bibek Dhami on 22 Jul 2020
Let me make it clear.
I have several files of the format ( unix executable) in a folder. I need to process each of these files with the same method. Rather than doing each of them manually I want to process them with this code. On doing if I select a particular file from the loop, it only show me the file structure rather than loading the data within the file.

MUHAMMAD Shoaib
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

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!