%%%%Motion Silencing in Dynamic Visual Search for an Orientation Change%%%
%%%%Crowe, Howard, Gilchrist & Kent%%%%
%%%%Analyse Experiment 1%%%%

%Clear the workspace
clear all;
close all;

%Set constants for experiment
participants = 31;
trials = 360;
conditions = 12;
excludedParticipants = 2;

%Combine Participants
CombineParticipants = NaN(1,3);
CombineParticipantsAll = NaN(1,3);

for i = 1:participants
    
    %Set counter for each participant to check excluded trials
    TooFast = 0;
    TooSlow = 0;
    
    %Read in participant data
    filename = sprintf('Data%d.txt',i);
    AllData = load(filename);   
    
    %Make empty matrices to store data
    Cond1 = zeros(30,1);
    Cond2 = zeros(30,1);
    Cond3 = zeros(30,1);
    Cond4 = zeros(30,1);
    Cond5 = zeros(30,1);
    Cond6 = zeros(30,1);
    Cond7 = zeros(30,1);
    Cond8 = zeros(30,1);
    Cond9 = zeros(30,1);
    Cond10 = zeros(30,1);
    Cond11 = zeros(30,1);
    Cond12 = zeros(30,1);
    
    %Set counters 
    A = 0;
    B = 0;
    C = 0;
    D = 0;
    E = 0;
    F = 0;
    G = 0;
    H = 0;
    I = 0;
    J = 0;
    K = 0;
    L = 0;

    for j = 1:trials;
        
        
         %%Remove trials with fast (< 200 ms) or slow (> 4000) response
         %%times
         if AllData(j,7) > 4000 
            TooSlow = TooSlow+1;
            AllData(j,7) = NaN;  
            
            else if AllData(j,7) < 200;
                    TooFast = TooFast+1;
                    AllData(j,7) = NaN;
                end
         end
        
        %Time from start of trial to hit
        TrialDuration(j,1) = AllData(j,5) + AllData(j,6);
        
        %Sort each trial into the correct condition
        
        if AllData(j,3) == 1 && AllData(j,5) == 2; %Stationary2
           Cond1(A+1,1) = AllData(j,7);
           CheckConditions(j,1) = 1;
           A = A+1;
                    else if AllData(j,3) == 1 && AllData(j,5) == 4; %Stationary4
                            Cond2(B+1,1) = AllData(j,7);
                            CheckConditions(j,1) = 2;
                            B = B+1;
                                   else if AllData(j,3) == 1 && AllData(j,5) == 8; %Stationary8
                                   Cond3(C+1,1) = AllData(j,7);
                                   CheckConditions(j,1) = 3;
                                   C = C+1;
                                            else if AllData(j,3) == 2 && AllData(j,5) == 2; %Motion2
                                            Cond4(D+1,1) = AllData(j,7);
                                            CheckConditions(j,1) = 4;
                                            D = D+1;
                                                    else if AllData(j,3) == 2 && AllData(j,5) == 4; %Motion4
                                                    Cond5(E+1,1) = AllData(j,7);
                                                    CheckConditions(j,1) = 5;
                                                    E = E+1;
                                                            else if AllData(j,3) == 2 && AllData(j,5) == 8; %Motion8
                                                            Cond6(F+1,1) = AllData(j,7);
                                                            CheckConditions(j,1) = 6;
                                                            F = F+1;
                                                                    else if AllData(j,3) == 3 && AllData(j,4) == 1 && AllData(j,5) == 2; %Both S2
                                                                    Cond7(G+1,1) = AllData(j,7);
                                                                    CheckConditions(j,1) = 7;
                                                                    G = G+1;
                                                                            else if AllData(j,3) == 3 && AllData(j,4) == 1 && AllData(j,5) == 4; %Both S4
                                                                            Cond8(H+1,1) = AllData(j,7);
                                                                            CheckConditions(j,1) = 8;
                                                                            H = H+1;
                                                                                     else if AllData(j,3) == 3 && AllData(j,4) == 1 && AllData(j,5) == 8; %Both S8
                                                                                     Cond9(I+1,1) = AllData(j,7);
                                                                                     CheckConditions(j,1) = 9;
                                                                                     I = I+1;
                                                                                            else if AllData(j,3) == 3 && AllData(j,4) == 2 && AllData(j,5) == 2; %Both M8
                                                                                            Cond10(J+1,1) = AllData(j,7);
                                                                                            CheckConditions(j,1) = 10;
                                                                                            J = J+1;
                                                                                                    else if AllData(j,3) == 3 && AllData(j,4) == 2 && AllData(j,5) == 4; %Both M8
                                                                                                    Cond11(K+1,1) = AllData(j,7);
                                                                                                    CheckConditions(j,1) = 11;
                                                                                                    K = K+1;
                                                                                                            else if AllData(j,3) == 3 && AllData(j,4) == 2 && AllData(j,5) == 8; %Both M8
                                                                                                            Cond12(L+1,1) = AllData(j,7);
                                                                                                            CheckConditions(j,1) = 12;
                                                                                                            L = L+1;
                                                                                 
                                                                                                        end
                                                                                                end
                                                                                        end
                                                                                end
                                                                        end
                                                                end
                                                        end
                                                end
                                        end
                                end
                        end
                end
    end %End of trial
  
  %Calculate the medians for each participant, for each condition
  %In revision, remember to remove SetSize from analysis because we
  %are now treating this as a qualitatively different task.
  RT(i,1) = nanmedian(Cond1);
  RT(i,2) = nanmedian(Cond2);
  RT(i,3) = nanmedian(Cond3);
  RT(i,4) = nanmedian(Cond4);
  RT(i,5) = nanmedian(Cond5);
  RT(i,6) = nanmedian(Cond6);
  RT(i,7) = nanmedian(Cond7);
  RT(i,8) = nanmedian(Cond8);
  RT(i,9) = nanmedian(Cond9);
  RT(i,10) = nanmedian(Cond10);
  RT(i,11) = nanmedian(Cond11);
  RT(i,12) = nanmedian(Cond12);

  %Remove data from the participants who displayed guessing following
  %the vincentizing analysis done during revisions. 
  RT(11,:) = NaN;
  RT(18,:) = NaN;
 
  %Check how much data is excluded for each participant    
  CheckExclusions(i,1) = TooFast;
  CheckExclusions(i,2) = TooSlow;

