function newanalyzedata(reset,display,newname,resetanova)
global In1 Userinput Explags Expstreams Expstreamnums Exptime Condition ExpEvents compact T1acc perlag Expmiscflag

global T1perlag T2perlag Swapperlag Swapacc Totaltimes Badtime ThisT2acc person  NameT1perlag NameT2perlag NameSwapperlag numnames Nameperlag  NameT2perlagraw NameT1perlagcond     ThisT1accnocond T2perlagraw T1perlagcond ThisT1acc T2acc ITEMS
global order  T1bylet T1byletcor T1byletacc T2bylet T2byletcor T2byletacc Absoluteacc Allacc T2bindata T1bindata T2bindatacount T1bindatacount
global  T2anovadata  T2anovacount  T2anovagroup T1anovadata T1anovacount T1anovagroup ThisSwapacc T1list T2list T2perlagnotT1 T1perlagnotT2 T1perlag T1solo soloperlag
global NotT1perlag  NameT2perlagnotT1  NameNotT1perlag  ThisT2accnotT1

global T1perstrength T2perstrength T1sacc T2sacc perlagstrength Lag

addpath 'userdata'



% Exp 3

load parmfile

Lag = 0;

lags = 16;  %replace later
numtimes = size(Exptime,2)-2 ;%ignore the last 2 elements of the stream
if(reset)
    T1perlag = zeros(lags,1);

        T2perlag = zeros(lags,1);
    NotT1perlag = zeros(lags,1);
    Swapperlag = zeros(lags,1);
    perlag = zeros(lags,1);
    T2perlagraw =zeros(lags,1);
    T2perlagnotT1 =zeros(lags,1);
    T1perlagnotT2 =zeros(lags,1);
    T1perlagcond=   zeros(lags,1);
    T1perstrength = zeros(lags,3,3);
    T2perstrength = zeros(lags,3,3);
    perlagstrength = zeros(lags,3,3);
    start = 0;
    'RESETTTTTTING'
    ThisT2acc = 0;
    Badtime = 0;

    Totaltimes = zeros(1,numtimes-2);
    person = 0;

    T1bylet = zeros(size(ITEMS,2),1);
    T1byletcor = zeros(size(ITEMS,2),1);
    T2bylet = zeros(size(ITEMS,2),1); 
    T2byletcor = zeros(size(ITEMS,2),1);

T1solo  = 0;
soloperlag = 0;
    
    
    Absoluteacc = zeros(lags,1)
    T2bindata = zeros(1,lags);
    T1bindata = zeros(1,lags);
    T1bindatacount = zeros(lags,1);
    T2bindatacount = zeros(lags,1);

    T1list = 0;
    T2list = 0;

    
if(resetanova)
    T2anovadata = 0;
    T2anovagroup = 0;
    T2anovacount = 0;
    T1anovadata = 0;
    T1anovagroup = 0;
    T1anovacount = 0;
end  
end

    
    
compact = 0;
%used to create data for individuals
if(newname)
    NameT1perlag= T1perlag;
    NameT2perlag= T2perlag;    
    NameT2perlagnotT1=   T2perlagnotT1;
    NameNotT1perlag = NotT1perlag;

    
    NameSwapperlag = Swapperlag;
    Nameperlag = perlag;
    
    NameT1perlagcond = T1perlagcond;

%used for the T2 not T1 condition

%    NameT2perlag= T2perlagnotT1;


%     for(i = 1:lags)
%         NameT1perlagcond(i) =  perlag(i)-T1perlag(i);
%     end
    
    NameT2perlagraw = T2perlagraw;
    person  = person + 1;
end

