function [] = figure6_human;

%
% function [] = figure6_human;
%
% plots the rightmost column of figure 6 of the paper, based on the
% data file dataExp2.mat
%
% Marx, S., Gruenhage, G., Walper, D., Rutishauser, U., Einhäuser,
% W. (2015). Competition with and without priority control: linking
% rivalry to attention through winner-take-all networks with
% memory. Annals of the New York Academy of Sciences. 1339, 138-153.



figure(6);
clf;

load('dataExp2');

% loop over contrasts, blank duraitons and subjects
for cL = 1 : 3
    for bL = 1 : 4
        for s = 1 : 5

            % count survivals and switches
            surv = 0;
            swit = 0;
            perz = prevPerz(cL,bL,s);
            % loop over all blanks
            for trial = 1: length(presStart{cL,bL,s})

                but_interval = whichButton{cL,bL,s}((buttonStart{cL,bL,s} >= presStart{cL,bL,s}(trial)) & (buttonStart{cL,bL,s} < presEnd{cL,bL,s}(trial)));
                % check if button was pushed between start of presentation and
                % end of blanking
                if isempty(but_interval)
                    % since some blanking times are very short, subjects could
                    % have pushed the button longer (like in continuous
                    % presentation) --> define new button time
                    but_interval2 = whichButton{cL,bL,s}((buttonStart{cL,bL,s} <= presStart{cL,bL,s}(trial)) & (buttonEnd{cL,bL,s} >= presEnd{cL,bL,s}(trial)));
                    if isempty(but_interval2)
                        perz = 0;
                    elseif but_interval2 == perz
                        surv=surv+1;
                        perz = but_interval2(end);
                    elseif but_interval2 ~= perz
                        swit = swit+1;
                        perz = but_interval2(end);
                    end
                elseif but_interval(1,1) == perz
                    surv = surv+1;
                    perz = but_interval(end);
                elseif but_interval(1,1) ~= perz
                    swit = swit+1;
                    perz = but_interval(end);
                end
            end

            numbPresent = length(presStart{cL,bL,s});
            SurvProb(cL,bL,s) = surv/numbPresent;
            SwitchProb(cL,bL,s) = swit/numbPresent;

        end
    end
end

% now compute the statistics for plotting

SurvProb_exp = nanmean(SurvProb,3);
SwitchProb_exp = nanmean(SwitchProb,3);
SurvProb_exp_std = nanstd(SurvProb,0,3);
SwitchProb_exp_std = nanstd(SwitchProb,0,3);
Sum_std = nanstd(SurvProb+SwitchProb,0,3);

subplot(3,1,1)
errorbar(SurvProb_exp',SurvProb_exp_std'./sqrt(5))
set(gca,'XTick',1:4);
set(gca,'XTickLabel',[0.5 1 2 4]);
xlim([0.5 4.5]);
ylim([0 1]);
xlabel('blanking duration [1/s]');
ylabel('survival probability');
legend('contrast 2', 'contrast 4', 'contrast 6','Location','SouthEast')

subplot(3,1,2)
errorbar(SwitchProb_exp',SwitchProb_exp_std'./sqrt(5))
set(gca,'XTick',1:4);
set(gca,'XTickLabel',[0.5 1 2 4]);
xlim([0.5 4.5]);
ylim([0 0.45]);
xlabel('blanking duration [1/s]');
ylabel('switch probability');

subplot(3,1,3)
errorbar([SwitchProb_exp + SurvProb_exp]',Sum_std'./sqrt(5))
set(gca,'XTick',1:4);
set(gca,'XTickLabel',[0.5 1 2 4]);
xlim([0.5 4.5]);
ylim([0.2 1]);
xlabel('blanking duration [1/s]');
ylabel('switch probability');

