% -------------------------------------------------------------------------
%
%   Demo for analysing cue integration to test for optimality
%
%   The data that is used for analysis was collected from real human
%   participants (students of the university of Bielefeld) using the visual
%   slant demo provided with this package.
%
%   by Loes van Dam, 2014
%   Loes.van_Dam@uni-bielefeld.de
%
% -------------------------------------------------------------------------

close all; clear all; clc;  % clear the memory etc

% -------------------------------------------------------------------------
%   Specify the data files to be used in a cellarray
% -------------------------------------------------------------------------

foldername = 'matfiles';

filelst2Base = { 'Participant_1', 'Participant_2', 'Participant_3', 'Participant_4', ...
                 'Participant_5', 'Participant_6', 'Participant_7', 'Participant_8', ...
                 'Participant_9' ,'Participant_10','Participant_11','Participant_12',...
                 'Participant_13','Participant_14'};

% -------------------------------------------------------------------------
%   load the matrix for a single participant
%
%   Remember: Each row in the datafile matrix will signify a single trial
%   The numbers within each row corresponds to:
%   [1] cues used: 0 = texture only, 1 = disparity only; 2 = both
%   [2] standard slant
%   [3] conflict in standard slant
%   [4] comparison slant
%   [5] which first 0 = base slant first; 1 = comparison first
%   [6] response 0 = turned leftward; 1 = turned rightward
% -------------------------------------------------------------------------

filelst = filelst2Base;

alldatlst =[];
for sub = 1:length(filelst)
    load([foldername,filesep,filelst{sub},'.mat']);

    % the following switches the responses 0 into 1 and vice versa for trials 
    % with baseslant first. This way the responses are changed into comparison
    % more rightward responses rather than based on the interval order.
    Trial_List(Trial_List(:,5) == 0,6) = abs(Trial_List(Trial_List(:,5) == 0,6)-1);

    if sub == 1,
        standard_conditions = unique(Trial_List(:,2));      % how many baseslant conditions were there
    end

    % -------------------------------------------------------------------------
    %   Put into right format for the Analysis
    %
    %   First put the matrix in the right format for the analysis:
    %   [C-S, P(C>S), N, S, [cues on/off], [Conf added], [noise level], subject]
    %
    %   since for slant-perception a different baseslant leads to differences
    %   in JND, we will treat the different baseslants as different noise
    %   conditions instead of different standard conditions
    % -------------------------------------------------------------------------

    nrtrials = length(Trial_List(:,1));
    datlst = [  Trial_List(:,[4,6]),...
                ones(nrtrials,1),...
                Trial_List(:,2)*0,...
                Trial_List(:,1) == 0 | Trial_List(:,1) == 2,...
                Trial_List(:,1) == 1 | Trial_List(:,1) == 2,...
                 Trial_List(:,3)/2,...
                -Trial_List(:,3)/2,...
                Trial_List(:,2),...
                Trial_List(:,2),...
                sub*ones(nrtrials,1)
                ];

    alldatlst = [alldatlst;datlst];
end


% -------------------------------------------------------------------------
%   nrcues was 2 (perspective & binocular disparity)
% -------------------------------------------------------------------------

nrCues = 2;

% -------------------------------------------------------------------------
%   Do MLE analysis for each baseslant
% -------------------------------------------------------------------------

ploton = 1;
colstring = 'rbmgcyk';

cd ..;
MLE_Analysis(alldatlst,2,0,0,[0,0,1])