T2item = 0;
for(i= 1: size(ExpEvents,1))   
        
    [x T1pos] = min(abs(Expstreams(i,:)-1));
    T1set = ExpEvents(i,T1pos,2);
    T1item = ITEMS(T1set,Expstreamnums(i,T1pos));   
    T1list(length(T1list)+1) = T1item;
    [x T2pos] = min(abs(Expstreams(i,:)-2));
    
    compact(i,3) = T1item;
    
    
    
    
        if(Userinput(i,3) > 13)        %we have to trim some
            for(count = 1:size(Userinput,2))
                t = Userinput(i,count);
                if(t>97)
                    t = t - 32;
                end
                if(t ~= T1item & t ~=T2item)
                    for(count2 = count+1:size(Userinput,2))
                        Userinput(i,count2-1) = Userinput(i,count2);
                        
                    end
                end
            end
        end
        
       compact(i,5) = Userinput(i,1);
        if(compact(i,5) > 96)
            compact(i,5) =             compact(i,5) - 32;  %lowercase
        end
        if(size(Userinput,2) >1)
            compact(i,6) = Userinput(i,2);
        else
            compact(i,6) = 0;
        end
        
        if(compact(i,6) > 96)
            compact(i,6) =             compact(i,6) - 32;  %lowercase
        end               

    
    
    
    
        [x T2pos] = min(abs(Expstreams(i,:)-2));
    
    
    
    
    if(x ==0)  %there was a T2
    
        Lag = T2pos - T1pos;       
        T2set = ExpEvents(i,T2pos,2);
        T2item = ITEMS(T2set,Expstreamnums(i,T2pos));
        compact(i,1) = 1;   %there was a T2!
        T2list(length(T2list)+1) = T2item;
        a = Exptime(i,3:numtimes) - Exptime(i,2:numtimes-1);
%        mes = Totaltimes + a;
                
        if(max(a) > .08 | min(a) < .03)
            compact(i,1) = 0;
            Badtime = Badtime + 1
        end
        
        compact(i,2) = Lag;

        compact(i,4) = T2item;       
                
    
        if(Expstreams(i,T1pos-1) == -1);
            compact(i,7) = 1;  %T1 - 1 is unmasked!!
        else
            compact(i,7) = 0;
        end
%ORTHOPAIRS(2,1,1) = 20;  %2 = z
%ORTHOPAIRS(2,1,3) = 1;  %4 = A
%ORTHOPAIRS(2,1,4) = 15;  %5 = s
%ORTHOPAIRS(2,1,5) = 7;  %6 = G
%ORTHOPAIRS(2,1,6) = 16;  %7 = t
%ORTHOPAIRS(2,1,7) = 2;  %8 = B
        compact(i,8) = 0;
        for(ord = 1:size(order,1))
            if(compact(i,3) == order(ord,1) & compact(i,4) == order(ord,2))
                        compact(i,8) = 1;
             elseif  (compact(i,4) == order(ord,1) & compact(i,3) == order(ord,2))
                        compact(i,8) = -1;
                        
          end
      end
        if(i <= length(Expmiscflag));           
            compact(i,9) = Expmiscflag(i);  %where was the prime?!
        end
    else
        compact(i,14) = 0;
    end
    
    %5 and 6 are user input,
    %7 is T1-1 blank   %0 = no blank, 1 = blank
    %8 is word expectancy
    %9 is Prime position
    %10 is T2 identity
    %90 = Z   83 = S
    
    %13 is easy/hard T1
    
    %14 is easy/hard T2
    
    
    if(compact(i,4) == 90 | compact(i,4) == 83)
        compact(i,10) = 1;
    else
        compact(i,10) = 0;
    end
    
    compact(i,11:12) = 0;
    if(Expstreams(i,T1pos+1) ==-1)
        compact(i,11) = 1;
    end
    if(Expstreams(i,T2pos+1) ==-1)
        compact(i,12) = 1;
    end
    compact(i,13:14) = 0;
