function [] = figure2

% creates figure 2 of
% Einhäuser, W., Sandrock, A., & Schütz, A. (2021).
% Perceptual Difficulty Persistently Increases Dominance in Binocular Rivalry - Even Without a Task.
% Perception, 50(4), 343-366. https://doi.org/10.1177/0301006621999929

nSubs = 24;

percCorrect  = nan(nSubs,4);
medianRT     = nan(nSubs,4);

levelNames = {'both easy','blue hard','red hard','both hard'};

load allData isCorrect diffRed diffBlue idxButton

for subNum = 1 : nSubs
    
    % reaction time in s
    RT = idxButton(subNum,5:12,:)/1000;
    
    % difficulty level
    % 1- both easy
    % 2- blue hard
    % 3- red hard
    % 4- both hard
    
    diffLev = 2*(diffRed(subNum,5:12,:)-1) + diffBlue(subNum,5:12,:);
    correct = isCorrect(subNum,5:12,:);
    
    % loop across difficulty levels
    for levCount = 1 : 4
        percCorrect(subNum,levCount)  = 100*mean(correct(diffLev==levCount));
        idx = ((diffLev==levCount)&correct);
        medianRT(subNum,levCount) = median(RT(idx));
    end
    
    
end

figure(2);
clf;
subplot(1,2,1);
bar([1:4],mean(percCorrect));
hold on;
errorbar([1:4],mean(percCorrect),std(percCorrect)/sqrt(size(percCorrect,1)),'ko');
xlabel('difficulty');
set(gca,'XTick',[1:4]);
set(gca,'XTickLabel',levelNames);
ylabel('% correct');
xlim([0.5,4.5]);
ylim([50 100]);
set(gca,'YTick',[50:10:100]);
subplot(1,2,2);
hold on;
bar([1:4],mean(medianRT));
errorbar([1:4],mean(medianRT),std(medianRT)/sqrt(size(medianRT,1)),'ko');
xlabel('difficulty');
ylabel('median RT (correct trials) [ms]');
xlim([0.5,4.5]);
set(gca,'XTick',[1:4]);
set(gca,'XTickLabel',levelNames);
ylim([0 2.5]);
set(gca,'YTick',[0:.5:2.5]);
