%% Import all experimental Data

% uncorrected
[files4,pathes4]=uigetfile('*.new','choose','Multiselect','on')
for i=1:size(files4,2)

    uFRET(1,i).counts=importdata(files4{1,i});
    uFRET(1,i).names=files4(1,i);
end;

% and (machine) corrected
[files4,pathes4]=uigetfile('*.dat','choose','Multiselect','on')
for i=1:size(files4,2)

    cFRET(2,i).counts=importdata(files4{1,i});
    cFRET(2,i).names=files4(1,i);
end;

%% Insert relevant Information (photocounts and filenames) into the structure

for i=1:127
oxyFRET(1,i).counts=uFRET(4,i-128).counts(:,2:3);
oxyFRET(1,i).names=files4{1,i-128};
end
%% calculate Inensities for various time windows 

for i=86:127
oxyFRET(1,i).ints10=zeros(size(oxyFRET(1,i).counts,1)-9,2);
oxyFRET(1,i).ints20=zeros(size(oxyFRET(1,i).counts,1)-19,2);
oxyFRET(1,i).ints50=zeros(size(oxyFRET(1,i).counts,1)-49,2);
oxyFRET(1,i).ints100=zeros(size(oxyFRET(1,i).counts,1)-99,2);
oxyFRET(1,i).ints10=timewindow(oxyFRET(1,i).counts,10);
oxyFRET(1,i).ints20=timewindow(oxyFRET(1,i).counts,20);
oxyFRET(1,i).ints50=timewindow(oxyFRET(1,i).counts,50);
oxyFRET(1,i).ints100=timewindow(oxyFRET(1,i).counts,100);
end

%% Raw-efficiencies (no correction considered)
for i=86:127
oxyFRET(1,i).eff10=zeros(size(oxyFRET(1,i).ints10),2);
oxyFRET(1,i).eff20=zeros(size(oxyFRET(1,i).ints20),2);
oxyFRET(1,i).eff50=zeros(size(oxyFRET(1,i).ints50),2);
oxyFRET(1,i).eff100=zeros(size(oxyFRET(1,i).ints100),2);
oxyFRET(1,i).eff10=efficiency(oxyFRET(1,i).ints10);
oxyFRET(1,i).eff20=efficiency(oxyFRET(1,i).ints20);
oxyFRET(1,i).eff50=efficiency(oxyFRET(1,i).ints50);
oxyFRET(1,i).eff100=efficiency(oxyFRET(1,i).ints100);
end

%% end of active phase through length of corrected timeseries
for i=86:127
oxyFRET(1,i).A_bleached=size(cFRET(2,i-85).counts,1);
end
%% The point in time where the Donor is bleached has to be pinned by hand 
%% in the plot, then exported and inserted into the structure
clear l k
for l=4:5
for k=1:1:10
figure(l)
subplot(10,1,k);
plot(oxyFRET(1,-10+10*l+k).ints20(:,1:2));
set(gca,'Position',[0.003 1-0.098*k 0.98 0.09])
set(gca,'XTickLabel',[],'ytick',[],...
'TickLength',[0.004,0.001],...
'XMinorGrid','on','XAxisLocation','top','XMinorGrid','off','XAxisLocation','top');
pause(0.5)
end
end
%%

%%
for l=1:10
Dbleached1(1,l)=cursor_info(1,11-l).DataIndex;
end;
for l=1:10
Dbleached(1,l+10)=cursor_info(1,11-l).DataIndex;
end;
for l=1:10
Dbleached(1,l+20)=cursor_info(1,11-l).DataIndex;
end;
for l=1:10
Dbleached(1,l+30)=cursor_info(1,11-l).DataIndex;
end;
for l=1:7
Dbleached(1,l+40)=cursor_info(1,8-l).DataIndex;
end;


%% With the two timestamps the background and crosstalk can be calculated
%% The Machinegamma is set to 1 as in thte experimental Data

for k=86:127
oxyFRET(1,k).D_bleached=Dbleached(k-80,1);
tD=oxyFRET(1,k).D_bleached;
tA=oxyFRET(1,k).A_bleached;
IDsolo=sum(oxyFRET(1,k).counts(tA:tD,1));
IAsolo=sum(oxyFRET(1,k).counts(tA:tD,2));
length=size(oxyFRET(1,k).counts,1);
tdark=length-tD;

oxyFRET(1,k).bgsum=sum(oxyFRET(1,k).counts(tD:end,:));
bg=nuFRET(1,k).bgsum/tdark;

oxyFRET(1,k).bg=bg;
oxyFRET(1,k).cross=(IAsolo-bg(1,2))/IDsolo;
oxyFRET(1,k).machine_gamma=1;
clear tA tD IDsolo IAsolo length tdark bg
k
end

for m=1:127
%rauschen(m,:)=nuFRET(1,m).bg
%crosstalks(m,:)=nuFRET(1,m).cross
%names(m)=strvcat(nuFRET(1,m).names)
%names(m)={nuFRET(1,m).names}
newnames(m,2)={int2str(a(m))};
m
end
names=sortrows(newnames)