%100 msec version
    easylets = ['A','H','N','Y','T','L'];
    hardlets = ['B','C','E','P','J','V'];
   
    %50 msec version
    easylets = ['A','K','T','Y','V','U'];
    hardlets = ['B','C','E','F','P','D'];
    
    
    if(min(abs(easylets-T1item)) == 0)
        compact(i,13) = 1;
    end
    if(min(abs(hardlets-T1item)) == 0)
        compact(i,13) = -1;
    end
    if(min(abs(easylets-T2item)) == 0)
        compact(i,14) = 1;
    end
    if(min(abs(hardlets-T2item)) == 0)
        compact(i,14) = -1;
    end

    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %%%%%%here's the big one!  this selects which trials will be processed.  
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    
    %NO T2
    if(compact(i,1) == 0)
        soloperlag = soloperlag + 1;
        if(T1item == compact(i,5) | T1item == compact(i,6))
                        T1solo = T1solo + 1;
                    end
                end
                
    
    if(compact(i,1) & compact(i,11)   >-1  &compact(i,12) >-1&  compact(i,8)   >-21  & compact(i,13) >-21 & compact(i,14) >-21) 
        thismarker =2;        
        perlag(Lag)   =  perlag(Lag) + 1;
            Lag;
            compact(i,13);
            compact(i,14);
        
            perlagstrength(Lag,compact(i,13)+2,compact(i,14)+2) =             perlagstrength(Lag,compact(i,13)+2,compact(i,14)+2) +1;
        
        %     if(compact(i,1) & compact(i,8)==1) % T1 -1 blank?
            if(T1item == compact(i,5) & T2item==compact(i,6))
                Absoluteacc(Lag) =   Absoluteacc(Lag) +1;
            end
            [x ind] = min(abs(ITEMS(T1set,:)-T1item));
            
            if (x ==0)
                whichlet = ind;
            end

            [x ind] = min(abs(ITEMS(T2set,:)-T2item));
            
            if (x ==0)
                whichlet2 = ind;
            end
            
            
            T1bylet(whichlet) = T1bylet(whichlet) + 1;
        
            T1anovacount =   T1anovacount  +1;
            T1anovagroup(T1anovacount,1) = Lag;
            T1anovagroup(T1anovacount,2) = thismarker;
            T1anovagroup(T1anovacount,3) = person;
            

%    T2 not T1
            if(T1item ~= compact(i,5) & T1item ~= compact(i,6))
               if(T2item == compact(i,5) | T2item == compact(i,6))
                    T2perlagnotT1(Lag) =   T2perlagnotT1(Lag)+ 1;   
                end
            end
            %T1 not T2
            if(T2item ~= compact(i,5) & T2item ~= compact(i,6))
               if(T1item == compact(i,5) | T1item == compact(i,6))
                    T1perlagnotT2(Lag) =   T1perlagnotT2(Lag)+ 1;   
                end
            end
            
            
            
            
            
            
        if(T1item == compact(i,5) | T1item == compact(i,6))

            T1anovadata(T1anovacount) = 1;          
            T1perlag(Lag) =       T1perlag(Lag)+ 1;            
            T1byletcor(whichlet) = T1byletcor(whichlet) + 1;
            T2bindatacount(Lag) = T2bindatacount(Lag) + 1;
           
            T2anovacount =             T2anovacount  +1;
            T2anovagroup(T2anovacount,1) = Lag;
            T2anovagroup(T2anovacount,2) = thismarker;
            T2anovagroup(T2anovacount,3) = person;
            
            T2bylet(whichlet2) =             T2bylet(whichlet2) +1;
           
            T1perstrength(Lag,compact(i,13)+2,compact(i,14)+2) =   T1perstrength(Lag,compact(i,13)+2,compact(i,14)+2) +1;
            
            if(T2item == compact(i,5) | T2item == compact(i,6))
%             if(compact(i,6) > 13)                 
                T2anovadata(T2anovacount,1) = 1;
                T2byletcor(whichlet2) =             T2byletcor(whichlet2) +1;
                T2bindata(T2bindatacount(Lag),Lag) = 1;
                T2perlag(Lag) =   T2perlag(Lag)+ 1;                
                if(T2item == compact(i,5))
                    Swapperlag(Lag) =  Swapperlag(Lag) +1;
                    T2anovadata(T2anovacount,2) = 1;
                else
                    T2anovadata(T2anovacount,2) = 0;
                end
                T2perstrength(Lag,compact(i,13)+2,compact(i,14)+2) =   T2perstrength(Lag,compact(i,13)+2,compact(i,14)+2) +1;
                
