function newanalyzedots(reset,newsub)
global In1 Expkeyspressed Expkeycount Userinput Explags Expstreams Expstreamnums Expstreams2 Expstreamnums2 Exptime Condition ExpEvents Miscflag Expmiscflag Thiscondition ExpCondition ExpThiscondition priorcondition

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 allrtscount allrts
global dotts subject persubmeans nondot bogusrt Or Userinput2 ors oldOr oldors oldT2rc oldT1r perdur durs t1seen

global thingie thingieperson thingietrial thingieitem thingietargs cstrings strings reps reporder testit numallgood newresponses
global subjfile3 cstringsmax numtrials cstringssubject numresponses thispersonnumallgood  thispersonreporder numtop numbottom thispersondurs thispersonperdur
ExpCondition = ExpThiscondition;

%THE RT SECTION

condflag = 2;

dott = 6;
numcond = 7;

if(reset)
    'resetting'
    rtcount = [0,0,0,0,0,0];
	
    rtlist = 0;
    dotprofile = 0;
    thingie = 0;
    dotcount = 0;
    Trialcount = zeros(4,1);
    T1r = zeros(4,1);
    T2r = zeros(4,1);
    T2rc = zeros(4,1);  %T2 correct and conditional on T1
    T2rnc = zeros(4,1);
    Or = zeros(4,1);
    Orn = zeros(4,1);
    Swap= zeros(4,1);
    Both = zeros(4,1);
    ors = zeros(4,1);
    nondot = 0;
    bogusrt = 0;
    perdur = zeros(numcond,1);
    durs = zeros(numcond,1);
    reps = zeros(numcond,1);
    t1seen = zeros(numcond,1);
    cstringssubject = zeros(4,numcond,1);
    numresponses = zeros(numcond,2);
    cstrings = zeros(4,numcond,1);
    cstringsmax = zeros(4,numcond,1);
    strings = zeros(4,numcond,1);
    numallgood = zeros(numcond,2);
    thispersonnumallgood = zeros(15,numcond,1);
    reporder = zeros(numcond,1,4,4);
    thispersonreporder = zeros(15,numcond,1,4,4);
    thispersondurs = zeros(15,numcond,1);
    thispersonperdur = zeros(15,numcond,1);
	
    %the accuracy BIT
    allrts = 0;
    allrtscount = 0;
	
    subject = 0;
    persubmeans = zeros(1,numcond)
    testit = 0;
    numtop = zeros(4,numcond,1);
    numbottom = zeros(4,numcond,1);
	
	
end

if(newsub)
    numtrials = zeros(numcond,1);
    cstringssubject = zeros(4,numcond,1);
    reps = zeros(numcond,1);
    testit = testit +1
    subject = subject + 1;
    oldors = ors;
    oldOr = Or;
    oldT2rc = T2rc;
    oldT1r = T1r;
    perdur = zeros(numcond,1);
    durs = zeros(numcond,1);
end


means = 0;
count = 0;

