Reading all the files in a folder for processing
조회 수: 240 (최근 30일)
이전 댓글 표시
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
Rik
2020년 7월 21일
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?
채택된 답변
Image Analyst
2020년 7월 21일
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
Image Analyst
2020년 7월 22일
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
추가 답변 (2개)
MUHAMMAD Shoaib
2022년 6월 3일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!