else
                
                T2bindata(T2bindatacount(Lag),Lag) = 0;
                T2anovadata(T2anovacount,1) = 0;
            end                
                
        else
             T1anovadata(T1anovacount) = 0;                     
                      NotT1perlag(Lag) =              NotT1perlag(Lag) +1 
    
         end
        
        
       if(T2item == compact(i,5) | T2item == compact(i,6))
           T2perlagraw(Lag) =            T2perlagraw(Lag) + 1;
          if(T1item == compact(i,5) | T1item == compact(i,6))
                T1perlagcond(Lag) =       T1perlagcond(Lag)+ 1;
            end
        end
       
    end        
end
   

lags
for( i = 1:lags)
    i;
    ThisT2acc(person,i) = (T2perlag(i)-NameT2perlag(i))/ (T1perlag(i)-NameT1perlag(i));

    %used for the T2 NOT T1 data
    %ThisT2acc(person,i) = (T2perlagnotT1(i)-NameT2perlag(i))/ ((perlag(i)-T1perlag(i)) - NameT1perlagcond(i));
    

    ThisT1acc(person,i) = (T1perlagcond(i)-NameT1perlagcond(i))/ (T2perlagraw(i)-NameT2perlagraw(i));
    ThisT1accnocond(person,i) = (T1perlag(i)-NameT1perlag(i))/ (perlag(i)-Nameperlag(i));
    ThisT2accnotT1(person,i) = (T2perlagnotT1(i)-NameT2perlagnotT1(i))/ (NotT1perlag(i)-NameNotT1perlag(i));

    
    Allacc(i) = Absoluteacc(i) /perlag(i);
    T1acc(i) = T1perlag(i) / perlag(i);  %non cond T1
%    T1acc(i) = T1perlagcond(i) /T2perlagraw(i);    %cond T1
%     T1acc(i) = T1perlagnotT2(i) / (perlag(i)-T2perlagraw(i));  %T1 NOT T2

    T2acc(i) = T2perlag(i)/T1perlag(i);     %cond T2
%    T2acc(i) = T2perlagraw(i)/perlag(i);    %noncondT2
%     T2acc(i) = T2perlagnotT1(i) / (perlag(i)-T1perlag(i));  %T2 NOT T1
 
    Swapacc(i) = Swapperlag(i) /T2perlag(i);
    ThisSwapacc(person,i) = (Swapperlag(i)-NameSwapperlag(i)) /(T2perlag(i)-NameT2perlag(i));
end
    


if(display)
    figure
    subplot(3,1,1);
    scatter([1:lags],T1acc(1:lags));
    subplot(3,1,2);
    scatter([1:lags],T2acc(1:lags));
    subplot(3,1,3);
    scatter([1:lags],Swapacc(1:lags));
end


%Stats:

meanperlag = 0;
stdperlag = 0;
stdT1perlag = 0;
meanT1perlag = 0;



persons = person;
for(lag = 1:size(ThisT2acc,2))
    for(ij = 1:3)
        for(ji = 1:3)

            T1sacc(lag,ij,ji) =    T1perstrength(lag,ij,ji) / perlagstrength(lag,ij,ji);

            T2sacc(lag,ij,ji) =    T2perstrength(lag,ij,ji) / T1perstrength(lag,ij,ji);
        end
    end
   
    
    useitems = 0;   %don't allow NAN's to invade!
    count = 0;
    for(i = 1:persons);
        if(ThisT2acc(i,lag) > - 1)
            count = count + 1;
            useitems(count) = i;

        end
    end
    
    if(useitems(1,1) > 0)
    meanperlag(lag) = mean(ThisT2acc(useitems,lag));

    meanT1perlag(lag) = mean(ThisT1accnocond(useitems,lag));
    stdT1perlag(lag) =  std(ThisT1accnocond(useitems,lag))/sqrt(count);
    stdperlag(lag) =  std(ThisT2acc(useitems,lag))/sqrt(count);

    meanswapperlag(lag) = mean(ThisSwapacc(useitems,lag));    
  
    stdswapperlag(lag) =  std(ThisSwapacc(useitems,lag))/sqrt(count);
    
