clear all; close all;

%% Define what to analyse

% Which subjects?
subjects = [1:34];

% Which data?
whichdata = 'clean';  % alternative: 'raw'

if whichdata == 'clean'
    load('P:/3018029.09/data_sd_clean.mat')
    folder_str = 'Clean data';
elseif whichdata == 'raw'
    load('P:/3018029.09/data_sd.mat')
    folder_str = 'Raw data';
end

% Make plots?
plots_direction = true;
plots_p_repeat  = true;

%% Calculate direction difference

diffMotionDir = abs(data_sd.currMotionDir - data_sd.prevMotionDir);

for i = 1:length(diffMotionDir)
    if diffMotionDir(i) > 180
        diffMotionDir(i) = 360 - diffMotionDir(i);
    end
end

data_sd = addvars(data_sd,diffMotionDir);


direction_division = [30 60 90 120 150 181];
         
%% Try to find out if motion direction difference matters on performance/rt/confidence etc

for iSubject = 1:length(subjects)
    
    %% Select the subject's data
    
    subject_data = data_sd(data_sd.subject == subjects(iSubject), :);
    
    for iMotion = 1:length(direction_division)-1
        
        % Select data for motion diff category 
        subject_data_mot = subject_data(subject_data.diffMotionDir >= direction_division(iMotion) & subject_data.diffMotionDir <= direction_division(iMotion+1), :);
        
        % Percentage correct answers per motion diff category 
        correct_percentage(iSubject, iMotion) = ...
            100* sum(subject_data_mot.currCorrect == 1) ... 
            / size(subject_data_mot.currRelativeCoherenceAbs,1);
                      
        
        % Percentage correct answers and mean RT per motion diff category per confidence
        % level
        for iConfidence = 1:4
            correct_percentage_per_evid_conf{iMotion}(iSubject,iConfidence) = ...
                100* sum(subject_data_mot.currCorrect == 1 & subject_data_mot.currConfidence == iConfidence) ... 
                / sum(subject_data_mot.currConfidence == iConfidence);
            responsetime_per_evid_conf{iMotion}(iSubject,iConfidence) = ...
                1000* nanmean(subject_data_mot.currRt(subject_data_mot.currConfidence == iConfidence));   
        end
       
        % Mean response time per motion diff category 
        responsetime(iSubject,iMotion) = ...
            1000* nanmean(subject_data_mot.currRt);   

        % Mean confidence per motion diff category 
        confidence(iSubject,iMotion) = nanmean(subject_data_mot.currConfidence);     
        
        % Mean RT and confidence split for incorrect (i=1)/correct (i=2) trials
        for i = 1:2 
            responsetime_corr_split{i}(iSubject,iMotion) = ...
                1000* nanmean(subject_data_mot.currRt(subject_data_mot.currCorrect == i-1)); 
            confidence_corr_split{i}(iSubject,iMotion) = ...
                nanmean(subject_data_mot.currConfidence(subject_data_mot.currCorrect == i-1));  
        end        
        
    end
    
  
end

% Group mean and se
correct_percentage_mean     = mean(correct_percentage);
    correct_percentage_se   = std(correct_percentage)/sqrt(length(correct_percentage));
responsetime_mean           = mean(responsetime);
    responsetime_se         = std(responsetime)/sqrt(length(responsetime));
confidence_mean             = mean(confidence);
    confidence_se           = std(confidence)/sqrt(length(confidence));
    
    % also for split incorrect (i=1)/correct (i=2) trials
    for i = 1:2
        responsetime_corr_split_mean(i,:)   = nanmean(responsetime_corr_split{i});
            responsetime_corr_split_se(i,:) = nanstd(responsetime_corr_split{i})/sqrt(length(responsetime_corr_split{i}(~isnan(responsetime_corr_split{i}))));
        confidence_corr_split_mean(i,:)     = nanmean(confidence_corr_split{i});
            confidence_corr_split_se(i,:)   = nanstd(confidence_corr_split{i}/sqrt(length(confidence_corr_split{i}(~isnan(confidence_corr_split{i})))));
    end
    
