필터 지우기
필터 지우기

How to load in a large number of data files with numbered names that skip numbers

조회 수: 19 (최근 30일)
I have about 600 data files that I need to extract a few variables from and manipulate. They are titled "experiment-n.m" where n is a number ranging from 1-900. I already have the code to manipulate them, but I was hoping to find an easier way to read in these files than individually. The hard part is that the numbering is rather sporadic, and can be experiment-200 than skip to experiment-697.
I was considering making a for loop along the lines of:
for step = 1:900
str = sprintf('experiment-%d',step)
force = load(str,magf);
area = load(str, fn_area);
end
I have yet to test this code so there may be some errors in it. However, this would not work with the data due to skipping experiment numbers (like I said, the files will be experiments 1-100, skip to 150, skip to 257, etc.)
Is there some way to have this code process an error received when the file name is not found and make it skip to the next file name?
Thank you.

답변 (2개)

Image Analyst
Image Analyst 2014년 12월 6일
See the FAQ for how to use dir() to read files that actually exist:
myFolder = pwd; % or whatever....
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, 'experiment*.*');
baseFileNames = dir(filePattern);
for k = 1:length(baseFileNames)
% Get this filename.
baseFileName = baseFileNames(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Load file into a structure.
s = load(fullFileName);
% Extract fields from structure into individual arrays.
force(k) = s.magf;
area(k) = s.fn_area;
end
  댓글 수: 1
Image Analyst
Image Analyst 2014년 12월 6일
Note, my solution
  1. does not overwrite the reserved keyword "path",
  2. it also checks that the folder actually exists,
  3. it uses fullfile (the recommended way to construct full pathnames),
  4. it only calls load() once instead of twice, and
  5. it gives you arrays for force and area (rather than overwriting scalars every iteration).

댓글을 달려면 로그인하십시오.


Matt Raum
Matt Raum 2014년 12월 6일
How about something like this:
path = 'C:\yourdir\';
filelist = dir([path 'experiment*']);
for k=1:numel(filelist)
fname = [path filelist(k).name];
force = load(fname,magf);
area = load(fname,fn_area);
end
I'm guessing that the "load" function your calling is your own file reader which takes the "magf" and "fn_area" arguments your passing?
In any case, the code above should allow you to only read the files that actually exist.

카테고리

Help CenterFile Exchange에서 Debugging and Analysis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by