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 temptop tempbottom
global dotcount dotprofile samestream diffstream condcopy evcopy dotsatpos dotacc allrtscount allrts matrix
global dotts subject persubmeans nondot bogusrt Or Userinput2 ors oldOr oldors oldT2rc oldT1r perdur durs t1seen temptop tempbottom


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
global thispersonperlet thispersonperletbase cstringssubjectmax subjfile4
ExpCondition = ExpThiscondition;

%THE RT SECTION



dott = 6;
if(reset)
    'resetting'
    rtcount = [0,0,0,0,0,0];
       matrix = zeros(4,4,2,2);
    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(4,6);
    durs = zeros(4,6);
    reps = zeros(4,6);
    t1seen = zeros(4,6);
    cstringssubject = zeros(4,2,2);
    numresponses = zeros(2,2);
    cstrings = zeros(4,3,2);
    cstringsmax = zeros(4,3,2);
    strings = zeros(4,3,2);
    numallgood = zeros(3,2);
    thispersonnumallgood = zeros(15,3,2);
    reporder = zeros(3,2,4,4);
    thispersonreporder = zeros(15,3,2,4,4);
    thispersondurs = zeros(15,4,6);
    thispersonperdur = zeros(15,4,6);

    thispersonperlet=zeros(15,26);
    thispersonperletbase=zeros(15,26);

    %the accuracy BIT
    allrts = 0;
    allrtscount = 0;

    subject = 0;
    persubmeans = zeros(1,5)
    testit = 0;
    numtop = zeros(4,3,2);
    numbottom = zeros(4,3,2);

    temptop = zeros(4,2,2);  % number of targets,  number of lag conditions
    tempbottom = zeros(4,2,2);



end

if(newsub)
    numtrials = zeros(2,2);
    cstringssubject = zeros(4,2,2);
    cstringssubjectmax = zeros(4,2,2);
    reps = zeros(4,6);
    testit = testit +1
    subject = subject + 1;
    oldors = ors;
    oldOr = Or;
    oldT2rc = T2rc;
    oldT1r = T1r;
    perdur = zeros(4,6);
    durs = zeros(4,6);
    temptop = zeros(4,2,2);  % number of targets,  number of lag conditions
    tempbottom = zeros(4,2,2);

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)

    %THE FILTER
    if(1)
        lag = condcopy(1);
        Soa = lag;
        numtargs = condcopy(2);
        IDD = numtargs;
        %Soa/lag 1 = 107,  2 = 53ms
        %numtargs/IDD 1 = no IDDS, 2 = 1 IDD
        
        
        [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) > 96)
                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)

            thispersonperletbase(subject,ts(c)-64) =  thispersonperletbase(subject,ts(c)-64) +1;

            if (max(responses == ts(c)) ==1 & ts(c) > 0)
                corrects(c) = 1;
                thispersonperlet(subject,ts(c)-64) =  thispersonperlet(subject,ts(c)-64) +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)
            
            
            
            for(c2 = c:4)
                if(hits(c) & hits(c2))
                    matrix(c,c2,lag,numtargs) = matrix(c,c2,lag,numtargs)+1;
                end
            end
            
            
            if(hits(1) &hits(2))    %this is the magical conditional
                cstrings(c,lag,numtargs) =  cstrings(c,lag,numtargs) +hits(c);  %accuracy
                cstringssubject(c,lag,numtargs) =  cstringssubject(c,lag,numtargs) +hits(c);
                cstringssubjectmax(c,lag,numtargs) =  cstringssubjectmax(c,lag,numtargs) +1;   %number trials

                cstringsmax(c,lag,numtargs) =  cstringsmax(c,lag,numtargs) +1;   %number trials

                if(lag ==1)
                    condtrial = numtargs;
                else
                    condtrial = numtargs +2;
                end
                condflag = 2;
                
                fprintf(subjfile3, '%d\t%d\t%d\t%d\t%.2g\t%d\t%d\n', subject,c,lag,numtargs,hits(c),condtrial,condflag);


            end
            
                        %OUTPUT THE CONDITIONALS INTO AN ANOVA FRIENDLY FILE
        for(targ = 1:3)
            for(c= 1:4)
                if(hits(targ)& c==targ+1)   %here is the critical conditional statement   check to see what is in hits()
                    fprintf(subjfile4, '%d\t%d\t%d\t%d\t%d\t%.2g\n', subject,c,lag,numtargs,1,hits(c));
                end
            end

            for(c= 1:4)
                if(~hits(targ)& c==targ+1)   %here is the critical conditional statement   check to see what is in hits()

                    fprintf(subjfile4, '%d\t%d\t%d\t%d\t%d\t%.2g\n', subject,c,lag,numtargs,0,hits(c));
                end
            end
        end

            
            
        end


        numresponses(lag,numtargs) = numresponses(lag,numtargs) + length(newresponses);



        easytargets = [7,8,11,12,14,17,20,22,25]+64;
        hardtargets = [2,3,4,6,10,16,18,24,26]+64;
        SuperEasy=[	8,11,14,20,22,25]+64;
        SuperHard	=[2,3,10,16,24,26]+64;



        alltargets = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]+64;

        reeves = 0;
        if(numtargs >0)

            if(reeves)
                %compute overall probability of order swaps with increasing
                %memory load



                %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;
                            tempbottom(sum(hits),lag,numtargs)  =  tempbottom(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;
                                temptop(sum(hits),lag,numtargs) = temptop(sum(hits),lag,numtargs) +1;
                            end
                        end
                    end
                end

            elseif(IDD ==1)

                %compute specific probability of a given order swap
                for(i = 1)

                    for(j = 3)
                        if(hits(i) & hits(j) & max(ts(i)==alltargets) & max(ts(j)==alltargets))
                            %                        if(hits(i) & hits(j) & max(ts(i)==easytargets) & max(ts(j)==hardtargets))
                            %                        if(hits(i) & hits(j) & max(ts(j)==easytargets))
                            %                        if(hits(i) & hits(j) & max(ts(i)==hardtargets) & max(ts(j)==easytargets))
                            %     if(hits(i) & hits(j) & max(ts(i)==easytargets) & max(ts(j)==easytargets))
                            %                       if(hits(i) & hits(j) & max(ts(i)==hardtargets) & max(ts(j)==hardtargets))
                            numbottom(sum(hits),lag,numtargs)  =  numbottom(sum(hits),lag,numtargs)+1;
                            tempbottom(sum(hits),lag,numtargs)  =  tempbottom(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;
                                temptop(sum(hits),lag,numtargs) = temptop(sum(hits),lag,numtargs) +1;
                            end
                        end
                    end
                end

            elseif(IDD ==2)
                for(i = 1)

                    for(j = 2)
                        if(hits(i) & hits(j) & max(ts(i)==alltargets) & max(ts(j)==alltargets))
                            %                        if(hits(i) & hits(j) & max(ts(j)==easytargets))
                            %                        if(hits(i) & hits(j)& max(ts(i)==easytargets) & max(ts(j)==hardtargets))
                            %                        if(hits(i) & hits(j)& max(ts(i)==hardtargets) & max(ts(j)==easytargets))
                            %                        if(hits(i) & hits(j) & max(ts(i)==hardtargets) & max(ts(j)==hardtargets))

                            %if(hits(i) & hits(j) & max(ts(i)==easytargets) & max(ts(j)==easytargets))

                            numbottom(sum(hits),lag,numtargs)  =  numbottom(sum(hits),lag,numtargs)+1;
                            tempbottom(sum(hits),lag,numtargs)  =  tempbottom(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;
                                temptop(sum(hits),lag,numtargs) = temptop(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
        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

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


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