for iMotion = 1:length(direction_division)-1
    correct_percentage_per_evid_conf_mean{iMotion}    = nanmean(correct_percentage_per_evid_conf{iMotion});
        correct_percentage_per_evid_conf_se{iMotion}  = nanstd(correct_percentage_per_evid_conf{iMotion})./sqrt(sum(~isnan(correct_percentage_per_evid_conf{iMotion})));
    responsetime_per_evid_conf_mean{iMotion}          = nanmean(responsetime_per_evid_conf{iMotion});
        responsetime_per_evid_conf_se{iMotion}        = nanstd(responsetime_per_evid_conf{iMotion})./sqrt(sum(~isnan(responsetime_per_evid_conf{iMotion})));
end 


% Plot the sanity check data if plots are enabled

if plots_direction == true

    % Percentage correct answers per motion diff category 
    figure; hold on;
        bar(correct_percentage_mean); 
        errorbar(correct_percentage_mean,correct_percentage_se,'.b'); 
        ylim([45 100]); 
        xlim([0.25 5.75]); 
        set(gca,'XTick',1:5,'XTickLabel',{'smallest','','','','largest'});
        xlabel('Motion difference from previous trial')
        ylabel('Mean percentage correct per stimulus level')
        title('Performance');
        
    % Percentage correct answers per motion diff category 
    figure; hold on;
        for iSubject = 1:length(subjects)
            plot(correct_percentage(iSubject,:))
        end
        ylim([45 100]); 
        xlim([0.25 5.75]); 
        set(gca,'XTick',1:5,'XTickLabel',{'smallest','','','','largest'});
        xlabel('Motion difference from previous trial (deg)')
        ylabel('Mean percentage correct per stimulus level')
        title('Performance');
        
    % Percentage correct answers per motion diff category per confidence level
    figure; hold on;    
        for iMotion = 1:5
            subplot(2,3,iMotion);
            hold on;
            errorbar(correct_percentage_per_evid_conf_mean{iMotion}, correct_percentage_per_evid_conf_se{iMotion}, 'k');
            ylim([45 100]); 
            xlim([0.25 5.75]);         
            set(gca,'XTick',1:4,'XTickLabel',{'1','2','3','4'});
            xlabel('Confidence rating')
            ylabel('Performance (%)')
            title(['Motion difference category ' num2str(iMotion)]);
            hold off;
        end
       
    % Mean response time per stimulus level per confidence level
    figure; hold on;    
    for iMotion = 1:5
        subplot(2,3,iMotion);
        hold on;
        errorbar(responsetime_per_evid_conf_mean{iMotion}, responsetime_per_evid_conf_se{iMotion}, 'k');
        ylim([750 1600]); 
        xlim([0.25 4.75]);         
        set(gca,'XTick',1:4,'XTickLabel',{'1','2','3','4'});
        xlabel('Confidence rating')
        ylabel('RT (ms)')
        title(['Coherence level ' num2str(iMotion)]);
        hold off;
    end
    
   
    % Mean response time per motion diff category, with standard errors
    figure; hold on;
        bar(responsetime_mean);
        errorbar(responsetime_mean, responsetime_se, '.b')
        ylim([750 1500]); 
        xlim([0.25 5.75]); 
        set(gca,'XTick',1:5,'XTickLabel',{'smallest','','','','largest'});
        xlabel('Motion difference from previous trial (deg)')
        ylabel('Response time (ms)')
        title('Mean response time per stimulus level');
        hold off;
       
    % split for incorrect/correct trials
    figure; hold on;
        plot(responsetime_corr_split_mean');
            errorbar(responsetime_corr_split_mean', responsetime_corr_split_se', '.k')
        legend('Incorrect responses', 'Correct responses')
        legend('Location','northwest')
        ylim([750 1500]); 
        xlim([0.25 5.75]); 
        set(gca,'XTick',1:5,'XTickLabel',{'smallest','','','','largest'});
        xlabel('Motion difference from previous trial (deg)')
        ylabel('Response time (ms)')
        title('Mean response time per stimulus level');
        hold off;
        
    % Mean confidence per motion diff category 
    figure; hold on;
        bar(confidence_mean); 
        errorbar(confidence_mean, confidence_se, '.b')
        ylim([1 4]); 
        xlim([0.25 5.75]); 
        set(gca,'XTick',1:5,'XTickLabel',{'smallest','','','','largest'});
        xlabel('Motion difference from previous trial (deg)')
        ylabel('Confidence (1-4)')
        title('Mean confidence per stimulus level');
       
    % split for incorrect/correct trials
    figure; hold on;
        plot(confidence_corr_split_mean');
            errorbar(confidence_corr_split_mean', confidence_corr_split_se', '.k')
        legend('Incorrect responses', 'Correct responses')
        legend('Location','northwest')
        ylim([1 4]); 
        xlim([0.25 5.75]); 
        set(gca,'XTick',1:5,'XTickLabel',{'smallest','','','','largest'});
        xlabel('Motion difference from previous trial (deg)')
        ylabel('Confidence (1-4)')
        title('Mean confidence per stimulus level');

end


%% ========= P_repeats ========== %%

% Add row to data for choice repetition

choiceRepetition    = (data_sd.prevResponseDir == data_sd.currResponseDir); 
data_sd             = [data_sd, table(choiceRepetition)]; 

for iSubject = 1:length(subjects)
    
    %% Select the subject's data
    
    subject_data = data_sd(data_sd.subject == subjects(iSubject), :);
    
    %% p_repeat for previous stimulus level
    for iMotion = 1:length(direction_division)-1
        
        % Select data for motion diff category 
        subject_data_mot = subject_data(subject_data.diffMotionDir >= direction_division(iMotion) & subject_data.diffMotionDir <= direction_division(iMotion+1), :);
        
        % Calculate p_repeat for each current response separately
        subject_data_mot_0 = subject_data_mot(subject_data_mot.currResponseDir == 0, :);
        subject_data_mot_1 = subject_data_mot(subject_data_mot.currResponseDir == 1, :);
  
        p_repeat(iSubject,iMotion) = (mean(subject_data_mot_0.choiceRepetition)+mean(subject_data_mot_1.choiceRepetition))/2;
        
        %% p_repeat for previous stim level, split for prev RT
        
        % Split for prev RT
        subject_data_mot_rtsorted = sortrows(subject_data_mot,'prevRt');
        length_rt = length(subject_data_mot_rtsorted.prevRt);
        %half_length_rt = length(subject_data_stim_rtsorted.prevRt)/2;
        half_length_rt = floor(length(subject_data_mot_rtsorted.prevRt)/2);
        %{
        if isinteger(half_length_rt)
            % Low prev RTs
            subject_data_stim_prev_lowRt = subject_data_stim_rtsorted(1:half_length_rt,:);
            % High prev RTs
            subject_data_stim_prev_highRt = subject_data_stim_rtsorted((half_length_rt+1):length_rt,:);
        else
            % Low prev RTs
            subject_data_stim_prev_lowRt = subject_data_stim_rtsorted(1:floor(half_length_rt),:);
            % High prev RTs
            subject_data_stim_prev_highRt = subject_data_stim_rtsorted(ceil(half_length_rt):length_rt,:);
        end
        %}
        
        % Low prev RTs
        subject_data_mot_prev_lowRt = subject_data_mot_rtsorted(1:half_length_rt,:);
        % High prev RTs
        subject_data_mot_prev_highRt = subject_data_mot_rtsorted((half_length_rt+1):length_rt,:);
        
        
        % Calculate p_repeat for each prev stimulus level, for prev low RTs
        subject_data_mot_prev_lowRt_0 = subject_data_mot_prev_lowRt( ...
            subject_data_mot_prev_lowRt.currResponseDir == 0, :);
        subject_data_mot_prev_lowRt_1 = subject_data_mot_prev_lowRt( ...
            subject_data_mot_prev_lowRt.currResponseDir == 1, :);

        p_repeat_lowRt(iSubject,iMotion) = (mean(subject_data_mot_prev_lowRt_0.choiceRepetition)+mean(subject_data_mot_prev_lowRt_1.choiceRepetition))/2;
        
        % Calculate p_repeat for each prev stimulus level, for prev high RTs
        subject_data_mot_prev_highRt_0 = subject_data_mot_prev_highRt( ...
            subject_data_mot_prev_highRt.currResponseDir == 0, :);
        subject_data_mot_prev_highRt_1 = subject_data_mot_prev_highRt( ...
            subject_data_mot_prev_highRt.currResponseDir == 1, :);

        p_repeat_highRt(iSubject,iMotion) = (mean(subject_data_mot_prev_highRt_0.choiceRepetition)+mean(subject_data_mot_prev_highRt_1.choiceRepetition))/2;
        
        
        %% p_repeat for previous stim level, split for prev correctness
        
        % Split data into previous incorrect and previous correct
        subject_data_mot_prev_incorr   = subject_data_mot(subject_data_mot.prevCorrect == 0, :);
        subject_data_mot_prev_corr     = subject_data_mot(subject_data_mot.prevCorrect == 1, :);
        
        % Calculate p_repeat for each prev stimulus level, for prev
        % incorrect
        subject_data_mot_prev_incorr_0 = subject_data_mot_prev_incorr( ...
            subject_data_mot_prev_incorr.currResponseDir == 0, :);
        subject_data_mot_prev_incorr_1 = subject_data_mot_prev_incorr( ...
            subject_data_mot_prev_incorr.currResponseDir == 1, :);
        
        p_repeat_prev_incorr(iSubject,iMotion) = (mean(subject_data_mot_prev_incorr_0.choiceRepetition)+mean(subject_data_mot_prev_incorr_1.choiceRepetition))/2;
        
        % Calculate p_repeat for each prev stimulus level, for prev correct
        subject_data_mot_prev_corr_0 = subject_data_mot_prev_corr( ...
            subject_data_mot_prev_corr.currResponseDir == 0, :);
        subject_data_mot_prev_corr_1 = subject_data_mot_prev_corr( ...
            subject_data_mot_prev_corr.currResponseDir == 1, :);

        p_repeat_prev_corr(iSubject,iMotion) = (mean(subject_data_mot_prev_corr_0.choiceRepetition)+mean(subject_data_mot_prev_corr_1.choiceRepetition))/2;
        
        %% p_repeat for previous stim level, split for prev confidence
        
        % Split data into previous low and high confidence
        subject_data_mot_prev_lowConf      = subject_data_mot(subject_data_mot.prevConfidence <= 2, :);
        subject_data_mot_prev_highConf     = subject_data_mot(subject_data_mot.prevConfidence >= 3, :);
        
        % Calculate p_repeat for each prev stimulus level, for prev
        % low conf
        subject_data_mot_prev_lowConf_0 = subject_data_mot_prev_lowConf( ...
            subject_data_mot_prev_lowConf.currResponseDir == 0, :);
        subject_data_mot_prev_lowConf_1 = subject_data_mot_prev_lowConf( ...
            subject_data_mot_prev_lowConf.currResponseDir == 1, :);
        
        p_repeat_prev_lowConf(iSubject,iMotion) = (mean(subject_data_mot_prev_lowConf_0.choiceRepetition)+mean(subject_data_mot_prev_lowConf_1.choiceRepetition))/2;
        
        % Calculate p_repeat for each prev stimulus level, for prev 
        % high conf
        subject_data_mot_prev_highConf_0 = subject_data_mot_prev_highConf( ...
            subject_data_mot_prev_highConf.currResponseDir == 0, :);
        subject_data_mot_prev_highConf_1 = subject_data_mot_prev_highConf( ...
            subject_data_mot_prev_highConf.currResponseDir == 1, :);

        p_repeat_prev_highConf(iSubject,iMotion) = (mean(subject_data_mot_prev_highConf_0.choiceRepetition)+mean(subject_data_mot_prev_highConf_1.choiceRepetition))/2;
        
        
        %% p_repeat for previous stim level, split for prev confidence
        
        % Figure out the most equal split
        for i = 1:3
            temp(i) = abs(sum(subject_data_mot.prevConfidence <= i) - sum(subject_data_mot.prevConfidence >= i+1));
        end

        [~,crit] = min(temp);
        
        % Split data into previous low and high confidence
        subject_data_mot_prev_lowConf_2      = subject_data_mot(subject_data_mot.prevConfidence <= crit, :);
        subject_data_mot_prev_highConf_2     = subject_data_mot(subject_data_mot.prevConfidence >= crit+1, :);
        
        % Calculate p_repeat for each prev stimulus level, for prev
        % incorrect
        subject_data_mot_prev_lowConf_0_2 = subject_data_mot_prev_lowConf_2( ...
            subject_data_mot_prev_lowConf_2.currResponseDir == 0, :);
        subject_data_mot_prev_lowConf_1_2 = subject_data_mot_prev_lowConf_2( ...
            subject_data_mot_prev_lowConf_2.currResponseDir == 1, :);
        
        p_repeat_prev_lowConf_2(iSubject,iMotion) = (mean(subject_data_mot_prev_lowConf_0_2.choiceRepetition)+mean(subject_data_mot_prev_lowConf_1_2.choiceRepetition))/2;
        
        % Calculate p_repeat for each prev stimulus level, for prev correct
        subject_data_mot_prev_highConf_0_2 = subject_data_mot_prev_highConf_2( ...
            subject_data_mot_prev_highConf_2.currResponseDir == 0, :);
        subject_data_mot_prev_highConf_1_2 = subject_data_mot_prev_highConf_2( ...
            subject_data_mot_prev_highConf_2.currResponseDir == 1, :);

        p_repeat_prev_highConf_2(iSubject,iMotion) = (mean(subject_data_mot_prev_highConf_0_2.choiceRepetition)+mean(subject_data_mot_prev_highConf_1_2.choiceRepetition))/2;
        
        
        %% p_repeat for previous stim level, split for prev confidence
       
        for conf = 1:4
            
            % Select data for one prev confidence level
            subject_data_mot_prev_conf{conf} = subject_data_mot(subject_data_mot.prevConfidence == conf, :);
        
            % Calculate p_repeat for each prev stimulus level, for prev
            % incorrect
            subject_data_mot_prev_conf_0{conf} = subject_data_mot_prev_conf{conf}( ...
                subject_data_mot_prev_conf{conf}.currResponseDir == 0, :);
            subject_data_mot_prev_conf_1{conf} = subject_data_mot_prev_conf{conf}( ...
                subject_data_mot_prev_conf{conf}.currResponseDir == 1, :);
        
            p_repeat_prev_conf{conf}(iSubject,iMotion) = (mean(subject_data_mot_prev_conf_0{conf}.choiceRepetition)+mean(subject_data_mot_prev_conf_1{conf}.choiceRepetition))/2;
        
        end

    end
end


%% Group means and ses

for iMotion = 1:length(direction_division)-1
    
    p_repeat_mean(1,iMotion)              = mean(p_repeat(:,iMotion));                               
        p_repeat_se(1,iMotion)            = std(p_repeat(:,iMotion))/sqrt(length(p_repeat(:,iMotion)));
    p_repeat_lowRt_mean(1,iMotion)        = mean(p_repeat_lowRt(:,iMotion));
        p_repeat_lowRt_se(1,iMotion)      = std(p_repeat_lowRt(:,iMotion))/sqrt(length(p_repeat_lowRt(:,iMotion)));
    p_repeat_highRt_mean(1,iMotion)       = mean(p_repeat_highRt(:,iMotion));
        p_repeat_highRt_se(1,iMotion)     = std(p_repeat_highRt(:,iMotion))/sqrt(length(p_repeat_highRt(:,iMotion)));
    p_repeat_incorr_mean(1,iMotion)       = nanmean(p_repeat_prev_incorr(:,iMotion));
        p_repeat_incorr_se(1,iMotion)     = nanstd(p_repeat_prev_incorr(:,iMotion))/sqrt(length(p_repeat_prev_incorr(~isnan(p_repeat_prev_incorr(:,iMotion)),iMotion)));
    p_repeat_corr_mean(1,iMotion)         = nanmean(p_repeat_prev_corr(:,iMotion));
        p_repeat_corr_se(1,iMotion)       = nanstd(p_repeat_prev_corr(:,iMotion))/sqrt(length(p_repeat_prev_corr(~isnan(p_repeat_prev_corr(:,iMotion)),iMotion)));
    p_repeat_lowConf_mean(1,iMotion)      = nanmean(p_repeat_prev_lowConf(:,iMotion));
        p_repeat_lowConf_se(1,iMotion)    = nanstd(p_repeat_prev_lowConf(:,iMotion))/sqrt(length(p_repeat_prev_lowConf(~isnan(p_repeat_prev_lowConf(:,iMotion)),iMotion)));
    p_repeat_highConf_mean(1,iMotion)     = nanmean(p_repeat_prev_highConf(:,iMotion));
        p_repeat_highConf_se(1,iMotion)   = nanstd(p_repeat_prev_highConf(:,iMotion))/sqrt(length(p_repeat_prev_highConf(~isnan(p_repeat_prev_highConf(:,iMotion)),iMotion)));
    p_repeat_lowConf_mean_2(1,iMotion)    = nanmean(p_repeat_prev_lowConf_2(:,iMotion));
        p_repeat_lowConf_se_2(1,iMotion)    = nanstd(p_repeat_prev_lowConf_2(:,iMotion))/sqrt(length(p_repeat_prev_lowConf_2(~isnan(p_repeat_prev_lowConf_2(:,iMotion)),iMotion)));
    p_repeat_highConf_mean_2(1,iMotion)     = nanmean(p_repeat_prev_highConf_2(:,iMotion));
        p_repeat_highConf_se_2(1,iMotion)   = nanstd(p_repeat_prev_highConf_2(:,iMotion))/sqrt(length(p_repeat_prev_highConf_2(~isnan(p_repeat_prev_highConf_2(:,iMotion)),iMotion)));

    for conf = 1:4
        p_repeat_conf_mean{conf}(1,iMotion) = nanmean(p_repeat_prev_conf{conf}(:,iMotion));
        p_repeat_conf_se{conf}(1,iMotion)   = nanstd(p_repeat_prev_conf{conf}(:,iMotion))/sqrt(length(p_repeat_prev_conf{conf}(~isnan(p_repeat_prev_conf{conf}(:,iMotion)),iMotion)));

    end

end

%{
   % cut 6th stimulus level out of p_repeat_incorr_mean, because of too
    % little data going into it
    p_repeat_incorr_mean    = p_repeat_incorr_mean(:, 1:end-1);
    p_repeat_incorr_se      = p_repeat_incorr_se(:, 1:end-1);
  %}
  
%% Plots

if plots_p_repeat == true
    
    % p_repeat per previous stimulus level   
    figure; hold on;
        plot(p_repeat_mean); 
        errorbar(p_repeat_mean,p_repeat_se,'.b'); 
        ylim([.42 .60]); 
        xlim([0.25 5.75]); 
        set(gca,'XTick',1:5,'XTickLabel',{'smallest','','','','largest'});
        xlabel('Motion difference from previous trial (deg)')
        ylabel('p(repeat)')
        title('Choice repetition per previous relative stimulus coherence');

    % p_repeat per previous stimulus level, split for prev RT  
    figure; hold on;
        x = plot(p_repeat_lowRt_mean,'b'); 
            errorbar(p_repeat_lowRt_mean,p_repeat_lowRt_se,'.b');
        y = plot(p_repeat_highRt_mean,'r');
            errorbar(p_repeat_highRt_mean,p_repeat_highRt_se,'.r');
        ylim([.42 .60]); 
        xlim([0.25 5.75]); 
        set(gca,'XTick',1:5,'XTickLabel',{'smallest','','','','largest'});
        xlabel('Motion difference from previous trial (deg)')
        ylabel('p(repeat)')
        legend([x,y], 'Previous low RT', 'Previous high RT');
        title('Choice repetition per previous relative stimulus coherence');

    % p_repeat per previous stimulus level, split for prev correctness  
    figure; hold on;
        x = plot(p_repeat_corr_mean,'b'); 
            errorbar(p_repeat_corr_mean,p_repeat_corr_se,'.b');
        y = plot(p_repeat_incorr_mean,'r'); 
            errorbar(p_repeat_incorr_mean,p_repeat_incorr_se,'.r');
        ylim([.42 .60]);
        xlim([0.25 5.75]); 
        set(gca,'XTick',1:5,'XTickLabel',{'smallest','','','','largest'});
        xlabel('Motion difference from previous trial (deg)')
        ylabel('p(repeat)')
        legend([x,y], 'Previous correct response', 'Previous incorrect response','Location','northwest');
        title('Choice repetition per previous relative stimulus coherence');

    % p_repeat per previous stimulus level, split for prev confidence  
    figure; hold on;
        x = plot(p_repeat_lowConf_mean,'b'); 
            errorbar(p_repeat_lowConf_mean,p_repeat_lowConf_se,'.b');
        y = plot(p_repeat_highConf_mean,'r'); 
            errorbar(p_repeat_highConf_mean,p_repeat_highConf_se,'.r');
        ylim([.42 .60]);
        xlim([0.25 5.75]); 
        set(gca,'XTick',1:5,'XTickLabel',{'smallest','','','','largest'});
        xlabel('Motion difference from previous trial (deg)')
        ylabel('p(repeat)')
        legend([x,y], 'Previous low confidence', 'Previous high confidence','Location','northeast');
        title('Choice repetition per previous relative stimulus coherence');

    % p_repeat per previous stimulus level, split for prev confidence  
    figure; hold on;
        x = plot(p_repeat_lowConf_mean_2,'b'); 
            errorbar(p_repeat_lowConf_mean_2,p_repeat_lowConf_se_2,'.b');
        y = plot(p_repeat_highConf_mean_2,'r'); 
            errorbar(p_repeat_highConf_mean_2,p_repeat_highConf_se_2,'.r');
        ylim([.42 .60]);
        xlim([0.25 5.75]); 
        set(gca,'XTick',1:5,'XTickLabel',{'smallest','','','','largest'});
        xlabel('Motion difference from previous trial (deg)')
        ylabel('p(repeat)')
        legend([x,y], 'Previous low confidence', 'Previous high confidence','Location','northwest');
        title('Choice repetition per previous relative stimulus coherence');
  

    % p_repeat per previous stimulus level, split for prev confidence 
    % 4 LEVELS
    figure; hold on;
        k = plot(p_repeat_conf_mean{1},'g'); 
            errorbar(p_repeat_conf_mean{1},p_repeat_conf_se{1},'.g');
        l = plot(p_repeat_conf_mean{2},'c'); 
            errorbar(p_repeat_conf_mean{2},p_repeat_conf_se{2},'.c');
        m = plot(p_repeat_conf_mean{3},'b'); 
            errorbar(p_repeat_conf_mean{3},p_repeat_conf_se{3},'.b');
        n = plot(p_repeat_conf_mean{4},'m'); 
            errorbar(p_repeat_conf_mean{4},p_repeat_conf_se{4},'.m');
        ylim([.4 .70]);
        xlim([0.25 5.75]); 
        set(gca,'XTick',1:5,'XTickLabel',{'smallest','','','','largest'});
        xlabel('Motion difference from previous trial (deg)')
        ylabel('p(repeat)')
        legend([k,l,m,n], 'Prev confidence = 1', 'Prev confidence = 2', 'Prev confidence = 3', 'Prev confidence = 4', 'Location','northwest');
        title('Choice repetition per previous relative stimulus coherence');

end