function [] = analyzeMain

%
% function [] = analyzeMain
%
% Analysis of data as in
%
% Veto, Peter & Uhlig, Marvin & Troje, Nikolaus F. & Einhäuser, Wolfgang.
% (2018). Cognition modulates action-to-perception transfer in ambiguous
% perception. Journal of Vision. 18(8),5. 
% https://doi.org/10.1167/18.8.5
%
% 
% Verbose Matlab function ("belt", "gear", "congruent",
% "incongruent" as defined in article).

subjects_all = 32;

load('subjects_list.mat'); % list of participant and block order; 1-subj, 2-block, 3-belt_or_gear, 4-dir
ind_temp = [];
blocks = [];
for subj = 1:subjects_all
    
    % find belt_congruent blocks (belt_or_gear = 1; dir = 1)
    ind_temp = find(subjects_list(:,1)==subj & subjects_list(:,3)==1 & subjects_list(:,4)==1);
    blocks = subjects_list(ind_temp,2);
    dd_belt_congruent(subj) = analysis_repos_veto_block(subj,blocks); % get dominance durations (dd)
    ind_temp = [];
    blocks = [];
    
    % find belt_incongruent blocks (belt_or_gear = 1; dir = 2)
    ind_temp = find(subjects_list(:,1)==subj & subjects_list(:,3)==1 & subjects_list(:,4)==2);
    blocks = subjects_list(ind_temp,2);
    dd_belt_incongruent(subj) = analysis_repos_veto_block(subj,blocks); % get dominance durations (dd)
    ind_temp = [];
    blocks = [];
    
    % find gear_congruent blocks (belt_or_gear = 2; dir = 2 <- congruent is
    % opposite direction in GEAR condition!)
    ind_temp = find(subjects_list(:,1)==subj & subjects_list(:,3)==2 & subjects_list(:,4)==2);
    blocks = subjects_list(ind_temp,2);
    dd_gear_congruent(subj) = analysis_repos_veto_block(subj,blocks); % get dominance durations (dd)
    ind_temp = [];
    blocks = [];
    
    % find gear_incongruent blocks (belt_or_gear = 2; dir = 1)
    ind_temp = find(subjects_list(:,1)==subj & subjects_list(:,3)==2 & subjects_list(:,4)==1);
    blocks = subjects_list(ind_temp,2);
    dd_gear_incongruent(subj) = analysis_repos_veto_block(subj,blocks); % get dominance durations (dd)
    ind_temp = [];
    blocks = [];
    
end

% uncomment to show means and SDs for each condition:
% disp('~~~~~~~~~~~~~~~~~');
% disp('BELT');
% disp('mean congruent:');
% mean(dd_belt_congruent)
% disp('SD congruent:');
% std(dd_belt_congruent)
% disp('mean incongruent:');
% mean(dd_belt_incongruent)
% disp('SD incongruent:');
% std(dd_belt_incongruent)
% disp('~~~~~~~~~~~~~~~~~');
% disp('GEAR');
% disp('mean congruent:');
% mean(dd_gear_congruent)
% disp('SD congruent:');
% std(dd_gear_congruent)
% disp('mean incongruent:');
% mean(dd_gear_incongruent)
% disp('SD incongruent:');
% std(dd_gear_incongruent)

% uncomment to save results structured for analysis in SPSS etc.:
% save_for_spss = [dd_belt_congruent.',dd_belt_incongruent.',dd_gear_congruent.',dd_gear_incongruent.'];
% save('spss_anova.mat','save_for_spss');

% uncomment to show results of t-tests:
% disp('~~~~~~~~~~~~~~~~~');
% disp('t-tests:');
% disp('BELT');
% [~, p_belt,~, stats_belt] = ttest(dd_belt_congruent, dd_belt_incongruent);
% p_belt
% stats_belt
% disp('GEAR');
% [~, p_gear,~, stats_gear] = ttest(dd_gear_congruent, dd_gear_incongruent);
% p_gear
% stats_gear

% Plot results:

% SEM
sem_belt_c = std(dd_belt_congruent)/sqrt(subjects_all);
sem_belt_i = std(dd_belt_incongruent)/sqrt(subjects_all);
sem_gear_c = std(dd_gear_congruent)/sqrt(subjects_all);
sem_gear_i = std(dd_gear_incongruent)/sqrt(subjects_all);

% Figure
figure;
p = plot([1,2], [mean(dd_belt_congruent),mean(dd_belt_incongruent)],'bo-');
hold on
p = errorbar(1,mean(dd_belt_congruent),sem_belt_c,'b');
hold on
p = errorbar(2,mean(dd_belt_incongruent),sem_belt_i,'b');
hold on
p = plot([1,2], [mean(dd_gear_congruent),mean(dd_gear_incongruent)],'ro-');
hold on
p = errorbar(1,mean(dd_gear_congruent),sem_gear_c,'r');
hold on
p = errorbar(2,mean(dd_gear_incongruent),sem_gear_i,'r');

% Figure properties
set(gca, 'xlim', [0 3], 'xtick', [1 2], 'xticklabel', ['Congr.';'Incong']);
set(gca, 'ylim', [2.5 5.5], 'box', 'off', 'TickDir','out');
title('Dominance durations');
xlabel('Condition');
ylabel('Median dominance duration (s)');

end