end

end
%meanT1perlag
%meanperlag
%stdT1perlag
for(i = 1:length(T1bylet))
    T1byletacc(i) = T1byletcor(i)/T1bylet(i);
end
for(i = 1:length(T2bylet))
    T2byletacc(i) = T2byletcor(i)/T2bylet(i);
end


a = fopen('dataout.xl','w')

fprintf(a, 'ThisSwapacc'); fprintf(a, '\n');

for(i = 1:size(ThisSwapacc,1))
    fprintf(a,'%g, ',ThisSwapacc(i,:));
    fprintf(a, '\n');
end
fprintf(a, 'ThisT1'); fprintf(a, '\n');
for(i = 1:size(ThisT1accnocond,1))
    fprintf(a,'%g, ',ThisT1accnocond(i,:));
    fprintf(a, '\n');
end

fprintf(a, 'ThisT2'); fprintf(a, '\n');
for(i = 1:size(ThisT2accnotT1,1))
    fprintf(a,'%g, ',ThisT2accnotT1(i,:));
    fprintf(a, '\n');
end

fprintf(a, '\n');
fprintf(a, 'T2');
fprintf(a, '\n');
fprintf(a, '%g, ',meanperlag);
fprintf(a, '\n');
fprintf(a, '%g, ',stdperlag);
fprintf(a, '\n');
fprintf(a, '%g, ',Swapacc);
fprintf(a, '\n');
fprintf(a, '\n');
fprintf(a, 'T1');
fprintf(a, '\n');
fprintf(a, '%g, ',meanT1perlag);
fprintf(a, '\n');
fprintf(a, '%g, ',stdT1perlag);
fprintf(a, '\n');
fprintf(a, '%g, ',stdswapperlag);
fprintf(a, '\n');
fprintf(a, 'T2acc other method \n');
fprintf(a, '\n');
fprintf(a, '%g, ',T2acc);
fprintf(a, '\n');
fprintf(a, 'T1acc other method \n');
fprintf(a, '\n');
fprintf(a, '%g, ',T1acc);
fprintf(a, '\n');


fprintf(a, '\n');
fprintf(a, '\n');
fprintf(a, 'By Letter accuracy \n');
for(i = 1:length(T1bylet))
    fprintf(a,'%g,   %g,  %c \n', T1byletacc(i),T2byletacc(i),ITEMS(T1set,i));
end

fprintf(a, '\n');


for(i = 1:max(T2bindatacount));
    for(j = 1:8)
        if(T2bindatacount(j) >= i)           
            fprintf(a,'%d, ',T2bindata(i,j));
        else
            fprintf(a,' , ',T2bindata(i,j));
        end
    end
    fprintf(a, '\n');
end

fclose(a);
Swapacc
person


%output the anova style data

% a = fopen('T1anovaout.xl','w')
% for(i=1:length(T1anovadata))
%     fprintf(a,'%d  %d  %d %d\n', T1anovadata(i),T1anovagroup(i,1),T1anovagroup(i,2),T1anovagroup(i,3));
% end
% fclose(a)
% 
% a = fopen('T2anovaout.xl','w')
% for(i=1:length(T2anovadata))
%     fprintf(a,'%d %d  %d  %d %d\n', T2anovadata(i,1), T2anovadata(i,2) ,T2anovagroup(i,1),T2anovagroup(i,2),T2anovagroup(i,3));
% end
% fclose(a)
% 
% 
% 


%do the by-letter analysis of T1
T2perlagraw(:)
