%% restructure for figure 3. This file is called in Figure_3.m
%% plotting error angle and AI to target location:
clearvars
OVERWRITE = 0;                                                              % redo restructuring for Figure 3
currentFolder = pwd;
addpath(genpath('Matlab_functions')
path = currentFolder(1:end-8);

tic
CW = [-1,-1,-1,1,1,1,-1,1,1,-1,-1,1,-1,1,-1,1,1,1,-1,-1,-1,1,1,-1];      % -1 no/CCW-CW; 1 yes/CW-CCW 
%% loading in the error angle data:
file = sprintf('%s%s',path,'data\processed\','UpdateAngle.mat');
load(file)
%% loading in the processed AI data:
if ~exist('SU')
    for s = [1:10,12:24]
        clear subject
        file = sprintf('%s%ss%03d.mat',path,'data\processed\',s);
        load(file)
        SU{s} = subject;
    end
end
%% Loading in the colour matrix:
% col(shade,RGBcode,colour)
[~,col] =DefColours(5,5);
%% restructuring the data to simplify plotting:
%% error angle & AI
%         pp    trials  blocks  trialtype 
Err = NaN(24,   350,    6,      4); % error angle
AI =  NaN(24,   350,    6,      4); % adaptation index
% trial type: baseline, first force field, second force field 
% block: baseline followed by the 5 learning blocks.
%% relevant trialtypes:
%       baseline    ff 1     ff 2
tt_AI  = [5,        3,       6];
tt_Err = [0,        1,       2];
%% baseline trials:
if exist(sprintf('%s%s%',path,'data\restructured_Fig_3.mat'),'file')~=2 || OVERWRITE
       %% learning trials:
    for  s = [1:10,12:24]
        tt = 1;
        block = 0;
        ind_error =find(SU{s}.block == block & SU{s}.TT ==(tt-1) & SU{s}.TA == 0);
        ind_error(ind_error>500) =[]; % washout would otherwise be included in baseline;
        % find relevant trials (per block, trialtype, and training target)
        ind_AI = find(SU{s}.block == block & SU{s}.TT ==tt_AI(tt) & SU{s}.TA == 0);
        ind_AI(ind_AI>500) =[]; % washout would otherwise be included in baseline;
        AI(s,ind_AI,block+1,tt) = CW(s)*SU{s}.r(ind_AI);
        Err(s,ind_error,block+1,tt) = -ThetaInDeg(s,ind_error);% negative because in vBOT CW is positive, so relative to vBOT coordinate system;

        for block = 1:5
            % find first trial of the block to allign the rest of the data to:
            first_trial_of_the_block = find(SU{s}.block == block,1);
            
            
            % when learning:
            for tt =2:3
                %% find relevant learning trails
                ind_error =find(SU{s}.block == block & SU{s}.TT ==tt_Err(tt) & SU{s}.TA == 0);
                %% find relevant clamp trails
                ind_AI = find(SU{s}.block == block & SU{s}.TT ==tt_AI(tt) & SU{s}.TA == 0);
                if tt == 2
                    %% first force field: restructuring
                    Err(s,ind_error-first_trial_of_the_block+1,block+1,tt) =-ThetaInDeg(s,ind_error);
                    % negative because in vBOT CW is positive, so relative to vBOT coordinate system;
                    AI(s,ind_AI-first_trial_of_the_block+1,block+1,tt) = CW(s)*SU{s}.r(ind_AI);
                elseif tt == 3
                    %% in the second force field we allign the trials to the first, and last trial of B:
                    
                    %% allign to start or end of B
                    ind_end_of_B = max([ind_AI;ind_error]);
                    ind_B = sort([ind_AI;ind_error]);
                    if length(ind_B) < 15
                        %% pp 7 has 1 less trial
                        ind_begin = ind_B(1:14);
                        ind_end = ind_B(end-[0:13]);
                    else
                        ind_begin = ind_B(1:15);
                        ind_end = ind_B(end-[0:14]);
                    end
                    %% find the learning trials of B alligned to the beginning
                    x = intersect(ind_begin,ind_error);
                    y = -ThetaInDeg(s,x);
                    %% add them to Err
                    Err(s,x-first_trial_of_the_block+1,block+1,tt) =y;
                    %% find the learning trials of B alligned to the end
                    x = intersect(ind_end,ind_error);
                    y = -ThetaInDeg(s,x);
                    %% add them to Err
                    Err(s,300+x-ind_end_of_B,block+1,tt+1) =y;
                    %% same for clamp trials:
                    %% find the clamp trials of B alligned to the beginning
                    x = intersect(ind_begin,ind_AI);
                    y = CW(s)*SU{s}.r(x);
                    AI(s,x-first_trial_of_the_block+1,block+1,tt) = y;
                    %% find the clamp trials of B alligned to the end
                    x = intersect(ind_end,ind_AI);
                    y = CW(s)*SU{s}.r(x);
                    AI(s,300+x-ind_end_of_B,block+1,tt) = y;
                    
                    
                end
            end
            
        end
    end
    save(sprintf('%s%s%',path,'data\restructured_Fig_3.mat'),'AI','Err')
end