file read by number and perform operation
조회 수: 1 (최근 30일)
이전 댓글 표시
Dear all,
from a list of txt(ascii) file text0, text1, text2, text3, text4, ... which is an alternace of data and background measurements.
I would like to subtract file by pairs:
1 - 0,
3 - 2,
5 - 4,
7 - 6
... etc.
All files are located into 1 folder, so my first attempt was as follow:
myFolder = 'C:\Users\...........';
filePattern = fullfile(myFolder, '*.txt')
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
datafile1 = theFiles(k).name;
fullFileName = fullfile(myFolder, datafile1);
fprintf(1, 'Now reading %s\ data n ', fullFileName);
rawdata = dlmread(fullFileName,'\t','C4..AYP4');
a = k + 1;
datafile2 = theFiles(a).name;
backgroundfile = fullfile(myFolder, datafile2);
xaxis = dlmread(backgroundfile,'\t','C2..AYP2');
background = dlmread(backgroundfile,'\t','C4..AYP4');
fprintf(1, 'Now reading %s background \n', backgroundfile);
% do whatever with data
C = bsxfun(@minus, rawdata, background);
end
files are liste in a structure as expected with their names, however, as you can see the problem is that the loop will not do the subration by pair (1-0, then 3-2, 5-4, etc.)
in fact i got something like this:
(process of 0 data) minus (process of 1 background), then (process of 1 data)minus (process of 2 background), etc.
how should I correct the loop to work by pair of file properly ?
Thank you for your time,
댓글 수: 0
답변 (1개)
Walter Roberson
2016년 4월 22일
If you change
fprintf(1, 'Now reading %s\n', fullFileName);
to
fprintf(1, 'Now reading %s for foreground\n', fullFileName);
and
fprintf(1, 'Now reading %s\n', backgroundfile);
to
fprintf(1, 'Now reading %s for background\n', backgroundfile);
then I think you will find that you are reading the files the way you asked.
댓글 수: 3
Walter Roberson
2016년 4월 22일
Just reverse the order then
myFolder = 'C:\Users\...........';
filePattern = fullfile(myFolder, '*.txt')
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
datafile1 = theFiles(k).name;
backgroundfile = fullfile(myFolder, datafile1);
fprintf(1, 'Now reading %s\ background n ', );
background = dlmread(backgroundfile,'\t','C4..AYP4');
a = k + 1;
datafile2 = theFiles(a).name;
fullFileName = fullfile(myFolder, datafile2);
xaxis = dlmread(fullFileName,'\t','C2..AYP2');
rawdata = dlmread(fullFileName,'\t','C4..AYP4');
fprintf(1, 'Now reading %s data \n', backgroundfile);
% do whatever with data
C = bsxfun(@minus, rawdata, background);
end
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!