function  batrunanalysis(outlistname, resetanova)


global Userinput Expstreams Explags Expstreamnums ExpEvents Exptime numnames outlist outlist2

global rt startrt rtlist dottime compact T1r T2r T2rc T2rnc T1rc Trialcount Swap Both rtcount means ITEMS
global dotcount dotprofile samestream diffstream condcopy evcopy dotsatpos dotacc persubmeans Or ors

global numtrials cstringssubject reporder cstringssubjectmax subjfile4 strings cstrings numallgood avgreporder subjfile3  filename temptop tempbottom


ft = fopen(outlistname,'r');
outlist = fscanf( ft ,'%s');

fclose(ft);
a = size(outlist,2);
sofar = 1
last = 1;
i = 1;

filename = 'CondT1t2'

 subjfile3 = fopen(sprintf('dataout%s.txt',filename), 'w');
 subjfile4 = fopen(sprintf('conddataout.txt',filename), 'w');


for(i=1:a-3)
    if(sum(outlist(i:i+3)== '.mat') == 4)

        outlist2{sofar} = outlist(last:i+3);
        last = i+4;
        sofar = sofar + 1;
    end
end

sofar = sofar - 1;
newone = 0;
oldname = 'xxx'
numnames = 0;
for i=1:sofar

    load(outlist2{i})
    if(i ==1)
        newanalyzedots(1,1)
    elseif(i/2 < ceil(i/2))   %newsub
        newanalyzedots(0,1)
    else
        newanalyzedots(0,0)
        subcstrings((i/2),:,:,:) = reshape(cstringssubject(:,:,:),[1,4,2,2]);   %even number
       
          subresp((i/2),:,:,:) = reshape(cstringssubjectmax(:,:,:),[1,4,2,2]);
      
        
           subtop((i/2),1:4,1:2,1:2) = reshape(temptop,[1,4,2,2]);
            subbottom((i/2),1:4,1:2,1:2) = reshape(tempbottom,[1,4,2,2]);
          avgsubtop((i/2),1:2,1:2) = reshape(sum(temptop,1),[1,2,2]);
          avgsubbottom((i/2),1:2,1:2) = reshape(sum(tempbottom,1),[1,2,2]);
  
        
    end


end



fp= fopen('orderout.txt', 'w');
avg = 0;
 fprintf(fp, 'SOA 1 IDD 1\n');
for(j = 1:size(persubmeans,1))
     fprintf(fp, '%g\n',avgsubtop(j,1,1)/avgsubbottom(j,1,1));
     avg = avg + avgsubtop(j,1,1)/avgsubbottom(j,1,1);
end
  fprintf(fp, '\n%g\n\n\n',avg/size(persubmeans,1));


avg = 0;
fprintf(fp, 'SOA 1 IDD 2\n');
for(j = 1:size(persubmeans,1))
     fprintf(fp, '%g\n',avgsubtop(j,1,2)/avgsubbottom(j,1,2));
      avg = avg + avgsubtop(j,1,2)/avgsubbottom(j,1,2);
end
  fprintf(fp, '\n%g\n\n\n',avg/size(persubmeans,1));

  avg = 0;
  fprintf(fp, 'SOA 2 IDD 1\n');
for(j = 1:size(persubmeans,1))
     fprintf(fp, '%g\n',avgsubtop(j,2,1)/avgsubbottom(j,2,1));
     avg = avg + avgsubtop(j,2,1)/avgsubbottom(j,2,1);
end
  fprintf(fp, '\n%g\n\n\n',avg/size(persubmeans,1));


avg = 0;
fprintf(fp, 'SOA 2 IDD 2\n');
for(j = 1:size(persubmeans,1))
     fprintf(fp, '%g\n',avgsubtop(j,2,2)/avgsubbottom(j,2,2));
      avg = avg + avgsubtop(j,2,2)/avgsubbottom(j,2,2);
end
  fprintf(fp, '\n%g\n\n\n',avg/size(persubmeans,1));

  
  
fclose(fp)
 
%T1r = T1r/Trialcount(1,1)


for(i = 1:size(reporder,1))
    for(j = 1:size(reporder,2))
        avgreporder(i,j,:,:) = reporder(i,j,:,:) /numallgood(i,j);
    end
end


subjfile = fopen('persubmeans.csv', 'w');

%persubjmeans (subject, 1:3, 1:2)

for(j = 1:size(persubmeans,1))

    fprintf(subjfile, '%g, %g, %g, %g,,  ', subcstrings(j,:,2,1)/subresp(j,1,2,1));
    fprintf(subjfile, '%g, %g, %g, %g,,  ', subcstrings(j,:,2,2)/subresp(j,1,2,2));
    fprintf(subjfile, '%g, %g, %g, %g,,  ', subcstrings(j,:,1,1)/subresp(j,1,1,1));
    fprintf(subjfile, '%g, %g, %g, %g \n',  subcstrings(j,:,1,2)/subresp(j,1,1,2));

end



%When you get to this, Brad
%remember that the IDD variable is in a different array position compared
%to the IDD3

%IDD = condcopy(2) here, which goes into the numtargs variable (i.e. the
%third and final data point here

% 
% What I got, when I ran this initially,  after numtop./numbottom:
% 
% ans(:,:,1) =    %these are for the slow presentation rate, I THINK
%        NaN
%     0.8788       
%     0.7945
%     0.7879
% ans(:,:,2) =
%        NaN
%     0.9186
%     0.9429
%     1.0000
% 


% ans(:,:,1) =     %these are for the fast presentation rate, 
%        NaN
%     0.6071
%     0.4828
%     0.6667
% ans(:,:,2) =           %note the lack of a difference between IDD
%                           conditions, as if there's no blink in either
%                           case!!!
%        NaN
%     0.6300
%     0.5890
%     0.5000


 
     fprintf(subjfile, '\n\n\nNumtop\n\n');
for(j = 1:size(persubmeans,1))
     fprintf(subjfile, '%g, %g, %g, %g,,',subtop(j,:,1,1));   %100ms  no IDD
%     fprintf(subjfile, '%g, %g, %g, %g\n',subtop(j,:,2,2));   %50ms,  with IDD
     fprintf(subjfile, '%g, %g, %g, %g\n',subtop(j,:,2,1));   %50ms, no IDD

end

fprintf(subjfile, '\n\n\nNumbottom\n\n');
for(j = 1:size(persubmeans,1))
     fprintf(subjfile, '%g, %g, %g, %g,,',subbottom(j,:,1,1));  %100ms  no IDD
 %    fprintf(subjfile, '%g, %g, %g, %g\n',subbottom(j,:,2,2));   %50ms,  with IDD 
     fprintf(subjfile, '%g, %g, %g, %g\n',subbottom(j,:,2,1));   %50ms,  no IDD

end



subjfile2 = fopen('dataout.txt', 'w');

for(j = 1:size(persubmeans,1))
    for(pos = 1:4)
        for(soa = 1:2)
            for(idd = 1:2)
                fprintf(subjfile2, '%d\t%d\t%d\t%d\t%.5g\n', j,pos,soa,idd,subcstrings(j,pos,soa,idd)/subresp(j,soa,idd));
            end
        end
    end
end




fclose(subjfile);
fclose(subjfile2);
fclose(subjfile3);
fclose(subjfile4);
showresults




