MATLAB: How to get averaged value from multiple lines of txt file.

get value from files

Hello,
I have saved some .txt files (trace file, example as attached, like hel1_1, hel1_2….hel1_100) in one folder.
Each file contain about 100 lines (Frames numbers) and 5 column (Col1, Frame number; Col2, CH1; Col3, BGND1; Col4, CH2; Col5, BGND2).
Here, My question is: 1, I want to get the value for each line use the function
(CH2-BGND2) /((CH2-BGND2)+(CH1-BGND1))
, which also means
(col(4)-col(5))/((col(2)-col(3))+(col(4)-col(5))).
2, I want to average values of the first 40 frames (or any continuous frames) from the calculated values of step 1, get the standard-error of them. For each saved trace file.
3, I want to write the averaged FRET value and stand-error from each file into a new .txt file with 3 column. Column 1, name of the trace file; Column 2, averaged FRET value; Column 3, stand-error. For all the .txt files in the folder.
Thank you for your time to give any suggestion.
Best,

Best Answer

d=dir('hel1*.txt'); % list of files
res=zeros(length(d),3); % room for the results
for i=1:length(d)
x=textread(d(i).name,'headerlines',1); % read it
res(i,1)=(x(:,4)-x(:,5))./((x(:,2)-x(:,3))+(x(:,4)-x(:,5))); % the function
That's the whole file content; the thing about 40 or the contiguous frames is a little nebulous what you mean, precisely, as the example file shows the frame value as being incremented by one. To do that, simply define the range you actually want to operate over and replace the range ':' with that. First 40 would simply be 1:40 for example.
2-3.
res(i,2)=mean(res(i,1));
res(i,3)=std(res(i,1));
end
dlmwrite('youroutputfile.txt',res,'delimiter',' ','precision','%.4f')