end %End of participant 

%Take the mean of all participants 
AllParticipants = nanmean(RT);

ErrorBars = nanstd(RT)/sqrt(participants-excludedParticipants);
EBStatic = ErrorBars(1:3);
EBMoving = ErrorBars(4:6);
EBBothS = ErrorBars(7:9);
EBBothM = ErrorBars(10:12);

%Now plot this in matlab
figure(1);
xaxis = [2 4 8];
Static = AllParticipants(1:3);
Moving = AllParticipants(4:6);
BothS = AllParticipants(7:9);
BothM = AllParticipants(10:12);

%Plot means
plot(xaxis,Static,'-o','color',[.85 .63 .13],'MarkerSize',5,'MarkerFaceColor',[.85 .63 .13],'MarkerEdgeColor',[.85 .63 .13],'LineWidth',2);
hold on
plot(xaxis,Moving,'-o','color',[.1, .53, .55],'MarkerSize',5,'MarkerFaceColor',[.1 .53 .55],'MarkerEdgeColor',[.1 .53 .55],'LineWidth',2);
hold on
plot(xaxis,BothS,'-o','color',[.8, 0, .8],'MarkerSize',5,'MarkerFaceColor',[.8, 0, .8],'MarkerEdgeColor',[.8, 0, .8],'LineWidth',2);
hold on
plot(xaxis,BothM,'-o','color',[.4, .9, .15],'MarkerSize',5,'MarkerFaceColor',[.4, .9, .15],'MarkerEdgeColor',[.4, .9, .15],'LineWidth',2);

%Plot error bar
errorbar(xaxis,Static,EBStatic,'LineWidth',2,'Color',[.85 .63 .13]);

errorbar(xaxis,Moving,EBMoving,'LineWidth',2,'Color',[.1 .53 .55]);

errorbar(xaxis,BothS,EBBothS,'LineWidth',2,'Color',[.8, 0, .8]);

errorbar(xaxis,BothM,EBBothM,'LineWidth',2,'Color',[.4, .9, .15]);

%Format figure
ylim([400 800]);
xlim([0 9]);
xticks([2 4 8]);
xlabel('Set Size');
ylabel('Response Time (ms)');
legend({'Pure Static','Pure Moving', 'Mixed Display - Static Target','Mixed Display - Moving Target'},'Location','northwest');

%Make figure nice
set(findall(0,'type','axes'),'linewidth',1,'box','off','FontSize',14);%New parameters

%Write out the data for subsequent analysis 
csvwrite(strcat(pwd,sprintf('\\ReactionTimes_Experiment2.csv')),RT);