for(i=1: size(Expkeycount,1))
    condcopy = ExpThiscondition(:,i);
    %conditions
	
	
    evcopy = reshape(ExpEvents(i,:,:),[size(ExpEvents,2) size(ExpEvents,3)]);
	
	
    %expevents:
    %    1 is the time
    %    2 is target/distractor
    %    3   Which target/distractor item  (filter through the ITEMS array
    %           in params.mat
    %    7  which target (1-4)
	lag = condcopy(1);
    %THE FILTER
    if(lag < 8 & lag > 0)
	%	lag = lag -1;
		
        Soa = lag;
        numtargs = condcopy(2);
        IDD = numtargs;
		%Soa  (lag) is the main conditional variable here... it goes 1-7
		
		
        [x T1pos] = min(abs(evcopy(:,7)-1));
		
        T1item = ITEMS(1,evcopy(T1pos,3));
		
        [x T2pos] = min(abs(evcopy(:,7)-2));
        T2item = ITEMS(1,evcopy(T2pos,3));
		
        [x T3pos] = min(abs(evcopy(:,7)-3));
        T3item = ITEMS(1,evcopy(T3pos,3));
		
        [x T4pos] = min(abs(evcopy(:,7)-4));
        T4item = ITEMS(1,evcopy(T4pos,3));
		
        %ts are the items presented
        ts(1:4) = [T1item, T2item, T3item, T4item];
		
        responses = Userinput(i,:);
        repeats = 0;
        for(r = 1: length(responses))   % fix case of user response
            if(responses(r) > 97)
                responses(r) = responses(r) -32;
			end
            if(responses(r) > 13 & sum(responses(r)==responses(1:r-1))> 0)
                repeats= 1;
			end
		end
	
        %this line reduces the number of responses to 4
        %responses = responses(1:min(length(responses),4));
		
		
        %count the number of repeats;
        reps(lag,numtargs) = reps(lag,numtargs)+repeats;
		
        corrects = zeros(4);
        for(c = 1:4)
            if (max(responses == ts(c)) ==1 & ts(c) > 0)
                corrects(c) = 1;
			end
		end
	
        %Remove all non targets from the response string
        %exclude repeats
        newresponses = 0;
        index = 0;
		
        for(r = 1: length(responses))  %strip White space
            if(responses(r)> 32)
                index = index + 1;
                newresponses(index) = responses(r);
			else
                'Break';
			end
		end
	
        responses = newresponses; %  Cut down to first 4 responses
		
        responses = responses(1:min(length(responses),4));
		
		
        newresponses = 0;
        index = 0;
		numts = 4;
		
        for(r = 1: length(responses))
            if(sum(responses(r) ==ts(1:numts))> 0 & sum(responses(r) ==responses(1:r-1)) ==0)
                index = index + 1;
                newresponses(index) = responses(r);
			end
		end
	
        %now how many hits on this trial
        hits = zeros(4,1);
        for(c = 1:4)
            hit = max(ts(c) ==newresponses) & ts(c) > 0;
            if(hit)
                hits(c) = 1;
			end
		
            if(c ==1)
                numtrials(lag,numtargs) =   numtrials(lag,numtargs)  +1;
			end
		end
	
        for(c= 1:4)
            if(hits(1) & hits(2))   %here is the critical conditional statement   check to see what is in hits()
                cstrings(c,lag,numtargs) =  cstrings(c,lag,numtargs) +hits(c);  %accuracy
                cstringssubject(c,lag,numtargs) =  cstringssubject(c,lag,numtargs) +hits(c);
                cstringsmax(c,lag,numtargs) =  cstringsmax(c,lag,numtargs) +1;   %number trials
				
                
				fprintf(subjfile3, '%d\t%d\t%d\t%d\t%.2g\t%d\n', subject,c,lag,numtargs,hits(c),condflag);
				
				
			end
		end
	
	
        numresponses(lag,numtargs) = numresponses(lag,numtargs) + length(newresponses);
		
		
		
        if(numtargs == 1)  % 4 target trial
            if(lag >0)
                %compute overall probability of order swaps with increasing
                %memory load
				
                if( hits(1) & ~hits(2) & hits(3) & hits(4))
					
                    %the reeves & Sperling approach
                    for(i = 1:3)
						
                        for(j = i+1:4)
                            if(hits(i) & hits(j))
                                %   numbottom(sum(hits),lag,numtargs)  =  numbottom(sum(hits),lag,numtargs)+1;
                                [x ind1] = max(newresponses == ts(i));
                                [x ind2] = max(newresponses == ts(j));
                                if(ind1 < ind2)
                                    %       numtop(sum(hits),lag,numtargs) = numtop(sum(hits),lag,numtargs) +1;
								end
							end
						end
					end
				
                    %compute specific probability of a given order swap
                    for(i = 1)
						
                        for(j = 3)
                            if(hits(i) & hits(j))
                                numbottom(sum(hits),lag,numtargs)  =  numbottom(sum(hits),lag,numtargs)+1;
                                [x ind1] = max(newresponses == ts(i));
                                [x ind2] = max(newresponses == ts(j));
                                if(ind1 <ind2)
                                    numtop(sum(hits),lag,numtargs) = numtop(sum(hits),lag,numtargs) +1;
								end
							end
						end
					end
				end
			
			
                %       if( sum(hits) ==4)
                if(hits(1) & hits(2) & hits(3)& hits(4))
					
                    numallgood(lag,numtargs)= numallgood(lag,numtargs)+1;
					
                    thispersonnumallgood(subject,lag,numtargs) =          thispersonnumallgood(subject,lag,numtargs) +1;
                    for(c = 1:4)
                        length(newresponses);
                        [x ind] = max(newresponses == ts(c));
                        %reportder(1-3, 1-2, T1/2/3/4, pos 1/2/3/4)
						
                        reporder(lag,numtargs,c,ind) =   reporder(lag,numtargs,c,ind) +1;
                        thispersonreporder(subject,lag,numtargs,c,ind) =   thispersonreporder(subject,lag,numtargs,c,ind) +1;
					end
				end
			elseif(lag ==2)
		
                for(i = 1:3)
					
                    for(j = i+1:3)
                        if(hits(i) & hits(j))
                            numbottom(sum(hits),lag,numtargs)  =  numbottom(sum(hits),lag,numtargs)+1;
                            [x ind1] = max(newresponses == ts(i));
                            [x ind2] = max(newresponses == ts(j));
                            if(ind1 < ind2)
                                numtop(sum(hits),lag,numtargs) = numtop(sum(hits),lag,numtargs) +1;
							end
						end
					end
				end
			
			
			
                %if( sum(hits) ==3)
				
                if(hits(1) & hits(2) & hits(3))
                    numallgood(lag,numtargs)= numallgood(lag,numtargs)+1;
                    thispersonnumallgood(subject,lag,numtargs) =          thispersonnumallgood(subject,lag,numtargs) +1;
					
                    for(c = 1:3)
                        length(newresponses);
                        [x ind] = max(newresponses == ts(c));
                        reporder(lag,numtargs,c,ind) =   reporder(lag,numtargs,c,ind) +1;
                        thispersonreporder(subject,lag,numtargs,c,ind) =   thispersonreporder(subject,lag,numtargs,c,ind) +1;
						
					end
				
				end
			elseif(lag ==1)  %obvious but written for clarity
                %                 if( sum(hits) ==2)
                if(hits(1) & hits(2) & hits(3))
					
                    numallgood(lag,numtargs)= numallgood(lag,numtargs)+1;
                    for(c = 1:4)
                        length(newresponses);
                        [x ind] = max(newresponses == ts(c));
                        reporder(lag,numtargs,c,ind) =   reporder(lag,numtargs,c,ind) +1;
					end
				
				end
			end
		elseif(numtargs ==1)
            if(lag ==3)
                if( sum(hits) ==2)
                    numallgood(lag,numtargs)= numallgood(lag,numtargs)+1;
					
                    for(c = 1:2)
                        length(newresponses);
                        [x ind] = max(newresponses == ts(c));
                        reporder(lag,numtargs,c,ind) =   reporder(lag,numtargs,c,ind) +1;
					end
				end
			end
		end
	
	
	
		if(max(responses == ts(1)) ==1 & ts(1) > 0)
			perdur(lag,numtargs) = perdur(lag,numtargs) +1;
			thispersonperdur(subject,lag,numtargs)=  thispersonperdur(subject,lag,numtargs)+1;
		end
		%lag is 1 2 or 3:  (repeat variable)
		% numtargs = 2 or 4.
		
		if(lag ==1)  %the no repeat case
			
			if(numtargs ==1)  % 2 targets
				durs(lag,numtargs) = 	durs(lag,numtargs) +(corrects(2)&corrects(1));
				thispersondurs(subject,lag,numtargs) = thispersondurs(subject,lag,numtargs) +  (corrects(2)&corrects(1));
			else              % 4 targets
				durs(lag,numtargs) = 	durs(lag,numtargs) +(corrects(4)&corrects(1));
				
				thispersondurs(subject,lag,numtargs) = thispersondurs(subject,lag,numtargs) +  (corrects(4)&corrects(1));
				
			end
		elseif (lag ==1)  %repeat condition
			both = sum(responses == T1item);
			durs(lag,numtargs) = 			durs(lag,numtargs) +(both == 2);  %only consider it correct if they report TWO copies of the same item
			thispersondurs(subject,lag,numtargs) = thispersondurs(subject,lag,numtargs) +(both == 2);
			
		end
	
	end
end



for(lag = 1 :7)
    persubmeans(subject,1,lag) = durs(lag,1)./perdur(lag,1);
end


%persubmeans(subject,1:4) = reshape((T2rc-oldT2rc)./(T1r-oldT1r),[1 4]);
