file read by number and perform operation

5 ビュー (過去 30 日間)
Taro Ichimura
Taro Ichimura 2016 年 4 月 22 日
コメント済み: Walter Roberson 2016 年 4 月 26 日
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,

回答 (1 件)

Walter Roberson
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
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
Walter Roberson
Walter Roberson 2016 年 4 月 26 日
You can use
for k = 1 : 2 : length(theFiles)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by