file read by number and perform operation

1 view (last 30 days)
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,

Answers (1)

Walter Roberson
Walter Roberson on 22 Apr 2016
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 Comments
Walter Roberson
Walter Roberson on 22 Apr 2016
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

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!