function [p, medians_all, AttInd_all] = figure4_5;


%
% function [p, medians_all, AttInd_all] = figure4_5;
%
% plots figure 4 and 5 of Marx, S., & Einhäuser, W. (2015). Reward modulates perception in binocular rivalry. Journal of Vision, 15(1):11, 1–13,
% http://www.journalofvision.org/content/15/1/11, doi:10.1167/15.1.11. 
% and returns data and statistics as given in the paper
%

Sub_num = 8; 

% analyze data of all participants

medians_all     = NaN(12,Sub_num);

SwitchRate = [];

d = load('exp2_data.mat');
%table columns in data structure: direction of moving grating, left red, task red, task green,
%reward left, reward right

for k = 1:Sub_num
    
    % preprocess individual subject data
    [DomDurs_med, switchrate] = exp2_individual(d.data(k).DomDurs,d.data(k).ind,1);
    
    % dominance duraton medians: attrew, attrewoth, attpun, attpunoth, unattrew, unattrewoth,
    %  unattpun, unattpunoth, bothattrew, bothattrewoth,bothattpun, bothattpunoth
    % switchrate conditions: attended = rewarded, attended~= rewarded, both
    % attended and one rewarded, attended = punished, attended ~= punished,
    % both attended and one punished
    
    medians_all(:,k)    = reshape(DomDurs_med',12,1);

    SwitchRate          = [SwitchRate;switchrate];

    
end

% define indices that indicate conditions
ind_rew = [1 5 9];
ind_rew_oth = [2 6 10];
ind_pun = [3 7 11];
ind_pun_oth = [4 8 12];


means_allmedians    = mean(medians_all,2);
sem_allmedians      = std(medians_all,1,2)./sqrt(Sub_num); % standard error

diff_medians = [medians_all(ind_rew,:)-medians_all(ind_rew_oth,:); medians_all(ind_pun_oth,:)-medians_all(ind_pun,:)];


%% plots
% plot reward/punishment at different attentional conditions in different
% graphs

figure(4);

s1 = subplot(1,3,1);
bar([1 4 7], means_allmedians(ind_rew),0.2,'b')
hold on
bar([2 5 8], means_allmedians(ind_rew_oth),0.2,'c')
errorbar([1 4 7], means_allmedians(ind_rew), sem_allmedians(ind_rew),'ob','LineWidth',2)
errorbar([2 5 8], means_allmedians(ind_rew_oth), sem_allmedians(ind_rew_oth),'oc','LineWidth',2)
legend('rewarded', 'not rewarded')
set(gca, 'XTick',1:8)
set(gca,'XTickLabel', { 'full', ' ',' ', 'away', ' ',' ', 'split', ' '},'FontSize',14)
ylabel('normalized dominance duration','FontSize',14)
axis([0 9 0 4.5])

s2 = subplot(1,3,2);
h = bar([1 4 7], means_allmedians(ind_pun),0.2,'w');
set(h,'FaceColor',[1 0.5 0])
hold on
bar([2 5 8], means_allmedians(ind_pun_oth),0.2,'r')
errorbar([1 4 7], means_allmedians(ind_pun), sem_allmedians(ind_pun),'o','Color',[1 0.5 0],'LineWidth',2)
hold on;
errorbar([2 5 8], means_allmedians(ind_pun_oth), sem_allmedians(ind_pun_oth),'or','LineWidth',2)
legend('punished', 'not punished')

set(gca, 'XTick',1:8)
set(gca,'XTickLabel', { 'full', ' ',' ', 'away', ' ',' ', 'split', ' '},'FontSize',14)
ylabel('normalized dominance duration','FontSize',14)
axis([0 9 0 4.5])


% plot differences between reward conditions and punishment conditions
s3 = subplot(1,3,3);
bar([1 4 7],mean(diff_medians(1:3,:),2),0.2,'b')
hold on
hc = bar([2 5 8],mean(diff_medians(4:6,:),2),0.2,'w');
set(hc,'FaceColor',[0 0 0.5])
errorbar([1 4 7],mean(diff_medians(1:3,:),2),std(diff_medians(1:3,:),0,2)./sqrt(Sub_num),'ob','LineWidth',2);
errorbar([2 5 8],mean(diff_medians(4:6,:),2),std(diff_medians(4:6,:),0,2)./sqrt(Sub_num),'o','Color',[0 0 0.5],'LineWidth',2);
% axis([0 8 0 2.5])
ylabel('diff. dom dur','FontSize',14)
legend('rewarded - unrewarded','not punished - punished','Location','NorthWest')
set(gca,'XTick', 1:8)
set(gca,'XTickLabel',{'full', ' ',' ', 'away' ,' ',' ','split', ' ' },'FontSize',14);

linkaxes([s1 s2 s3])



%% plot switchrate

% plot alternation rate in the rewarded stimulus and the unpunished
% stimulus
figure(5)
bar([1:3],mean(SwitchRate(:,1:3)),0.2,'b')
hold on
hc = bar([5:7],mean(SwitchRate(:,[5 4 6])),0.2,'w');
set(hc,'FaceColor',[0 0 0.5])
errorbar([1:3],mean(SwitchRate(:,1:3)),std(SwitchRate(:,1:3))./sqrt(Sub_num),'ob','LineWidth',2);
errorbar([5:7],mean(SwitchRate(:,[5 4 6])),std(SwitchRate(:,[5 4 6]))./sqrt(Sub_num),'o','Color',[0 0 0.5],'LineWidth',2);

set(gca,'XTick', 1:7)
set(gca,'XTickLabel',{'att' 'unatt' 'split' ' '  'att' 'unatt' 'split'});

ylabel('alternation rate [1/s]')
legend('rewarded' ,'not punished')