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 中查找有关 Subplots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!