Reading all the files in a folder for processing

306 vues (au cours des 30 derniers jours)
Bibek Dhami
Bibek Dhami le 21 Juil 2020
Commenté : Image Analyst le 3 Juin 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 commentaire
Rik
Rik le 21 Juil 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?

Connectez-vous pour commenter.

Réponse acceptée

Image Analyst
Image Analyst le 21 Juil 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 commentaire
Image Analyst
Image Analyst le 22 Juil 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

Connectez-vous pour commenter.

Plus de réponses (2)

Bibek Dhami
Bibek Dhami le 22 Juil 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 le 3 Juin 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

Catégories

En savoir plus sur Startup and Shutdown dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by