clear all
close all

load('/Users/leonieoostwoudwijdenes/surfdrive/Experiments/Donders/ManualReflex/RDM/derivatives/Experiment1Data')
load('/Users/leonieoostwoudwijdenes/surfdrive/Experiments/Donders/ManualReflex/RDM/derivatives/SimulationAcc')
%positions in mm

% constants
sf = 250;
FilterOrder = 2;
FilterCutoff = 10;
vcrit = 50; %mm/s
vcrit_lenient = 100;
vcrit_extra_lenient = 150;
vcrit_sled = 3;
t_ax = 1:4:1000;
trials = length(trial);

sled = [0.95 1.3];
target_pos = [-96 96];
target_pos3 = [-96 0 96];
jumps = [0 0.1 0.2 0.35];
sled_end = [0.2 -0.3];
trial_split = 292;

% predefine variables
hand_pos_filt = NaN(trials,max_t,3);
sled_pos_filt = NaN(trials,max_t,3);
hand_vel = NaN(trials,max_t);
sled_vel = NaN(trials,max_t);
sled_stop = NaN(trials,1);
hand_vel_rel = NaN(trials,max_t);
hand_vel_rel_x = NaN(trials,max_t);
hand_vel_rel_y = NaN(trials,max_t);
hand_move_pos = NaN(trials,250,3);
ref_pos_x = NaN(trials,500);
ref_pos_y = NaN(trials,500);
ref_vel_x = NaN(trials,500);
ref_vel_y = NaN(trials,500);
corr_pos = NaN(trials,200,3);
corr_vel = NaN(trials,200,1);
mi = NaN(trials,1);
me = NaN(trials,1);
me_v = NaN(trials,1);
me_y = NaN(trials,1);
me_pos = NaN(trials,2);
PV = NaN(trials,1);
PVi = NaN(trials,1);
jump_pos = NaN(trials,2);
jump_rel_mi = NaN(trials,1);
jump_rel_me = NaN(trials,1);
jump_check = NaN(trials,1);
miss_marker = NaN(trials,1);
too_late = NaN(trials,1);
TT = NaN(trials,1);

%% reject (practice) trials
reject = repmat([ones(6,1); zeros(n_trials-6,1)],n_subj,1); %reject first 6 trials (practice)

%% filter hand and sled position
Wn=FilterCutoff/(.5*sf);
[B,A]=butter(FilterOrder,Wn); %filter 10 Hz
for i=1:trials
    ok = ~isnan(hand_pos(i,1:max_t,1));
    if any(ok)
        for j=1:3
            hand_pos_filt(i,ok,j) = filtfilt(B,A,hand_pos(i,ok,j));
        end
    end
    ok2 = ~isnan(sled_pos(i,1:max_t,1));
    if any(ok)
        for j=1:3
            sled_pos_filt(i,ok,j) = filtfilt(B,A,sled_pos(i,ok,j));
        end
    end
end

%% hand movement relative to sled
hand_pos_rel = hand_pos_filt-sled_pos_filt; %hand position relative to sled

%% define tangential velocity
for k=1:max_t
    if k==1
        hand_vel(1:trials,k)=(sqrt((hand_pos_filt(1:trials,k+1,1)-hand_pos_filt(1:trials,k,1)).^2+(hand_pos_filt(1:trials,k+1,2)-hand_pos_filt(1:trials,k,2)).^2+(hand_pos_filt(1:trials,k+1,3)-hand_pos_filt(1:trials,k,3)).^2))./(1/sf);
        sled_vel(1:trials,k)=(sqrt((sled_pos_filt(1:trials,k+1,1)-sled_pos_filt(1:trials,k,1)).^2+(sled_pos_filt(1:trials,k+1,2)-sled_pos_filt(1:trials,k,2)).^2+(sled_pos_filt(1:trials,k+1,3)-sled_pos_filt(1:trials,k,3)).^2))./(1/sf);
        hand_vel_rel(1:trials,k)=(sqrt((hand_pos_rel(1:trials,k+1,1)-hand_pos_rel(1:trials,k,1)).^2+(hand_pos_rel(1:trials,k+1,2)-hand_pos_rel(1:trials,k,2)).^2+(hand_pos_rel(1:trials,k+1,3)-hand_pos_rel(1:trials,k,3)).^2))./(1/sf);
        hand_vel_rel_x(1:trials,k)=squeeze((hand_pos_rel(1:trials,k+1,1)-hand_pos_rel(1:trials,k,1))./(1/sf));
        hand_vel_rel_y(1:trials,k)=squeeze((hand_pos_rel(1:trials,k+1,2)-hand_pos_rel(1:trials,k,2))./(1/sf));
    elseif k==max_t
        hand_vel(1:trials,k)=(sqrt((hand_pos_filt(1:trials,k,1)-hand_pos_filt(1:trials,k-1,1)).^2+(hand_pos_filt(1:trials,k,2)-hand_pos_filt(1:trials,k-1,2)).^2+(hand_pos_filt(1:trials,k,3)-hand_pos_filt(1:trials,k-1,3)).^2))./(1/sf);
        sled_vel(1:trials,k)=(sqrt((sled_pos_filt(1:trials,k,1)-sled_pos_filt(1:trials,k-1,1)).^2+(sled_pos_filt(1:trials,k,2)-sled_pos_filt(1:trials,k-1,2)).^2+(sled_pos_filt(1:trials,k,3)-sled_pos_filt(1:trials,k-1,3)).^2))./(1/sf);
        hand_vel_rel(1:trials,k)=(sqrt((hand_pos_rel(1:trials,k,1)-hand_pos_rel(1:trials,k-1,1)).^2+(hand_pos_rel(1:trials,k,2)-hand_pos_rel(1:trials,k-1,2)).^2+(hand_pos_rel(1:trials,k,3)-hand_pos_rel(1:trials,k-1,3)).^2))./(1/sf);
        hand_vel_rel_x(1:trials,k)=squeeze((hand_pos_rel(1:trials,k,1)-hand_pos_rel(1:trials,k-1,1))./(1/sf));
        hand_vel_rel_y(1:trials,k)=squeeze((hand_pos_rel(1:trials,k,2)-hand_pos_rel(1:trials,k-1,2))./(1/sf));
    else
        hand_vel(1:trials,k)=(sqrt((hand_pos_filt(1:trials,k+1,1)-hand_pos_filt(1:trials,k-1,1)).^2+(hand_pos_filt(1:trials,k+1,2)-hand_pos_filt(1:trials,k-1,2)).^2+(hand_pos_filt(1:trials,k+1,3)-hand_pos_filt(1:trials,k-1,3)).^2))./(2/sf);
        sled_vel(1:trials,k)=(sqrt((sled_pos_filt(1:trials,k+1,1)-sled_pos_filt(1:trials,k-1,1)).^2+(sled_pos_filt(1:trials,k+1,2)-sled_pos_filt(1:trials,k-1,2)).^2+(sled_pos_filt(1:trials,k+1,3)-sled_pos_filt(1:trials,k-1,3)).^2))./(2/sf);
        hand_vel_rel(1:trials,k)=(sqrt((hand_pos_rel(1:trials,k+1,1)-hand_pos_rel(1:trials,k-1,1)).^2+(hand_pos_rel(1:trials,k+1,2)-hand_pos_rel(1:trials,k-1,2)).^2+(hand_pos_rel(1:trials,k+1,3)-hand_pos_rel(1:trials,k-1,3)).^2))./(2/sf);
        hand_vel_rel_x(1:trials,k)=squeeze((hand_pos_rel(1:trials,k+1,1)-hand_pos_rel(1:trials,k-1,1))./(2/sf));
        hand_vel_rel_y(1:trials,k)=squeeze((hand_pos_rel(1:trials,k+1,2)-hand_pos_rel(1:trials,k-1,2))./(2/sf));
    end
end

%% define movement initiation & end
for i = 1:trials
    %reject
    if sum(isnan(hand_pos_rel(i,margin:margin+round(sled_move_time(i)*sf),1)))>0 %determine if marker is missing during sled motion reject trial
        reject(i) = 1;
        miss_marker(i) = sum(isnan(hand_pos_rel(i,margin:margin+round(sled_move_time(i)*sf),1)))>0;
    end
    
    %variables
    if ~reject(i)
        [PV(i), PVi(i)] = max(hand_vel_rel(i,margin:margin+round(sled_move_time(i)*sf))); %peak velocity hand during sled motion
        early_reverse = [44 68 126 128 140 174 177 210 225 291 294 378 473 474 486 497 498 508 515 525 557 562 1036 1098 1255 2880:3456 3504 3632:4032 5122 5761:6336];
        for j = 1:length(early_reverse)
            if i == early_reverse(j)
                [PV(i), PVi(i)] = max(hand_vel_rel(i,margin:margin+150)); %if hand moves back during forward motion of the sled
            end
        end
        PVi(i) = PVi(i)+margin-1;
        
        mi(i) = find(hand_vel_rel(i,1:PVi(i))<vcrit,1,'last'); %movement initiation
        me_v(i) = find(hand_vel_rel(i,PVi(i):end)<vcrit,1,'first')+PVi(i)-1; %tangential velocity criterion
        me_y(i) = find(hand_vel_rel_y(i,PVi(i):end)<0,1,'first')+PVi(i)-1; %y velocity criterion
        if me_v(i)<=me_y(i)
            me(i) = me_v(i); %movement end
        else
            me(i) = me_y(i);
        end
        
%         no_fullstop = [54 727 1203 1204 1212 1233 1250 1272 1388 1404 1496 1600 2880:3456 3516 3620 3709 3744 5761:6336];
%         for k = 1:length(no_fullstop)
%             if i == no_fullstop(k)
%                 me(i) = find(hand_vel_rel(i,PVi(i):end)<vcrit_lenient,1,'first')+PVi(i)-1; %if hand does not come to full stop before moving back
%             end
%         end
%         if i==2890 || i==2892  || i==2913 || i==2920 || i==2924 || i==3031 || i==3205 || i==3277 || i==5769 || i==5874 || i==5885 || i==6123 || i==6128 || i==6145 || i==6189
%             me(i) = find(hand_vel_rel(i,PVi(i):end)<vcrit_extra_lenient,1,'first')+PVi(i)-1; %if hand does not come to full stop before moving back
%         end
        
        sled_stop(i) = margin + sled_move_time(i)*sf;
%         if sled_stop(i) < me(i) %if hand did not stop moving before end of sled movement, take hand position at end of sled motion as end of movement
%             too_late(i) = 1;
%             me(i) = sled_stop(i);
%         end
        
        t_ix = size(mi(i):me(i),2);
        hand_move_pos(i,1:t_ix,:) = hand_pos_rel(i,mi(i):me(i),:);

        t_ix = 500-size(margin+1:me(i),2);
        ref_pos_x(i,:) = [hand_pos_rel(i,margin+1:me(i),1) repmat(hand_pos_rel(i,me(i),1),1,t_ix)];
        ref_vel_x(i,:) = [hand_vel_rel_x(i,margin+1:me(i)) repmat(hand_vel_rel_x(i,me(i)),1,t_ix)];
        ref_pos_y(i,:) = [hand_pos_rel(i,margin+1:me(i),2) repmat(hand_pos_rel(i,me(i),2),1,t_ix)];
        ref_vel_y(i,:) = [hand_vel_rel_y(i,margin+1:me(i)) repmat(hand_vel_rel_y(i,me(i)),1,t_ix)];
        me_pos(i,:) = [hand_pos_rel(i,me(i),1), hand_pos_rel(i,me(i),2)];

        if jump_timing(i)~=0
            t_ix = size((margin+diode(i,2)-diode(i,1)):me(i),2);
            corr_pos(i,1:t_ix,:) = hand_pos_rel(i,(margin+diode(i,2)-diode(i,1)):me(i),:); %cut trial at moment of target jump
            corr_vel(i,1:t_ix) = hand_vel_rel_x(i,(margin+diode(i,2)-diode(i,1)):me(i));
            jump_pos(i,:) = [215 680]-squeeze(hand_pos_rel(i,margin+diode(i,2)-diode(i,1),1:2))'; %distance in x and y to center target at time of jump
            jump_rel_mi(i) = (margin+diode(i,2)-diode(i,1) - mi(i))*(1000/sf); %time (ms) after movement start of jump
            jump_rel_me(i) = (me(i) - (margin+diode(i,2)-diode(i,1)))*(1000/sf); %time left until movement end from moment jump
            jump_check(i) = (diode(i,2)-diode(i,1))*(1000/sf) - (jump_timing(i)*1000); %difference in intended and actual moment of jump. positive is later than intended
        end
%         %plot jump positions
%         subplot(211),plot(1:1500,hand_pos_rel(i,:,1),'k',margin+diode(i,2)-diode(i,1),hand_pos_rel(i,(margin+diode(i,2)-diode(i,1)),1),'m*',[500 500],[180 260],'k--');text(100,200,[num2str((diode(i,2)-diode(i,1))*4) ' ms']);text(100,190,[num2str(jump_timing(i)*1000) ' ms'])
%         subplot(212),plot(1:1500,hand_pos_rel(i,:,2),'k',margin+diode(i,2)-diode(i,1),hand_pos_rel(i,(margin+diode(i,2)-diode(i,1)),2),'m*')
%         pause
        
        
        %plot
%         if i>0
% %         for j = 1:length(early_reverse)
% %             if i == early_reverse(j)
%                 subplot(211),plot(1:max_t,hand_vel_rel(i,:),'c',mi(i),hand_vel_rel(i,mi(i)),'k*',me(i),hand_vel_rel(i,me(i)),'b*');
%                 subplot(212),plot(1:max_t,hand_vel(i,:),'m',1:max_t,sled_vel(i,:),'r', [margin margin],[0 1000],'k-',[sled_stop(i) sled_stop(i)],[0 1000],'k-');
%                 text(0,100,num2str(i));
%                 pause
% %             end
%         end
    end
end

RT = (mi-margin)*(1000/sf); %reaction time
MT = (me-mi)*(1000/sf); %movement time
for i = 1:trials
    if RT(i)<0
        reject(i)=1;
    end
end
for j = 1:length(jumps)
    ix = jump_timing ==jumps(j) & ~reject;
    TT(ix) = RT(ix) + MT(ix) - (jumps(j)*1000); %time to target for each trial
end

%% define mean trajectories for no jump (control) trials
miss_marker_s = NaN(n_subj,1);
reject_s = NaN(n_subj,1);
ref_pos_m = NaN(n_subj,2,2,size(ref_pos_x,2));
ref_vel_m = NaN(n_subj,2,2,size(ref_pos_x,2));
ref_pos_sd = NaN(n_subj,2,2,size(ref_pos_x,2));
ref_vel_sd = NaN(n_subj,2,2,size(ref_pos_x,2));
ref_pos = NaN(trials,size(ref_pos_x,2));
ref_vel = NaN(trials,size(ref_pos_x,2));
pos_diff = NaN(trials,size(ref_pos_x,2));
vel_diff = NaN(trials,size(ref_pos_x,2));

%means of control trials
for s = 1:n_subj
    miss_marker_s(s) = nansum(miss_marker(subject==s));
    reject_s(s) = sum(reject(subject==s));
    for d = 1:length(sled)
        for f = 1:length(sled_end) %two sled final positions
            ref_pos_m(s,d,f,1:size(ref_pos_x,2)) = nanmean(ref_pos_x(subject==s & sled_move_time==sled(d) & jump_timing==0 & sled_final_pos==sled_end(f) & ~reject,:));
            ref_vel_m(s,d,f,1:size(ref_pos_x,2)) = nanmean(ref_vel_x(subject==s & sled_move_time==sled(d) & jump_timing==0 & sled_final_pos==sled_end(f) & ~reject,:));
            ref_pos_sd(s,d,f,1:size(ref_pos_x,2)) = nanstd(ref_pos_x(subject==s & sled_move_time==sled(d) & jump_timing==0 & sled_final_pos==sled_end(f) & ~reject,:));
            ref_vel_sd(s,d,f,1:size(ref_pos_x,2)) = nanstd(ref_vel_x(subject==s & sled_move_time==sled(d) & jump_timing==0 & sled_final_pos==sled_end(f) & ~reject,:));
        end
    end
end
%subtract control trials from experimental trials with same sled direction and speed
for s = 1:n_subj
    for d = 1:length(sled)
        for f = 1:length(sled_end)
            ix = subject==s & sled_move_time==sled(d) & sled_final_pos==sled_end(f) & ~reject;
            pos_diff(ix,:) = ref_pos_x(ix,:) - repmat(squeeze(ref_pos_m(s,d,f,:))',size(ref_pos_x(ix,:),1),1);
            vel_diff(ix,:) = ref_vel_x(ix,:) - repmat(squeeze(ref_vel_m(s,d,f,:))',size(ref_vel_x(ix,:),1),1);
        end
    end
end

%% GAINS
%% interpolation method
% on condition averages
t1 = .25;
t2 = .75;
t_ax = 1:size(ref_pos_x,2);

reps_s = NaN(n_subj,length(sled),length(jumps)-1,length(sled_end),length(target_pos));
reps_s_2 = NaN(n_subj,length(sled),length(jumps)-1,length(sled_end),length(target_pos),2);
pos_diff_m = NaN(n_subj,length(sled),length(jumps)-1,length(sled_end),length(target_pos),size(ref_pos_x,2));
vel_diff_m = NaN(n_subj,length(sled),length(jumps)-1,length(sled_end),length(target_pos),size(ref_pos_x,2));
vel_diff_m_2 = NaN(n_subj,length(sled),length(jumps)-1,length(sled_end),length(target_pos),2,size(ref_pos_x,2));
fit_points = NaN(n_subj,length(sled),length(jumps)-1,length(sled_end),length(target_pos),2);
fit_points_2 = NaN(n_subj,length(sled),length(jumps)-1,length(sled_end),length(target_pos),2,2);
gain = NaN(n_subj,length(sled),length(jumps)-1,length(sled_end),length(target_pos));
latency = NaN(n_subj,length(sled),length(jumps)-1,length(sled_end),length(target_pos));
fit_points_s = NaN(trials,2);
gain_s = NaN(trials,1);
latency_s = NaN(trials,1);
vel_diff_alt = NaN(n_subj,length(sled),length(jumps)-1,length(sled_end),size(ref_pos_x,2));
fit_points_alt = NaN(n_subj,length(sled),length(jumps)-1,length(sled_end),2);
gain_alt = NaN(n_subj,length(sled),length(jumps)-1,length(sled_end));
latency_alt = NaN(n_subj,length(sled),length(jumps)-1,length(sled_end));
gain_2 = NaN(n_subj,length(sled),length(jumps)-1,length(sled_end),length(target_pos),2);
latency_2 = NaN(n_subj,length(sled),length(jumps)-1,length(sled_end),length(target_pos),2);

for s = 1:n_subj
    for d = 1:length(sled)
        for j = 1:length(jumps)-1
            for f = 1:length(sled_end)
                for t = 1:length(target_pos)
                    ix = subject==s & sled_move_time==sled(d) & jump_timing==jumps(j+1) & sled_final_pos==sled_end(f) & target_final_pos_x==target_pos(t) & ~reject;
                    reps_s(s,d,j,f,t) = sum(ix);
                    pos_diff_m(s,d,j,f,t,:) = nanmean(pos_diff(ix,:));
                    vel_diff_m(s,d,j,f,t,:) = nanmean(vel_diff(ix,:));
                    
                    %fit on averages
                    [vel_diff_min, min_i] = min(vel_diff_m(s,d,j,f,t,1:150));
                    [vel_diff_max, max_i] = max(vel_diff_m(s,d,j,f,t,1:150));
                    if target_pos(t)<0
                        fit_points(s,d,j,f,t,1) = find(vel_diff_m(s,d,j,f,t,1:min_i)>=t1*vel_diff_min,1,'last');
                        fit_points(s,d,j,f,t,2) = find(vel_diff_m(s,d,j,f,t,1:min_i)>=t2*vel_diff_min,1,'last');
                    elseif target_pos(t)>0
                        fit_points(s,d,j,f,t,1) = find(vel_diff_m(s,d,j,f,t,1:max_i)<=t1*vel_diff_max,1,'last');
                        fit_points(s,d,j,f,t,2) = find(vel_diff_m(s,d,j,f,t,1:max_i)<=t2*vel_diff_max,1,'last');
                    end
                    
                    P = polyfit([fit_points(s,d,j,f,t,1) fit_points(s,d,j,f,t,2)],[vel_diff_m(s,d,j,f,t,fit_points(s,d,j,f,t,1)) vel_diff_m(s,d,j,f,t,fit_points(s,d,j,f,t,2))],1); %linear fit through interception points
                    gain(s,d,j,f,t) = P(1); %slope
                    fit = polyval(P,0:300);
                    if gain(s,d,j,f,t) > 0
                        latency(s,d,j,f,t) = (find(fit > 0,1,'first') * (1000/sf)) - jumps(j+1)*1000; %zero crossing in ms
                    elseif gain(s,d,j,f,t) < 0
                        latency(s,d,j,f,t) = (find(fit < 0,1,'first') * (1000/sf)) - jumps(j+1)*1000;
                    end
                    
%                     figure(s)
%                     plot(1:200,squeeze(vel_diff_m(s,d,j,f,t,1:200)),'k',fit_points(s,d,j,f,t,1),vel_diff_m(s,d,j,f,t,fit_points(s,d,j,f,t,1)),'*m',fit_points(s,d,j,f,t,2),vel_diff_m(s,d,j,f,t,fit_points(s,d,j,f,t,2)),'*c',59:199,fit(60:200),'k');axis([0 200 -300 300]);hold on
%                     figure(s+n_subj)
%                     subplot(6,4,t+(f-1)*2+(j-1)*4+(d-1)*12),plot(1:200,vel_diff(ix,1:200),'m',1:200,squeeze(vel_diff_m(s,d,j,f,t,1:200)),'k');
%                     pause

                    %fit on single trials
                    [vel_diff_min, min_i] = min(vel_diff(ix==1,:),[],2);
                    [vel_diff_max, max_i] = max(vel_diff(ix==1,:),[],2);
                    index = find(ix==1);
                    for x = 1:length(index)
                        if target_pos(t)<0
                            fit_points_s(index(x),1) = find(vel_diff(index(x),1:min_i(x))>=t1*vel_diff_min(x),1,'last');
                            fit_points_s(index(x),2) = find(vel_diff(index(x),1:min_i(x))>=t2*vel_diff_min(x),1,'last');
                        elseif target_pos(t)>0
                            fit_points_s(index(x),1) = find(vel_diff(index(x),1:max_i(x))<=t1*vel_diff_max(x),1,'last');
                            fit_points_s(index(x),2) = find(vel_diff(index(x),1:max_i(x))<=t2*vel_diff_max(x),1,'last');
                        end
                        
                        P = polyfit([fit_points_s(index(x),1) fit_points_s(index(x),2)],[vel_diff(index(x),fit_points_s(index(x),1)) vel_diff(index(x),fit_points_s(index(x),2))],1); %linear fit through interception points
                        gain_s(index(x)) = P(1); %slope
                        fit = polyval(P,0:300);
                        if gain_s(index(x)) > 0
                            latency_s(index(x)) = (find(fit > 0,1,'first') * (1000/sf)) - jumps(j+1)*1000; %zero crossing in ms
                        elseif gain_s(index(x)) < 0
                            latency_s(index(x)) = (find(fit < 0,1,'first') * (1000/sf)) - jumps(j+1)*1000;
                        end
                        
                        %                         figure(s)
                        %                         subplot(6,4,t+(f-1)*2+(j-1)*4+(d-1)*12),plot(1:200,vel_diff(index(x),1:200),'k',fit_points_s(index(x),1),vel_diff(index(x),fit_points_s(index(x),1)),'*m',fit_points_s(index(x),2),vel_diff(index(x),fit_points_s(index(x),2)),'*c',59:199,fit(60:200),'k');axis([0 200 -300 300]);hold on
                    end
                    %                     pause
                    
                    %fit on first and second half of trials separately
                    ix_first = subject==s & sled_move_time==sled(d) & jump_timing==jumps(j+1) & sled_final_pos==sled_end(f) & target_final_pos_x==target_pos(t) & trial<trial_split & ~reject;
                    ix_last = subject==s & sled_move_time==sled(d) & jump_timing==jumps(j+1) & sled_final_pos==sled_end(f) & target_final_pos_x==target_pos(t) & trial>=trial_split & ~reject;
                    reps_s_2(s,d,j,f,t,1:2) = [sum(ix_first) sum(ix_last)];
                    
                    vel_diff_m_2(s,d,j,f,t,1,:) = nanmean(vel_diff(ix_first,:));
                    vel_diff_m_2(s,d,j,f,t,2,:) = nanmean(vel_diff(ix_last,:));
                    
                    for p = 1:2
                        [vel_diff_min, min_i] = min(vel_diff_m_2(s,d,j,f,t,p,1:150));
                        [vel_diff_max, max_i] = max(vel_diff_m_2(s,d,j,f,t,p,1:150));
                        if target_pos(t)<0
                            fit_points_2(s,d,j,f,t,p,1) = find(vel_diff_m_2(s,d,j,f,t,p,1:min_i)>=t1*vel_diff_min,1,'last');
                            fit_points_2(s,d,j,f,t,p,2) = find(vel_diff_m_2(s,d,j,f,t,p,1:min_i)>=t2*vel_diff_min,1,'last');
                        elseif target_pos(t)>0
                            fit_points_2(s,d,j,f,t,p,1) = find(vel_diff_m_2(s,d,j,f,t,p,1:max_i)<=t1*vel_diff_max,1,'last');
                            fit_points_2(s,d,j,f,t,p,2) = find(vel_diff_m_2(s,d,j,f,t,p,1:max_i)<=t2*vel_diff_max,1,'last');
                        end
                        
                        P = polyfit([fit_points_2(s,d,j,f,t,p,1) fit_points_2(s,d,j,f,t,p,2)],[vel_diff_m_2(s,d,j,f,t,p,fit_points_2(s,d,j,f,t,p,1)) vel_diff_m_2(s,d,j,f,t,p,fit_points_2(s,d,j,f,t,p,2))],1); %linear fit through interception points
                        gain_2(s,d,j,f,t,p) = P(1); %slope
                        fit = polyval(P,0:300);
                        if gain_2(s,d,j,f,t,p) > 0
                            latency_2(s,d,j,f,t,p) = (find(fit > 0,1,'first') * (1000/sf)) - jumps(j+1)*1000; %zero crossing in ms
                        elseif gain_2(s,d,j,f,t,p) < 0
                            latency_2(s,d,j,f,t,p) = (find(fit < 0,1,'first') * (1000/sf)) - jumps(j+1)*1000;
                        end
                    end
                end
                
                %fit on difference between jumps to left and right
                ix1 = subject==s & sled_move_time==sled(d) & jump_timing==jumps(j+1) & sled_final_pos==sled_end(f) & target_final_pos_x==target_pos(1) & ~reject;
                ix2 = subject==s & sled_move_time==sled(d) & jump_timing==jumps(j+1) & sled_final_pos==sled_end(f) & target_final_pos_x==target_pos(2) & ~reject;
                vel_diff_alt(s,d,j,f,:) = nanmean(vel_diff(ix2,:))-nanmean(vel_diff(ix1,:));

                [vel_diff_alt_min, min_i] = min(vel_diff_alt(s,d,j,f,1:150));
                [vel_diff_alt_max, max_i] = max(vel_diff_alt(s,d,j,f,1:150));
                fit_points_alt(s,d,j,f,1) = find(vel_diff_alt(s,d,j,f,1:max_i)<=t1*vel_diff_alt_max,1,'last');
                fit_points_alt(s,d,j,f,2) = find(vel_diff_alt(s,d,j,f,1:max_i)<=t2*vel_diff_alt_max,1,'last');

                P = polyfit([fit_points_alt(s,d,j,f,1) fit_points_alt(s,d,j,f,2)],[vel_diff_alt(s,d,j,f,fit_points_alt(s,d,j,f,1)) vel_diff_alt(s,d,j,f,fit_points_alt(s,d,j,f,2))],1); %linear fit through interception points
                gain_alt(s,d,j,f) = P(1); %slope
                fit = polyval(P,0:300);
                latency_alt(s,d,j,f) = (find(fit > 0,1,'first') * (1000/sf)) - jumps(j+1)*1000; %zero crossing in ms
                
%                 figure(s)
%                 plot(1:200,squeeze(vel_diff_alt(s,d,j,f,1:200)),'k',fit_points_alt(s,d,j,f,1),vel_diff_alt(s,d,j,f,fit_points_alt(s,d,j,f,1)),'*m',fit_points_alt(s,d,j,f,2),vel_diff_alt(s,d,j,f,fit_points_alt(s,d,j,f,2)),'*c',59:199,fit(60:200),'k');axis([0 200 -300 300]);hold on
%                 pause

            end
        end
    end
end
%convert gain to m/sec
gain = gain*sf/1000;
gain_s = gain_s*sf/1000;
gain_alt = gain_alt*sf/1000;
gain_2 = gain_2*sf/1000;

%% means of all variables
MT_m = NaN(n_subj,length(sled),length(jumps),length(sled_end),length(target_pos3));
RT_m = NaN(n_subj,length(sled),length(jumps),length(sled_end),length(target_pos3));
TT_m = NaN(n_subj,length(sled),length(jumps),length(sled_end),length(target_pos3));
MT_m_nojump = NaN(n_subj,length(sled),length(sled_end));
RT_m_nojump = NaN(n_subj,length(sled),length(sled_end));
MT_m_jump = NaN(n_subj,length(sled),length(jumps)-1,length(sled_end),length(target_pos));
RT_m_jump = NaN(n_subj,length(sled),length(jumps)-1,length(sled_end),length(target_pos));
TT_m_jump = NaN(n_subj,length(sled),length(jumps)-1,length(sled_end),length(target_pos));
jump_pos_m = NaN(n_subj,length(sled),length(jumps),length(sled_end),length(target_pos3),2);
jump_rel_me_m = NaN(n_subj,length(sled),length(jumps),length(sled_end),length(target_pos3));
jump_pos_all_y_m = NaN(n_subj,length(sled),length(jumps),length(sled_end),length(target_pos3),3);
gain_m = NaN(n_subj,length(sled),length(jumps)-1,length(sled_end),length(target_pos));
latency_m = NaN(n_subj,length(sled),length(jumps)-1,length(sled_end),length(target_pos));
ref_pos_x_m_nojump = NaN(length(sled_end)*length(sled)*n_subj,size(ref_pos_x,2));
ref_pos_y_m_nojump = NaN(length(sled_end)*length(sled)*n_subj,size(ref_pos_x,2));
ref_vel_x_m_nojump = NaN(length(sled_end)*length(sled)*n_subj,size(ref_pos_x,2));
ref_vel_y_m_nojump = NaN(length(sled_end)*length(sled)*n_subj,size(ref_pos_x,2));
x_pos_subj_m = NaN(length(sled_end)*length(sled)*n_subj,size(ref_pos_x,2));
y_pos_subj_m = NaN(length(sled_end)*length(sled)*n_subj,size(ref_pos_x,2));
index_m_nojump = NaN(length(sled_end)*length(sled)*n_subj,3);
ref_pos_x_m_jump = NaN(length(target_pos)*length(sled_end)*(length(jumps)-1)*length(sled)*n_subj,size(ref_pos_x,2));
ref_pos_y_m_jump = NaN(length(target_pos)*length(sled_end)*(length(jumps)-1)*length(sled)*n_subj,size(ref_pos_x,2));
ref_vel_x_m_jump = NaN(length(target_pos)*length(sled_end)*(length(jumps)-1)*length(sled)*n_subj,size(ref_pos_x,2));
ref_vel_x_m_jump_abs = NaN(length(target_pos)*length(sled_end)*(length(jumps)-1)*length(sled)*n_subj,size(ref_pos_x,2));
ref_vel_y_m_jump_abs = NaN(length(target_pos)*length(sled_end)*(length(jumps)-1)*length(sled)*n_subj,size(ref_pos_x,2));
index_m_jump = NaN(length(target_pos)*length(sled_end)*(length(jumps)-1)*length(sled)*n_subj,5);
ref_pos_x_means = NaN(length(sled)*length(sled_end)+length(target_pos)*length(sled_end)*(length(jumps)-1)*length(sled),size(ref_pos_x,2));
ref_pos_y_means = NaN(length(sled)*length(sled_end)+length(target_pos)*length(sled_end)*(length(jumps)-1)*length(sled),size(ref_pos_x,2));
for s = 1:n_subj
    for d = 1:length(sled)
        for j = 1:length(jumps)
            for f = 1:length(sled_end)
                for t = 1:length(target_pos3)
                    ix = subject==s & sled_move_time==sled(d) & jump_timing==jumps(j) & sled_final_pos==sled_end(f) & target_final_pos_x==target_pos3(t) & ~reject;
                    MT_m(s,d,j,f,t) = nanmean(MT(ix));
                    RT_m(s,d,j,f,t) = nanmean(RT(ix));
                    TT_m(s,d,j,f,t) = nanmean(TT(ix));
                end
            end
        end
    end
end
%no jump trials
for s = 1:n_subj
    for d = 1:length(sled)
        for f = 1:length(sled_end)
            ix = subject==s & sled_move_time==sled(d) & jump_timing==0 & sled_final_pos==sled_end(f) & target_final_pos_x==0 & ~reject;
            MT_m_nojump(s,d,f) = nanmean(MT(ix));
            RT_m_nojump(s,d,f) = nanmean(RT(ix));
            ref_pos_x_m_nojump(f+(d-1)*2+(s-1)*4,:) = nanmean(ref_pos_x(ix,:));
            ref_pos_y_m_nojump(f+(d-1)*2+(s-1)*4,:) = nanmean(ref_pos_y(ix,:));
            ref_vel_x_m_nojump(f+(d-1)*2+(s-1)*4,:) = nanmean(ref_vel_x(ix,:));
            ref_vel_y_m_nojump(f+(d-1)*2+(s-1)*4,:) = nanmean(ref_vel_y(ix,:));
            x_pos_subj_m(s+(f-1)*n_subj+(d-1)*26,:) = nanmean(ref_pos_x(ix,:));
            y_pos_subj_m(s+(f-1)*n_subj+(d-1)*26,:) = nanmean(ref_pos_y(ix,:));
            index_m_nojump(f+(d-1)*2+(s-1)*4,:) = [f d s];
        end
    end
end
%jump trials
for s = 1:n_subj
    for d = 1:length(sled)
        for j = 1:length(jumps)-1
            for f = 1:length(sled_end)
                for t = 1:length(target_pos)
                    ix = subject==s & sled_move_time==sled(d) & jump_timing==jumps(j+1) & sled_final_pos==sled_end(f) & target_final_pos_x==target_pos(t) & ~reject;
                    MT_m_jump(s,d,j,f,t) = nanmean(MT(ix));
                    RT_m_jump(s,d,j,f,t) = nanmean(RT(ix));
                    TT_m_jump(s,d,j,f,t) = nanmean(TT(ix));
                    gain_m(s,d,j,f,t) = nanmean(gain_s(ix));
                    latency_m(s,d,j,f,t) = nanmean(latency_s(ix));
                    
                    jump_pos_m(s,d,j,f,t,1) = nanmean(jump_pos(ix,1));
                    jump_pos_m(s,d,j,f,t,2) = nanmean(jump_pos(ix,2));
                    jump_rel_me_m(s,d,j,f,t) = nanmean(jump_rel_me(ix));
                    jump_pos_all_y_m(s,d,j,f,t,1) = nanmean(ref_pos_y(ix,round(131/4))); %100 ms jump average occurance
                    jump_pos_all_y_m(s,d,j,f,t,2) = nanmean(ref_pos_y(ix,round(231/4))); %200 ms jump average occurance
                    jump_pos_all_y_m(s,d,j,f,t,3) = nanmean(ref_pos_y(ix,round(381/4))); %350 ms jump average occurance
                    
                    ref_pos_x_m_jump(t+(f-1)*2+(j-1)*4+(d-1)*12+(s-1)*24,:) = nanmean(ref_pos_x(ix,:));
                    ref_pos_y_m_jump(t+(f-1)*2+(j-1)*4+(d-1)*12+(s-1)*24,:) = nanmean(ref_pos_y(ix,:));
                    ref_vel_x_m_jump(t+(f-1)*2+(j-1)*4+(d-1)*12+(s-1)*24,:) = nanmean(vel_diff(ix,:));
                    ref_vel_x_m_jump_abs(t+(f-1)*2+(j-1)*4+(d-1)*12+(s-1)*24,:) = nanmean(ref_vel_x(ix,:));
                    ref_vel_y_m_jump_abs(t+(f-1)*2+(j-1)*4+(d-1)*12+(s-1)*24,:) = nanmean(ref_vel_y(ix,:));
                    x_pos_subj_m((4*n_subj)+s+(t-1)*n_subj+(f-1)*26+(j-1)*52+(d-1)*156,:) = nanmean(ref_pos_x(ix,:));
                    y_pos_subj_m((4*n_subj)+s+(t-1)*n_subj+(f-1)*26+(j-1)*52+(d-1)*156,:) = nanmean(ref_pos_y(ix,:));
                    index_m_jump(t+(f-1)*2+(j-1)*4+(d-1)*12+(s-1)*24,:) = [t f j d s];
                end
            end
        end
    end
end
%average over participants
for r = 1:28 %number of conditions (4 control [2 speed * 2 sled direction], 24 exp [2 speed * 2 sled direction * 3 jump * 2 jump direction])
    ref_pos_x_means(r,:) = nanmean(x_pos_subj_m((1:13)+(r-1)*13,:));
    ref_pos_y_means(r,:) = nanmean(y_pos_subj_m((1:13)+(r-1)*13,:));
end
%labelling of variables: jump direction/sled direction/jump condition/sled speed
labels = {'-/forward/nojump/fast','-/backward/nojump/fast','-/forward/nojump/slow','-/backward/nojump/slow','left/forward/j100/fast','right/forward/j100/fast','left/backward/j100/fast','right/backward/j100/fast','left/forward/j200/fast','right/forward/j200/fast','left/backward/j200/fast','right/backward/j200/fast','left/forward/j350/fast','right/forward/j350/fast','left/backward/j350/fast','right/backward/j350/fast','left/forward/j100/slow','right/forward/j100/slow','left/backward/j100/slow','right/backward/j100/slow','left/forward/j200/slow','right/forward/j200/slow','left/backward/j200/slow','right/backward/j200/slow','left/forward/j350/slow','right/forward/j350/slow','left/backward/j350/slow','right/backward/j350/slow'};

%% correlation between sled acceleration at moment of target jump and gain
A_sled_acc = repmat([repmat(acc_jump_exp1(1,1),13,1);repmat(acc_jump_exp1(2,1),13,1);repmat(acc_jump_exp1(3,1),13,1);repmat(acc_jump_exp1(1,2),13,1);repmat(acc_jump_exp1(2,2),13,1);repmat(acc_jump_exp1(3,2),13,1)],4,1);
A_sled_acc200 = repmat([repmat(acc_jump200_exp1(1,1),13,1);repmat(acc_jump200_exp1(2,1),13,1);repmat(acc_jump200_exp1(3,1),13,1);repmat(acc_jump200_exp1(1,2),13,1);repmat(acc_jump200_exp1(2,2),13,1);repmat(acc_jump200_exp1(3,2),13,1)],4,1);
B_gain = [abs(gain(:,1,1,1,1));abs(gain(:,1,2,1,1));abs(gain(:,1,3,1,1));abs(gain(:,2,1,1,1));abs(gain(:,2,2,1,1));abs(gain(:,2,3,1,1))
gain(:,1,1,1,2);gain(:,1,2,1,2);gain(:,1,3,1,2);gain(:,2,1,1,2);gain(:,2,2,1,2);gain(:,2,3,1,2)
abs(gain(:,1,1,2,1));abs(gain(:,1,2,2,1));abs(gain(:,1,3,2,1));abs(gain(:,2,1,2,1));abs(gain(:,2,2,2,1));abs(gain(:,2,3,2,1))
gain(:,1,1,2,2);gain(:,1,2,2,2);gain(:,1,3,2,2);gain(:,2,1,2,2);gain(:,2,2,2,2);gain(:,2,3,2,2)];
[corr,p_val,RL,RU] = corrcoef(A_sled_acc,B_gain);
[corr200,p_val200,RL200,RU200] = corrcoef(A_sled_acc200,B_gain);
regress = polyfit(A_sled_acc,B_gain,1);
regress200 = polyfit(A_sled_acc200,B_gain,1);
figure;plot(A_sled_acc,B_gain,'*')

%% correlation between time-to-target at moment of target jump and gain
time_to_target = NaN(n_subj,length(sled),length(jumps)-1,length(sled_end),length(target_pos));
for j = 1:3
    time_to_target(:,:,j,:,:) = RT_m_jump(:,:,j,:,:) + MT_m_jump(:,:,j,:,:) - (jumps(j+1)*1000);
end
A_time_to_target = [abs(time_to_target(:,1,1,1,1));abs(time_to_target(:,1,2,1,1));abs(time_to_target(:,1,3,1,1));abs(time_to_target(:,2,1,1,1));abs(time_to_target(:,2,2,1,1));abs(time_to_target(:,2,3,1,1))
time_to_target(:,1,1,1,2);time_to_target(:,1,2,1,2);time_to_target(:,1,3,1,2);time_to_target(:,2,1,1,2);time_to_target(:,2,2,1,2);time_to_target(:,2,3,1,2)
abs(time_to_target(:,1,1,2,1));abs(time_to_target(:,1,2,2,1));abs(time_to_target(:,1,3,2,1));abs(time_to_target(:,2,1,2,1));abs(time_to_target(:,2,2,2,1));abs(time_to_target(:,2,3,2,1))
time_to_target(:,1,1,2,2);time_to_target(:,1,2,2,2);time_to_target(:,1,3,2,2);time_to_target(:,2,1,2,2);time_to_target(:,2,2,2,2);time_to_target(:,2,3,2,2)];
[corr_ttt,p_val_ttt,RL_ttt,RU_ttt] = corrcoef(A_time_to_target,B_gain);
regress_ttt = polyfit(A_time_to_target,B_gain,1);
figure;plot(A_time_to_target,B_gain,'*')

%% plots

%% plots for poster
C_nj = [0 0 0; 0.4 0.4 0.4]; 
C_j = [42 60 126; 50 122 177; 67 172 225; 165 32 36; 231 51 0; 245 120 20]/255; %jump colours
tax = 1:4:950;
xax = 1:238;
ref_pos_x_plot_nj = (ref_pos_x_m_nojump-219)/10;
ref_pos_y_plot_nj = (ref_pos_y_m_nojump-440)/10;
ref_pos_x_plot_j = (ref_pos_x_m_jump-219)/10;
ref_pos_y_plot_j = (ref_pos_y_m_jump-440)/10;
ref_vel_x_m_jump = ref_vel_x_m_jump/10;
ref_vel_x_plot_nj = ref_vel_x_m_nojump/10;
ref_vel_y_plot_nj = ref_vel_y_m_nojump/10;
ref_vel_x_m_jump_abs = ref_vel_x_m_jump_abs/10;
ref_vel_y_m_jump_abs = ref_vel_y_m_jump_abs/10;

figure
bw = 0.8;
plot_set = [0,0,2.5,300;repmat([0,0,4.5,300],3,1);0,0,2.5,650;repmat([0,0,4.5,650],3,1);NaN(1,4);repmat([0,0,4.5,4.5],3,1);];
for k = 1:8
    subplot(3,4,k),rectangle('Position',plot_set(k,:),'EdgeColor',[.8 .8 .8],'FaceColor',[.8 .8 .8]);hold on;
end
for k = 10:12
    subplot(3,4,k),rectangle('Position',[0,0,4.5,300],'EdgeColor',[.8 .8 .8],'FaceColor',[.8 .8 .8]);hold on;
end
for d = 1:length(sled)
    for f = 1:length(sled_end)
        subplot(341),bar(d+(f-1)*2,mean(RT_m(:,d,1,f,2)),bw/2,'FaceColor',C_nj(d,:));errorbar(d+(f-1)*2,mean(RT_m(:,d,1,f,2)),std(RT_m(:,d,1,f,2))/sqrt(n_subj-1));
        subplot(345),bar(d+(f-1)*2,mean(MT_m(:,d,1,f,2)),bw/2,'FaceColor',C_nj(d,:));errorbar(d+(f-1)*2,mean(MT_m(:,d,1,f,2)),std(MT_m(:,d,1,f,2))/sqrt(n_subj-1));
    end
end
for d = 1:length(sled)
    for f = 1:length(sled_end)
        for j = 2:length(jumps)
            subplot(3,4,j),bar([1 2]+(d-1)*2+(f-1)*4,squeeze(mean(RT_m(:,d,j,f,[1 3]))),bw,'FaceColor',C_j((j-1)+(d-1)*3,:));errorbar([1 2]+(d-1)*2+(f-1)*4,squeeze(mean(RT_m(:,d,j,f,[1 3]))),squeeze(std(RT_m(:,d,j,f,[1 3])))/sqrt(n_subj-1));ylabel('response time (ms)');set(gca,'XTick',1:1:8);set(gca,'XTickLabel',{'L','R','L','R','L','R','L','R','L','R'});axis([0.5 8.5 0 300]);set(gca,'FontSize',18)
            subplot(3,4,j+4),bar([1 2]+(d-1)*2+(f-1)*4,squeeze(mean(MT_m(:,d,j,f,[1 3]))),bw,'FaceColor',C_j((j-1)+(d-1)*3,:));errorbar([1 2]+(d-1)*2+(f-1)*4,squeeze(mean(MT_m(:,d,j,f,[1 3]))),squeeze(std(MT_m(:,d,j,f,[1 3])))/sqrt(n_subj-1));ylabel('movement time (ms)');set(gca,'XTick',1:1:8);set(gca,'XTickLabel',{'L','R','L','R','L','R','L','R','L','R'});axis([0.5 8.5 0 650]);set(gca,'FontSize',18)
        end
        for j = 1:length(jumps)-1
            subplot(3,4,j+9),bar([1 2]+(d-1)*2+(f-1)*4,squeeze(mean(abs(gain(:,d,j,f,:)))),bw,'FaceColor',C_j(j+(d-1)*3,:));errorbar([1 2]+(d-1)*2+(f-1)*4,squeeze(mean(abs(gain(:,d,j,f,:)))),squeeze(std(abs(gain(:,d,j,f,:))))/sqrt(n_subj-1));ylabel('abs gain (m/s^2)');set(gca,'XTick',1:1:8);set(gca,'XTickLabel',{'L','R','L','R','L','R','L','R','L','R'});axis([0.5 8.5 0 4.5]);set(gca,'FontSize',18)
        end
    end
end
subplot(341);title('no jump');ylabel('response time (ms)');set(gca,'XTick',1.5:2:3.5);set(gca,'XTickLabel',{'forward','backward'});set(gca,'FontSize',18);axis([0.5 4.5 0 300])
subplot(342);title('100 ms jump');subplot(343);title('200 ms jump');subplot(344);title('350 ms jump');set(gca,'FontSize',18)
subplot(345);ylabel('movement time (ms)');set(gca,'XTick',1.5:2:3.5);set(gca,'XTickLabel',{'forward','backward'});axis([0.5 4.5 0 650]);set(gca,'FontSize',18)
set(gcf,'Position',[10 10 950 500])

% %figure to compare forward - backward
% figure
% ix = index_m_nojump(:,1)==1 & index_m_nojump(:,2)==1;
% subplot(141),plot(nanmean(ref_pos_x_plot_nj(ix,xax)),nanmean(ref_pos_y_plot_nj(ix,xax)),'Color',C_nj(1,:),'LineWidth',2);hold on;axis equal
% ix = index_m_nojump(:,1)==1 & index_m_nojump(:,2)==2;
% plot(nanmean(ref_pos_x_plot_nj(ix,xax)),nanmean(ref_pos_y_plot_nj(ix,xax)),'Color',C_nj(2,:),'LineWidth',2)
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==1 & index_m_jump(:,2)==1 & index_m_jump(:,1)==1;
% plot(nanmean(ref_pos_x_plot_j(ix,xax)),nanmean(ref_pos_y_plot_j(ix,xax)),'Color',C_j(1,:),'LineWidth',2)
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==1 & index_m_jump(:,2)==1 & index_m_jump(:,1)==1;
% plot(nanmean(ref_pos_x_plot_j(ix,xax)),nanmean(ref_pos_y_plot_j(ix,xax)),'Color',C_j(4,:),'LineWidth',2)
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==1 & index_m_jump(:,2)==1 & index_m_jump(:,1)==2;
% plot(nanmean(ref_pos_x_plot_j(ix,xax)),nanmean(ref_pos_y_plot_j(ix,xax)),'Color',C_j(1,:),'LineWidth',2)
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==1 & index_m_jump(:,2)==1 & index_m_jump(:,1)==2;
% plot(nanmean(ref_pos_x_plot_j(ix,xax)),nanmean(ref_pos_y_plot_j(ix,xax)),'Color',C_j(4,:),'LineWidth',2)
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==2 & index_m_jump(:,2)==1 & index_m_jump(:,1)==1;
% plot(nanmean(ref_pos_x_plot_j(ix,xax)),nanmean(ref_pos_y_plot_j(ix,xax)),'Color',C_j(2,:),'LineWidth',2)
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==2 & index_m_jump(:,2)==1 & index_m_jump(:,1)==1;
% plot(nanmean(ref_pos_x_plot_j(ix,xax)),nanmean(ref_pos_y_plot_j(ix,xax)),'Color',C_j(5,:),'LineWidth',2)
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==2 & index_m_jump(:,2)==1 & index_m_jump(:,1)==2;
% plot(nanmean(ref_pos_x_plot_j(ix,xax)),nanmean(ref_pos_y_plot_j(ix,xax)),'Color',C_j(2,:),'LineWidth',2)
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==2 & index_m_jump(:,2)==1 & index_m_jump(:,1)==2;
% plot(nanmean(ref_pos_x_plot_j(ix,xax)),nanmean(ref_pos_y_plot_j(ix,xax)),'Color',C_j(5,:),'LineWidth',2)
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==3 & index_m_jump(:,2)==1 & index_m_jump(:,1)==1;
% plot(nanmean(ref_pos_x_plot_j(ix,xax)),nanmean(ref_pos_y_plot_j(ix,xax)),'Color',C_j(3,:),'LineWidth',2)
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==3 & index_m_jump(:,2)==1 & index_m_jump(:,1)==1;
% plot(nanmean(ref_pos_x_plot_j(ix,xax)),nanmean(ref_pos_y_plot_j(ix,xax)),'Color',C_j(6,:),'LineWidth',2)
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==3 & index_m_jump(:,2)==1 & index_m_jump(:,1)==2;
% plot(nanmean(ref_pos_x_plot_j(ix,xax)),nanmean(ref_pos_y_plot_j(ix,xax)),'Color',C_j(3,:),'LineWidth',2)
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==3 & index_m_jump(:,2)==1 & index_m_jump(:,1)==2;
% plot(nanmean(ref_pos_x_plot_j(ix,xax)),nanmean(ref_pos_y_plot_j(ix,xax)),'Color',C_j(6,:),'LineWidth',2)
% ix = index_m_nojump(:,1)==2 & index_m_nojump(:,2)==1;
% plot(nanmean(ref_pos_x_plot_nj(ix,xax)),nanmean(ref_pos_y_plot_nj(ix,xax)),'--','Color',C_nj(1,:),'LineWidth',2)
% ix = index_m_nojump(:,1)==2 & index_m_nojump(:,2)==2;
% plot(nanmean(ref_pos_x_plot_nj(ix,xax)),nanmean(ref_pos_y_plot_nj(ix,xax)),'--','Color',C_nj(2,:),'LineWidth',2)
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==1 & index_m_jump(:,2)==2 & index_m_jump(:,1)==1;
% plot(nanmean(ref_pos_x_plot_j(ix,xax)),nanmean(ref_pos_y_plot_j(ix,xax)),'--','Color',C_j(1,:),'LineWidth',2)
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==1 & index_m_jump(:,2)==2 & index_m_jump(:,1)==1;
% plot(nanmean(ref_pos_x_plot_j(ix,xax)),nanmean(ref_pos_y_plot_j(ix,xax)),'--','Color',C_j(4,:),'LineWidth',2)
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==1 & index_m_jump(:,2)==2 & index_m_jump(:,1)==2;
% plot(nanmean(ref_pos_x_plot_j(ix,xax)),nanmean(ref_pos_y_plot_j(ix,xax)),'--','Color',C_j(1,:),'LineWidth',2)
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==1 & index_m_jump(:,2)==2 & index_m_jump(:,1)==2;
% plot(nanmean(ref_pos_x_plot_j(ix,xax)),nanmean(ref_pos_y_plot_j(ix,xax)),'--','Color',C_j(4,:),'LineWidth',2)
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==2 & index_m_jump(:,2)==2 & index_m_jump(:,1)==1;
% plot(nanmean(ref_pos_x_plot_j(ix,xax)),nanmean(ref_pos_y_plot_j(ix,xax)),'--','Color',C_j(2,:),'LineWidth',2)
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==2 & index_m_jump(:,2)==2 & index_m_jump(:,1)==1;
% plot(nanmean(ref_pos_x_plot_j(ix,xax)),nanmean(ref_pos_y_plot_j(ix,xax)),'--','Color',C_j(5,:),'LineWidth',2)
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==2 & index_m_jump(:,2)==2 & index_m_jump(:,1)==2;
% plot(nanmean(ref_pos_x_plot_j(ix,xax)),nanmean(ref_pos_y_plot_j(ix,xax)),'--','Color',C_j(2,:),'LineWidth',2)
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==2 & index_m_jump(:,2)==2 & index_m_jump(:,1)==2;
% plot(nanmean(ref_pos_x_plot_j(ix,xax)),nanmean(ref_pos_y_plot_j(ix,xax)),'--','Color',C_j(5,:),'LineWidth',2)
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==3 & index_m_jump(:,2)==2 & index_m_jump(:,1)==1;
% plot(nanmean(ref_pos_x_plot_j(ix,xax)),nanmean(ref_pos_y_plot_j(ix,xax)),'--','Color',C_j(3,:),'LineWidth',2)
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==3 & index_m_jump(:,2)==2 & index_m_jump(:,1)==1;
% plot(nanmean(ref_pos_x_plot_j(ix,xax)),nanmean(ref_pos_y_plot_j(ix,xax)),'--','Color',C_j(6,:),'LineWidth',2)
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==3 & index_m_jump(:,2)==2 & index_m_jump(:,1)==2;
% plot(nanmean(ref_pos_x_plot_j(ix,xax)),nanmean(ref_pos_y_plot_j(ix,xax)),'--','Color',C_j(3,:),'LineWidth',2)
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==3 & index_m_jump(:,2)==2 & index_m_jump(:,1)==2;
% plot(nanmean(ref_pos_x_plot_j(ix,xax)),nanmean(ref_pos_y_plot_j(ix,xax)),'--','Color',C_j(6,:),'LineWidth',2)
% xlabel('x position (cm)');ylabel('y position (cm)');set(gca,'FontSize',20)
% 
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==1 & index_m_jump(:,2)==1  & index_m_jump(:,1)==1;
% subplot(1,4,2:4),plot(tax,nanmean(ref_pos_x_plot_j(ix,xax)),'Color',C_j(1,:),'LineWidth',2);hold on
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==1 & index_m_jump(:,2)==1  & index_m_jump(:,1)==2;
% plot(tax,nanmean(ref_pos_x_plot_j(ix,xax)),'Color',C_j(1,:),'LineWidth',2);
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==2 & index_m_jump(:,2)==1  & index_m_jump(:,1)==1;
% plot(tax,nanmean(ref_pos_x_plot_j(ix,xax)),'Color',C_j(2,:),'LineWidth',2);
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==2 & index_m_jump(:,2)==1  & index_m_jump(:,1)==2;
% plot(tax,nanmean(ref_pos_x_plot_j(ix,xax)),'Color',C_j(2,:),'LineWidth',2);
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==3 & index_m_jump(:,2)==1  & index_m_jump(:,1)==1;
% plot(tax,nanmean(ref_pos_x_plot_j(ix,xax)),'Color',C_j(3,:),'LineWidth',2);
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==3 & index_m_jump(:,2)==1  & index_m_jump(:,1)==2;
% plot(tax,nanmean(ref_pos_x_plot_j(ix,xax)),'Color',C_j(3,:),'LineWidth',2);
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==1 & index_m_jump(:,2)==1  & index_m_jump(:,1)==1;
% plot(tax,nanmean(ref_pos_x_plot_j(ix,xax)),'Color',C_j(4,:),'LineWidth',2);
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==1 & index_m_jump(:,2)==1  & index_m_jump(:,1)==2;
% plot(tax,nanmean(ref_pos_x_plot_j(ix,xax)),'Color',C_j(4,:),'LineWidth',2);
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==2 & index_m_jump(:,2)==1  & index_m_jump(:,1)==1;
% plot(tax,nanmean(ref_pos_x_plot_j(ix,xax)),'Color',C_j(5,:),'LineWidth',2);
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==2 & index_m_jump(:,2)==1  & index_m_jump(:,1)==2;
% plot(tax,nanmean(ref_pos_x_plot_j(ix,xax)),'Color',C_j(5,:),'LineWidth',2);
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==3 & index_m_jump(:,2)==1  & index_m_jump(:,1)==1;
% plot(tax,nanmean(ref_pos_x_plot_j(ix,xax)),'Color',C_j(6,:),'LineWidth',2);
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==3 & index_m_jump(:,2)==1  & index_m_jump(:,1)==2;
% plot(tax,nanmean(ref_pos_x_plot_j(ix,xax)),'Color',C_j(6,:),'LineWidth',2);
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==1 & index_m_jump(:,2)==2  & index_m_jump(:,1)==1;
% plot(tax,nanmean(ref_pos_x_plot_j(ix,xax)),'--','Color',C_j(1,:),'LineWidth',2);
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==1 & index_m_jump(:,2)==2  & index_m_jump(:,1)==2;
% plot(tax,nanmean(ref_pos_x_plot_j(ix,xax)),'--','Color',C_j(1,:),'LineWidth',2);
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==2 & index_m_jump(:,2)==2  & index_m_jump(:,1)==1;
% plot(tax,nanmean(ref_pos_x_plot_j(ix,xax)),'--','Color',C_j(2,:),'LineWidth',2);
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==2 & index_m_jump(:,2)==2  & index_m_jump(:,1)==2;
% plot(tax,nanmean(ref_pos_x_plot_j(ix,xax)),'--','Color',C_j(2,:),'LineWidth',2);
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==3 & index_m_jump(:,2)==2  & index_m_jump(:,1)==1;
% plot(tax,nanmean(ref_pos_x_plot_j(ix,xax)),'--','Color',C_j(3,:),'LineWidth',2);
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==3 & index_m_jump(:,2)==2  & index_m_jump(:,1)==2;
% plot(tax,nanmean(ref_pos_x_plot_j(ix,xax)),'--','Color',C_j(3,:),'LineWidth',2);
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==1 & index_m_jump(:,2)==2  & index_m_jump(:,1)==1;
% plot(tax,nanmean(ref_pos_x_plot_j(ix,xax)),'--','Color',C_j(4,:),'LineWidth',2);
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==1 & index_m_jump(:,2)==2  & index_m_jump(:,1)==2;
% plot(tax,nanmean(ref_pos_x_plot_j(ix,xax)),'--','Color',C_j(4,:),'LineWidth',2);
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==2 & index_m_jump(:,2)==2  & index_m_jump(:,1)==1;
% plot(tax,nanmean(ref_pos_x_plot_j(ix,xax)),'--','Color',C_j(5,:),'LineWidth',2);
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==2 & index_m_jump(:,2)==2  & index_m_jump(:,1)==2;
% plot(tax,nanmean(ref_pos_x_plot_j(ix,xax)),'--','Color',C_j(5,:),'LineWidth',2);
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==3 & index_m_jump(:,2)==2  & index_m_jump(:,1)==1;
% plot(tax,nanmean(ref_pos_x_plot_j(ix,xax)),'--','Color',C_j(6,:),'LineWidth',2);
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==3 & index_m_jump(:,2)==2  & index_m_jump(:,1)==2;
% plot(tax,nanmean(ref_pos_x_plot_j(ix,xax)),'--','Color',C_j(6,:),'LineWidth',2);
% % axis([0 950 -25 25]);
% xlabel('time (ms)');ylabel('x position (cm)');
% 
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==1 & index_m_jump(:,2)==1  & index_m_jump(:,1)==1;
% subplot(1,4,6:8),plot(tax,nanmean(ref_pos_y_plot_j(ix,xax)),'Color',C_j(1,:),'LineWidth',2);hold on
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==1 & index_m_jump(:,2)==1  & index_m_jump(:,1)==2;
% plot(tax,nanmean(ref_pos_y_plot_j(ix,xax)),'Color',C_j(1,:),'LineWidth',2);
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==2 & index_m_jump(:,2)==1  & index_m_jump(:,1)==1;
% plot(tax,nanmean(ref_pos_y_plot_j(ix,xax)),'Color',C_j(2,:),'LineWidth',2);
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==2 & index_m_jump(:,2)==1  & index_m_jump(:,1)==2;
% plot(tax,nanmean(ref_pos_y_plot_j(ix,xax)),'Color',C_j(2,:),'LineWidth',2);
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==3 & index_m_jump(:,2)==1  & index_m_jump(:,1)==1;
% plot(tax,nanmean(ref_pos_y_plot_j(ix,xax)),'Color',C_j(3,:),'LineWidth',2);
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==3 & index_m_jump(:,2)==1  & index_m_jump(:,1)==2;
% plot(tax,nanmean(ref_pos_y_plot_j(ix,xax)),'Color',C_j(3,:),'LineWidth',2);
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==1 & index_m_jump(:,2)==1  & index_m_jump(:,1)==1;
% plot(tax,nanmean(ref_pos_y_plot_j(ix,xax)),'Color',C_j(4,:),'LineWidth',2);
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==1 & index_m_jump(:,2)==1  & index_m_jump(:,1)==2;
% plot(tax,nanmean(ref_pos_y_plot_j(ix,xax)),'Color',C_j(4,:),'LineWidth',2);
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==2 & index_m_jump(:,2)==1  & index_m_jump(:,1)==1;
% plot(tax,nanmean(ref_pos_y_plot_j(ix,xax)),'Color',C_j(5,:),'LineWidth',2);
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==2 & index_m_jump(:,2)==1  & index_m_jump(:,1)==2;
% plot(tax,nanmean(ref_pos_y_plot_j(ix,xax)),'Color',C_j(5,:),'LineWidth',2);
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==3 & index_m_jump(:,2)==1  & index_m_jump(:,1)==1;
% plot(tax,nanmean(ref_pos_y_plot_j(ix,xax)),'Color',C_j(6,:),'LineWidth',2);
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==3 & index_m_jump(:,2)==1  & index_m_jump(:,1)==2;
% plot(tax,nanmean(ref_pos_y_plot_j(ix,xax)),'Color',C_j(6,:),'LineWidth',2);
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==1 & index_m_jump(:,2)==2  & index_m_jump(:,1)==1;
% plot(tax,nanmean(ref_pos_y_plot_j(ix,xax)),'--','Color',C_j(1,:),'LineWidth',2);
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==1 & index_m_jump(:,2)==2  & index_m_jump(:,1)==2;
% plot(tax,nanmean(ref_pos_y_plot_j(ix,xax)),'--','Color',C_j(1,:),'LineWidth',2);
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==2 & index_m_jump(:,2)==2  & index_m_jump(:,1)==1;
% plot(tax,nanmean(ref_pos_y_plot_j(ix,xax)),'--','Color',C_j(2,:),'LineWidth',2);
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==2 & index_m_jump(:,2)==2  & index_m_jump(:,1)==2;
% plot(tax,nanmean(ref_pos_y_plot_j(ix,xax)),'--','Color',C_j(2,:),'LineWidth',2);
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==3 & index_m_jump(:,2)==2  & index_m_jump(:,1)==1;
% plot(tax,nanmean(ref_pos_y_plot_j(ix,xax)),'--','Color',C_j(3,:),'LineWidth',2);
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==3 & index_m_jump(:,2)==2  & index_m_jump(:,1)==2;
% plot(tax,nanmean(ref_pos_y_plot_j(ix,xax)),'--','Color',C_j(3,:),'LineWidth',2);
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==1 & index_m_jump(:,2)==2  & index_m_jump(:,1)==1;
% plot(tax,nanmean(ref_pos_y_plot_j(ix,xax)),'--','Color',C_j(4,:),'LineWidth',2);
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==1 & index_m_jump(:,2)==2  & index_m_jump(:,1)==2;
% plot(tax,nanmean(ref_pos_y_plot_j(ix,xax)),'--','Color',C_j(4,:),'LineWidth',2);
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==2 & index_m_jump(:,2)==2  & index_m_jump(:,1)==1;
% plot(tax,nanmean(ref_pos_y_plot_j(ix,xax)),'--','Color',C_j(5,:),'LineWidth',2);
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==2 & index_m_jump(:,2)==2  & index_m_jump(:,1)==2;
% plot(tax,nanmean(ref_pos_y_plot_j(ix,xax)),'--','Color',C_j(5,:),'LineWidth',2);
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==3 & index_m_jump(:,2)==2  & index_m_jump(:,1)==1;
% plot(tax,nanmean(ref_pos_y_plot_j(ix,xax)),'--','Color',C_j(6,:),'LineWidth',2);
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==3 & index_m_jump(:,2)==2  & index_m_jump(:,1)==2;
% plot(tax,nanmean(ref_pos_y_plot_j(ix,xax)),'--','Color',C_j(6,:),'LineWidth',2);
% % axis([0 950 -25 25]);
% xlabel('time (ms)');ylabel('y position (cm)');
% set(gca,'FontSize',20);set(gcf,'Position',[10 10 1300 400])

%no jump trials for OFC
figure
ix = index_m_nojump(:,1)==1 & index_m_nojump(:,2)==1;
subplot(221),plot(tax,nanmean(ref_pos_x_plot_nj(ix,xax)),'Color',C_nj(1,:),'LineWidth',2);hold on
subplot(222),plot(tax,nanmean(ref_pos_y_plot_nj(ix,xax)),'Color',C_nj(1,:),'LineWidth',2);hold on
subplot(223),plot(tax,nanmean(ref_vel_x_plot_nj(ix,xax)),'Color',C_nj(1,:),'LineWidth',2);hold on
subplot(224),plot(tax,nanmean(ref_vel_y_plot_nj(ix,xax)),'Color',C_nj(1,:),'LineWidth',2);hold on
ix = index_m_nojump(:,1)==1 & index_m_nojump(:,2)==2;
subplot(221),plot(tax,nanmean(ref_pos_x_plot_nj(ix,xax)),'Color',C_nj(2,:),'LineWidth',2)
subplot(222),plot(tax,nanmean(ref_pos_y_plot_nj(ix,xax)),'Color',C_nj(2,:),'LineWidth',2)
subplot(223),plot(tax,nanmean(ref_vel_x_plot_nj(ix,xax)),'Color',C_nj(2,:),'LineWidth',2)
subplot(224),plot(tax,nanmean(ref_vel_y_plot_nj(ix,xax)),'Color',C_nj(2,:),'LineWidth',2)
ix = index_m_nojump(:,1)==2 & index_m_nojump(:,2)==1;
subplot(221),plot(tax,nanmean(ref_pos_x_plot_nj(ix,xax)),'--','Color',C_nj(1,:),'LineWidth',2)
subplot(222),plot(tax,nanmean(ref_pos_y_plot_nj(ix,xax)),'--','Color',C_nj(1,:),'LineWidth',2)
subplot(223),plot(tax,nanmean(ref_vel_x_plot_nj(ix,xax)),'--','Color',C_nj(1,:),'LineWidth',2)
subplot(224),plot(tax,nanmean(ref_vel_y_plot_nj(ix,xax)),'--','Color',C_nj(1,:),'LineWidth',2)
ix = index_m_nojump(:,1)==2 & index_m_nojump(:,2)==2;
subplot(221),plot(tax,nanmean(ref_pos_x_plot_nj(ix,xax)),'--','Color',C_nj(2,:),'LineWidth',2)
subplot(222),plot(tax,nanmean(ref_pos_y_plot_nj(ix,xax)),'--','Color',C_nj(2,:),'LineWidth',2)
subplot(223),plot(tax,nanmean(ref_vel_x_plot_nj(ix,xax)),'--','Color',C_nj(2,:),'LineWidth',2)
subplot(224),plot(tax,nanmean(ref_vel_y_plot_nj(ix,xax)),'--','Color',C_nj(2,:),'LineWidth',2)
subplot(221);xlabel('time(ms)');ylabel('x position (cm)');legend('fast/forward','slow/forward','fast/back','slow/back')
subplot(222);xlabel('time(ms)');ylabel('y position (cm)');
subplot(223);xlabel('time(ms)');ylabel('x velocity (cm/s)');
subplot(224);xlabel('time(ms)');ylabel('y velocity (cm/s)');

%plots for paper
%new figure x y data
figure
ix = index_m_nojump(:,1)==1 & index_m_nojump(:,2)==1;
% subplot(3,4,[1 5 9]),plot(nanmean(ref_pos_x_plot_nj(ix,xax)),nanmean(ref_pos_y_plot_nj(ix,xax)),'Color',C_nj(1,:),'LineWidth',1);hold on;axis equal
subplot(141),plot(nanmean(ref_pos_x_plot_nj(ix,xax)),nanmean(ref_pos_y_plot_nj(ix,xax)),'Color',C_nj(1,:),'LineWidth',1);hold on;axis equal
ix = index_m_nojump(:,1)==1 & index_m_nojump(:,2)==2;
plot(nanmean(ref_pos_x_plot_nj(ix,xax)),nanmean(ref_pos_y_plot_nj(ix,xax)),'Color',C_nj(2,:),'LineWidth',1)
ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==1 & index_m_jump(:,2)==1 & index_m_jump(:,1)==1;
plot(nanmean(ref_pos_x_plot_j(ix,xax)),nanmean(ref_pos_y_plot_j(ix,xax)),'Color',C_j(1,:),'LineWidth',1)
ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==1 & index_m_jump(:,2)==1 & index_m_jump(:,1)==1;
plot(nanmean(ref_pos_x_plot_j(ix,xax)),nanmean(ref_pos_y_plot_j(ix,xax)),'Color',C_j(4,:),'LineWidth',1)
ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==1 & index_m_jump(:,2)==1 & index_m_jump(:,1)==2;
plot(nanmean(ref_pos_x_plot_j(ix,xax)),nanmean(ref_pos_y_plot_j(ix,xax)),'Color',C_j(1,:),'LineWidth',1)
ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==1 & index_m_jump(:,2)==1 & index_m_jump(:,1)==2;
plot(nanmean(ref_pos_x_plot_j(ix,xax)),nanmean(ref_pos_y_plot_j(ix,xax)),'Color',C_j(4,:),'LineWidth',1)
ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==2 & index_m_jump(:,2)==1 & index_m_jump(:,1)==1;
plot(nanmean(ref_pos_x_plot_j(ix,xax)),nanmean(ref_pos_y_plot_j(ix,xax)),'Color',C_j(2,:),'LineWidth',1)
ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==2 & index_m_jump(:,2)==1 & index_m_jump(:,1)==1;
plot(nanmean(ref_pos_x_plot_j(ix,xax)),nanmean(ref_pos_y_plot_j(ix,xax)),'Color',C_j(5,:),'LineWidth',1)
ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==2 & index_m_jump(:,2)==1 & index_m_jump(:,1)==2;
plot(nanmean(ref_pos_x_plot_j(ix,xax)),nanmean(ref_pos_y_plot_j(ix,xax)),'Color',C_j(2,:),'LineWidth',1)
ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==2 & index_m_jump(:,2)==1 & index_m_jump(:,1)==2;
plot(nanmean(ref_pos_x_plot_j(ix,xax)),nanmean(ref_pos_y_plot_j(ix,xax)),'Color',C_j(5,:),'LineWidth',1)
ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==3 & index_m_jump(:,2)==1 & index_m_jump(:,1)==1;
plot(nanmean(ref_pos_x_plot_j(ix,xax)),nanmean(ref_pos_y_plot_j(ix,xax)),'Color',C_j(3,:),'LineWidth',1)
ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==3 & index_m_jump(:,2)==1 & index_m_jump(:,1)==1;
plot(nanmean(ref_pos_x_plot_j(ix,xax)),nanmean(ref_pos_y_plot_j(ix,xax)),'Color',C_j(6,:),'LineWidth',1)
ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==3 & index_m_jump(:,2)==1 & index_m_jump(:,1)==2;
plot(nanmean(ref_pos_x_plot_j(ix,xax)),nanmean(ref_pos_y_plot_j(ix,xax)),'Color',C_j(3,:),'LineWidth',1)
ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==3 & index_m_jump(:,2)==1 & index_m_jump(:,1)==2;
plot(nanmean(ref_pos_x_plot_j(ix,xax)),nanmean(ref_pos_y_plot_j(ix,xax)),'Color',C_j(6,:),'LineWidth',1)
ix = index_m_nojump(:,1)==2 & index_m_nojump(:,2)==1;
plot(nanmean(ref_pos_x_plot_nj(ix,xax)),nanmean(ref_pos_y_plot_nj(ix,xax)),'--','Color',C_nj(1,:),'LineWidth',1)
ix = index_m_nojump(:,1)==2 & index_m_nojump(:,2)==2;
plot(nanmean(ref_pos_x_plot_nj(ix,xax)),nanmean(ref_pos_y_plot_nj(ix,xax)),'--','Color',C_nj(2,:),'LineWidth',1)
ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==1 & index_m_jump(:,2)==2 & index_m_jump(:,1)==1;
plot(nanmean(ref_pos_x_plot_j(ix,xax)),nanmean(ref_pos_y_plot_j(ix,xax)),'--','Color',C_j(1,:),'LineWidth',1)
ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==1 & index_m_jump(:,2)==2 & index_m_jump(:,1)==1;
plot(nanmean(ref_pos_x_plot_j(ix,xax)),nanmean(ref_pos_y_plot_j(ix,xax)),'--','Color',C_j(4,:),'LineWidth',1)
ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==1 & index_m_jump(:,2)==2 & index_m_jump(:,1)==2;
plot(nanmean(ref_pos_x_plot_j(ix,xax)),nanmean(ref_pos_y_plot_j(ix,xax)),'--','Color',C_j(1,:),'LineWidth',1)
ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==1 & index_m_jump(:,2)==2 & index_m_jump(:,1)==2;
plot(nanmean(ref_pos_x_plot_j(ix,xax)),nanmean(ref_pos_y_plot_j(ix,xax)),'--','Color',C_j(4,:),'LineWidth',1)
ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==2 & index_m_jump(:,2)==2 & index_m_jump(:,1)==1;
plot(nanmean(ref_pos_x_plot_j(ix,xax)),nanmean(ref_pos_y_plot_j(ix,xax)),'--','Color',C_j(2,:),'LineWidth',1)
ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==2 & index_m_jump(:,2)==2 & index_m_jump(:,1)==1;
plot(nanmean(ref_pos_x_plot_j(ix,xax)),nanmean(ref_pos_y_plot_j(ix,xax)),'--','Color',C_j(5,:),'LineWidth',1)
ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==2 & index_m_jump(:,2)==2 & index_m_jump(:,1)==2;
plot(nanmean(ref_pos_x_plot_j(ix,xax)),nanmean(ref_pos_y_plot_j(ix,xax)),'--','Color',C_j(2,:),'LineWidth',1)
ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==2 & index_m_jump(:,2)==2 & index_m_jump(:,1)==2;
plot(nanmean(ref_pos_x_plot_j(ix,xax)),nanmean(ref_pos_y_plot_j(ix,xax)),'--','Color',C_j(5,:),'LineWidth',1)
ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==3 & index_m_jump(:,2)==2 & index_m_jump(:,1)==1;
plot(nanmean(ref_pos_x_plot_j(ix,xax)),nanmean(ref_pos_y_plot_j(ix,xax)),'--','Color',C_j(3,:),'LineWidth',1)
ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==3 & index_m_jump(:,2)==2 & index_m_jump(:,1)==1;
plot(nanmean(ref_pos_x_plot_j(ix,xax)),nanmean(ref_pos_y_plot_j(ix,xax)),'--','Color',C_j(6,:),'LineWidth',1)
ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==3 & index_m_jump(:,2)==2 & index_m_jump(:,1)==2;
plot(nanmean(ref_pos_x_plot_j(ix,xax)),nanmean(ref_pos_y_plot_j(ix,xax)),'--','Color',C_j(3,:),'LineWidth',1)
ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==3 & index_m_jump(:,2)==2 & index_m_jump(:,1)==2;
plot(nanmean(ref_pos_x_plot_j(ix,xax)),nanmean(ref_pos_y_plot_j(ix,xax)),'--','Color',C_j(6,:),'LineWidth',1)
xlabel('x position (cm)');ylabel('y position (cm)');set(gca,'FontSize',8)

% ix = index_m_nojump(:,1)==1 & index_m_nojump(:,2)==1;
% subplot(3,4,2:4),plot(tax,nanmean(ref_vel_x_m_nojump(ix,xax)),'Color',C_nj(1,:),'LineWidth',1);hold on
% ix = index_m_nojump(:,1)==1 & index_m_nojump(:,2)==2;
% plot(tax,nanmean(ref_vel_x_m_nojump(ix,xax)),'Color',C_nj(2,:),'LineWidth',1)
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==1 & index_m_jump(:,2)==1  & index_m_jump(:,1)==1;
% plot(tax,nanmean(ref_vel_x_m_jump_abs(ix,xax)),'Color',C_j(1,:),'LineWidth',1);hold on
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==1 & index_m_jump(:,2)==1  & index_m_jump(:,1)==2;
% plot(tax,nanmean(ref_vel_x_m_jump_abs(ix,xax)),'Color',C_j(1,:),'LineWidth',1);
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==2 & index_m_jump(:,2)==1  & index_m_jump(:,1)==1;
% plot(tax,nanmean(ref_vel_x_m_jump_abs(ix,xax)),'Color',C_j(2,:),'LineWidth',1);
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==2 & index_m_jump(:,2)==1  & index_m_jump(:,1)==2;
% plot(tax,nanmean(ref_vel_x_m_jump_abs(ix,xax)),'Color',C_j(2,:),'LineWidth',1);
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==3 & index_m_jump(:,2)==1  & index_m_jump(:,1)==1;
% plot(tax,nanmean(ref_vel_x_m_jump_abs(ix,xax)),'Color',C_j(3,:),'LineWidth',1);
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==3 & index_m_jump(:,2)==1  & index_m_jump(:,1)==2;
% plot(tax,nanmean(ref_vel_x_m_jump_abs(ix,xax)),'Color',C_j(3,:),'LineWidth',1);
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==1 & index_m_jump(:,2)==1  & index_m_jump(:,1)==1;
% plot(tax,nanmean(ref_vel_x_m_jump_abs(ix,xax)),'Color',C_j(4,:),'LineWidth',1);
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==1 & index_m_jump(:,2)==1  & index_m_jump(:,1)==2;
% plot(tax,nanmean(ref_vel_x_m_jump_abs(ix,xax)),'Color',C_j(4,:),'LineWidth',1);
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==2 & index_m_jump(:,2)==1  & index_m_jump(:,1)==1;
% plot(tax,nanmean(ref_vel_x_m_jump_abs(ix,xax)),'Color',C_j(5,:),'LineWidth',1);
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==2 & index_m_jump(:,2)==1  & index_m_jump(:,1)==2;
% plot(tax,nanmean(ref_vel_x_m_jump_abs(ix,xax)),'Color',C_j(5,:),'LineWidth',1);
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==3 & index_m_jump(:,2)==1  & index_m_jump(:,1)==1;
% plot(tax,nanmean(ref_vel_x_m_jump_abs(ix,xax)),'Color',C_j(6,:),'LineWidth',1);
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==3 & index_m_jump(:,2)==1  & index_m_jump(:,1)==2;
% plot(tax,nanmean(ref_vel_x_m_jump_abs(ix,xax)),'Color',C_j(6,:),'LineWidth',1);
% ix = index_m_nojump(:,1)==2 & index_m_nojump(:,2)==1;
% plot(tax,nanmean(ref_vel_x_m_nojump(ix,xax)),'--','Color',C_nj(1,:),'LineWidth',1)
% ix = index_m_nojump(:,1)==2 & index_m_nojump(:,2)==2;
% plot(tax,nanmean(ref_vel_x_m_nojump(ix,xax)),'--','Color',C_nj(2,:),'LineWidth',1)
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==1 & index_m_jump(:,2)==2  & index_m_jump(:,1)==1;
% plot(tax,nanmean(ref_vel_x_m_jump_abs(ix,xax)),'--','Color',C_j(1,:),'LineWidth',1);
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==1 & index_m_jump(:,2)==2  & index_m_jump(:,1)==2;
% plot(tax,nanmean(ref_vel_x_m_jump_abs(ix,xax)),'--','Color',C_j(1,:),'LineWidth',1);
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==2 & index_m_jump(:,2)==2  & index_m_jump(:,1)==1;
% plot(tax,nanmean(ref_vel_x_m_jump_abs(ix,xax)),'--','Color',C_j(2,:),'LineWidth',1);
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==2 & index_m_jump(:,2)==2  & index_m_jump(:,1)==2;
% plot(tax,nanmean(ref_vel_x_m_jump_abs(ix,xax)),'--','Color',C_j(2,:),'LineWidth',1);
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==3 & index_m_jump(:,2)==2  & index_m_jump(:,1)==1;
% plot(tax,nanmean(ref_vel_x_m_jump_abs(ix,xax)),'--','Color',C_j(3,:),'LineWidth',1);
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==3 & index_m_jump(:,2)==2  & index_m_jump(:,1)==2;
% plot(tax,nanmean(ref_vel_x_m_jump_abs(ix,xax)),'--','Color',C_j(3,:),'LineWidth',1);
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==1 & index_m_jump(:,2)==2  & index_m_jump(:,1)==1;
% plot(tax,nanmean(ref_vel_x_m_jump_abs(ix,xax)),'--','Color',C_j(4,:),'LineWidth',1);
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==1 & index_m_jump(:,2)==2  & index_m_jump(:,1)==2;
% plot(tax,nanmean(ref_vel_x_m_jump_abs(ix,xax)),'--','Color',C_j(4,:),'LineWidth',1);
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==2 & index_m_jump(:,2)==2  & index_m_jump(:,1)==1;
% plot(tax,nanmean(ref_vel_x_m_jump_abs(ix,xax)),'--','Color',C_j(5,:),'LineWidth',1);
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==2 & index_m_jump(:,2)==2  & index_m_jump(:,1)==2;
% plot(tax,nanmean(ref_vel_x_m_jump_abs(ix,xax)),'--','Color',C_j(5,:),'LineWidth',1);
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==3 & index_m_jump(:,2)==2  & index_m_jump(:,1)==1;
% plot(tax,nanmean(ref_vel_x_m_jump_abs(ix,xax)),'--','Color',C_j(6,:),'LineWidth',1);
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==3 & index_m_jump(:,2)==2  & index_m_jump(:,1)==2;
% plot(tax,nanmean(ref_vel_x_m_jump_abs(ix,xax)),'--','Color',C_j(6,:),'LineWidth',1);
% axis([0 950 -26 26]);xlabel('time (ms)');ylabel('x velocity (cm/s)');set(gca,'FontSize',8)
% 
% ix = index_m_nojump(:,1)==1 & index_m_nojump(:,2)==1;
% subplot(3,4,6:8),plot(tax,nanmean(ref_vel_y_m_nojump(ix,xax)),'Color',C_nj(1,:),'LineWidth',1);hold on
% ix = index_m_nojump(:,1)==1 & index_m_nojump(:,2)==2;
% plot(tax,nanmean(ref_vel_y_m_nojump(ix,xax)),'Color',C_nj(2,:),'LineWidth',1)
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==1 & index_m_jump(:,2)==1  & index_m_jump(:,1)==1;
% plot(tax,nanmean(ref_vel_y_m_jump_abs(ix,xax)),'Color',C_j(1,:),'LineWidth',1);hold on
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==1 & index_m_jump(:,2)==1  & index_m_jump(:,1)==2;
% plot(tax,nanmean(ref_vel_y_m_jump_abs(ix,xax)),'Color',C_j(1,:),'LineWidth',1);
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==2 & index_m_jump(:,2)==1  & index_m_jump(:,1)==1;
% plot(tax,nanmean(ref_vel_y_m_jump_abs(ix,xax)),'Color',C_j(2,:),'LineWidth',1);
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==2 & index_m_jump(:,2)==1  & index_m_jump(:,1)==2;
% plot(tax,nanmean(ref_vel_y_m_jump_abs(ix,xax)),'Color',C_j(2,:),'LineWidth',1);
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==3 & index_m_jump(:,2)==1  & index_m_jump(:,1)==1;
% plot(tax,nanmean(ref_vel_y_m_jump_abs(ix,xax)),'Color',C_j(3,:),'LineWidth',1);
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==3 & index_m_jump(:,2)==1  & index_m_jump(:,1)==2;
% plot(tax,nanmean(ref_vel_y_m_jump_abs(ix,xax)),'Color',C_j(3,:),'LineWidth',1);
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==1 & index_m_jump(:,2)==1  & index_m_jump(:,1)==1;
% plot(tax,nanmean(ref_vel_y_m_jump_abs(ix,xax)),'Color',C_j(4,:),'LineWidth',1);
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==1 & index_m_jump(:,2)==1  & index_m_jump(:,1)==2;
% plot(tax,nanmean(ref_vel_y_m_jump_abs(ix,xax)),'Color',C_j(4,:),'LineWidth',1);
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==2 & index_m_jump(:,2)==1  & index_m_jump(:,1)==1;
% plot(tax,nanmean(ref_vel_y_m_jump_abs(ix,xax)),'Color',C_j(5,:),'LineWidth',1);
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==2 & index_m_jump(:,2)==1  & index_m_jump(:,1)==2;
% plot(tax,nanmean(ref_vel_y_m_jump_abs(ix,xax)),'Color',C_j(5,:),'LineWidth',1);
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==3 & index_m_jump(:,2)==1  & index_m_jump(:,1)==1;
% plot(tax,nanmean(ref_vel_y_m_jump_abs(ix,xax)),'Color',C_j(6,:),'LineWidth',1);
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==3 & index_m_jump(:,2)==1  & index_m_jump(:,1)==2;
% plot(tax,nanmean(ref_vel_y_m_jump_abs(ix,xax)),'Color',C_j(6,:),'LineWidth',1);
% ix = index_m_nojump(:,1)==2 & index_m_nojump(:,2)==1;
% plot(tax,nanmean(ref_vel_y_m_nojump(ix,xax)),'--','Color',C_nj(1,:),'LineWidth',1);
% ix = index_m_nojump(:,1)==2 & index_m_nojump(:,2)==2;
% plot(tax,nanmean(ref_vel_y_m_nojump(ix,xax)),'--','Color',C_nj(2,:),'LineWidth',1);
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==1 & index_m_jump(:,2)==2  & index_m_jump(:,1)==1;
% plot(tax,nanmean(ref_vel_y_m_jump_abs(ix,xax)),'--','Color',C_j(1,:),'LineWidth',1);
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==1 & index_m_jump(:,2)==2  & index_m_jump(:,1)==2;
% plot(tax,nanmean(ref_vel_y_m_jump_abs(ix,xax)),'--','Color',C_j(1,:),'LineWidth',1);
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==2 & index_m_jump(:,2)==2  & index_m_jump(:,1)==1;
% plot(tax,nanmean(ref_vel_y_m_jump_abs(ix,xax)),'--','Color',C_j(2,:),'LineWidth',1);
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==2 & index_m_jump(:,2)==2  & index_m_jump(:,1)==2;
% plot(tax,nanmean(ref_vel_y_m_jump_abs(ix,xax)),'--','Color',C_j(2,:),'LineWidth',1);
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==3 & index_m_jump(:,2)==2  & index_m_jump(:,1)==1;
% plot(tax,nanmean(ref_vel_y_m_jump_abs(ix,xax)),'--','Color',C_j(3,:),'LineWidth',1);
% ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==3 & index_m_jump(:,2)==2  & index_m_jump(:,1)==2;
% plot(tax,nanmean(ref_vel_y_m_jump_abs(ix,xax)),'--','Color',C_j(3,:),'LineWidth',1);
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==1 & index_m_jump(:,2)==2  & index_m_jump(:,1)==1;
% plot(tax,nanmean(ref_vel_y_m_jump_abs(ix,xax)),'--','Color',C_j(4,:),'LineWidth',1);
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==1 & index_m_jump(:,2)==2  & index_m_jump(:,1)==2;
% plot(tax,nanmean(ref_vel_y_m_jump_abs(ix,xax)),'--','Color',C_j(4,:),'LineWidth',1);
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==2 & index_m_jump(:,2)==2  & index_m_jump(:,1)==1;
% plot(tax,nanmean(ref_vel_y_m_jump_abs(ix,xax)),'--','Color',C_j(5,:),'LineWidth',1);
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==2 & index_m_jump(:,2)==2  & index_m_jump(:,1)==2;
% plot(tax,nanmean(ref_vel_y_m_jump_abs(ix,xax)),'--','Color',C_j(5,:),'LineWidth',1);
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==3 & index_m_jump(:,2)==2  & index_m_jump(:,1)==1;
% plot(tax,nanmean(ref_vel_y_m_jump_abs(ix,xax)),'--','Color',C_j(6,:),'LineWidth',1);
% ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==3 & index_m_jump(:,2)==2  & index_m_jump(:,1)==2;
% plot(tax,nanmean(ref_vel_y_m_jump_abs(ix,xax)),'--','Color',C_j(6,:),'LineWidth',1);
% axis([0 950 -2 90]);xlabel('time (ms)');ylabel('y velocity (cm/s)');set(gca,'FontSize',8)

ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==1 & index_m_jump(:,2)==1  & index_m_jump(:,1)==1;
% subplot(3,4,10:12),plot(tax,nanmean(ref_vel_x_m_jump(ix,xax)),'Color',C_j(1,:),'LineWidth',1);hold on
subplot(1,4,2:4),plot(tax,nanmean(ref_vel_x_m_jump(ix,xax)),'Color',C_j(1,:),'LineWidth',1);hold on
ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==1 & index_m_jump(:,2)==1  & index_m_jump(:,1)==2;
plot(tax,nanmean(ref_vel_x_m_jump(ix,xax)),'Color',C_j(1,:),'LineWidth',1);
ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==2 & index_m_jump(:,2)==1  & index_m_jump(:,1)==1;
plot(tax,nanmean(ref_vel_x_m_jump(ix,xax)),'Color',C_j(2,:),'LineWidth',1);
ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==2 & index_m_jump(:,2)==1  & index_m_jump(:,1)==2;
plot(tax,nanmean(ref_vel_x_m_jump(ix,xax)),'Color',C_j(2,:),'LineWidth',1);
ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==3 & index_m_jump(:,2)==1  & index_m_jump(:,1)==1;
plot(tax,nanmean(ref_vel_x_m_jump(ix,xax)),'Color',C_j(3,:),'LineWidth',1);
ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==3 & index_m_jump(:,2)==1  & index_m_jump(:,1)==2;
plot(tax,nanmean(ref_vel_x_m_jump(ix,xax)),'Color',C_j(3,:),'LineWidth',1);
ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==1 & index_m_jump(:,2)==1  & index_m_jump(:,1)==1;
plot(tax,nanmean(ref_vel_x_m_jump(ix,xax)),'Color',C_j(4,:),'LineWidth',1);
ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==1 & index_m_jump(:,2)==1  & index_m_jump(:,1)==2;
plot(tax,nanmean(ref_vel_x_m_jump(ix,xax)),'Color',C_j(4,:),'LineWidth',1);
ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==2 & index_m_jump(:,2)==1  & index_m_jump(:,1)==1;
plot(tax,nanmean(ref_vel_x_m_jump(ix,xax)),'Color',C_j(5,:),'LineWidth',1);
ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==2 & index_m_jump(:,2)==1  & index_m_jump(:,1)==2;
plot(tax,nanmean(ref_vel_x_m_jump(ix,xax)),'Color',C_j(5,:),'LineWidth',1);
ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==3 & index_m_jump(:,2)==1  & index_m_jump(:,1)==1;
plot(tax,nanmean(ref_vel_x_m_jump(ix,xax)),'Color',C_j(6,:),'LineWidth',1);
ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==3 & index_m_jump(:,2)==1  & index_m_jump(:,1)==2;
plot(tax,nanmean(ref_vel_x_m_jump(ix,xax)),'Color',C_j(6,:),'LineWidth',1);
ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==1 & index_m_jump(:,2)==2  & index_m_jump(:,1)==1;
plot(tax,nanmean(ref_vel_x_m_jump(ix,xax)),'--','Color',C_j(1,:),'LineWidth',1);
ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==1 & index_m_jump(:,2)==2  & index_m_jump(:,1)==2;
plot(tax,nanmean(ref_vel_x_m_jump(ix,xax)),'--','Color',C_j(1,:),'LineWidth',1);
ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==2 & index_m_jump(:,2)==2  & index_m_jump(:,1)==1;
plot(tax,nanmean(ref_vel_x_m_jump(ix,xax)),'--','Color',C_j(2,:),'LineWidth',1);
ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==2 & index_m_jump(:,2)==2  & index_m_jump(:,1)==2;
plot(tax,nanmean(ref_vel_x_m_jump(ix,xax)),'--','Color',C_j(2,:),'LineWidth',1);
ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==3 & index_m_jump(:,2)==2  & index_m_jump(:,1)==1;
plot(tax,nanmean(ref_vel_x_m_jump(ix,xax)),'--','Color',C_j(3,:),'LineWidth',1);
ix = index_m_jump(:,4)==1 & index_m_jump(:,3)==3 & index_m_jump(:,2)==2  & index_m_jump(:,1)==2;
plot(tax,nanmean(ref_vel_x_m_jump(ix,xax)),'--','Color',C_j(3,:),'LineWidth',1);
ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==1 & index_m_jump(:,2)==2  & index_m_jump(:,1)==1;
plot(tax,nanmean(ref_vel_x_m_jump(ix,xax)),'--','Color',C_j(4,:),'LineWidth',1);
ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==1 & index_m_jump(:,2)==2  & index_m_jump(:,1)==2;
plot(tax,nanmean(ref_vel_x_m_jump(ix,xax)),'--','Color',C_j(4,:),'LineWidth',1);
ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==2 & index_m_jump(:,2)==2  & index_m_jump(:,1)==1;
plot(tax,nanmean(ref_vel_x_m_jump(ix,xax)),'--','Color',C_j(5,:),'LineWidth',1);
ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==2 & index_m_jump(:,2)==2  & index_m_jump(:,1)==2;
plot(tax,nanmean(ref_vel_x_m_jump(ix,xax)),'--','Color',C_j(5,:),'LineWidth',1);
ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==3 & index_m_jump(:,2)==2  & index_m_jump(:,1)==1;
plot(tax,nanmean(ref_vel_x_m_jump(ix,xax)),'--','Color',C_j(6,:),'LineWidth',1);
ix = index_m_jump(:,4)==2 & index_m_jump(:,3)==3 & index_m_jump(:,2)==2  & index_m_jump(:,1)==2;
plot(tax,nanmean(ref_vel_x_m_jump(ix,xax)),'--','Color',C_j(6,:),'LineWidth',1);
plot([100 100],[-25 25],'Color',C_j(1,:),'LineWidth',1);plot([200 200],[-25 25],'Color',C_j(2,:),'LineWidth',1);plot([350 350],[-25 25],'Color',C_j(3,:),'LineWidth',1)
axis([0 950 -25 25]);xlabel('time (ms)');ylabel('x velocity difference (cm/s)');
set(gca,'FontSize',8); set(gcf,'Position',[10 10 600 220])

figure
subplot(341),bar(1,mean(mean(mean(mean(abs(gain(:,1,:,:,:)))))),bw,'FaceColor',C_j(2,:));hold on;errorbar(1,mean(mean(mean(mean(abs(gain(:,1,:,:,:)))))),std(mean(mean(mean(abs(gain(:,1,:,:,:)),5),4),3))/sqrt(n_subj-1));ylabel('abs gain (m/s^2)');
bar(2,mean(mean(mean(mean(abs(gain(:,2,:,:,:)))))),bw,'FaceColor',C_j(5,:));errorbar(2,mean(mean(mean(mean(abs(gain(:,2,:,:,:)))))),std(mean(mean(mean(abs(gain(:,2,:,:,:)),5),4),3))/sqrt(n_subj-1));ylabel('abs gain (m/s^2)');set(gca,'XTick',1:1:2);set(gca,'XTickLabel',{'fast','slow'});axis([0.5 2.5 0 4.1]);set(gca,'FontSize',8)
for k = 2:4
    subplot(3,4,k),rectangle('Position',[0,0,4.5,260],'EdgeColor',[.8 .8 .8],'FaceColor',[.8 .8 .8]);hold on;
end
for d = 1:length(sled)
    for f = 1:length(sled_end)
        for j = 1:length(jumps)-1
            subplot(3,4,j+1),bar([1 2]+(d-1)*2+(f-1)*4,squeeze(mean(abs(gain(:,d,j,f,:)))),bw,'FaceColor',C_j(j+(d-1)*3,:));errorbar([1 2]+(d-1)*2+(f-1)*4,squeeze(mean(abs(gain(:,d,j,f,:)))),squeeze(std(abs(gain(:,d,j,f,:))))/sqrt(n_subj-1));ylabel('abs gain (m/s^2)');set(gca,'XTick',1:1:8);set(gca,'XTickLabel',{'L','R','L','R','L','R','L','R','L','R'});axis([0.5 8.5 0 4.1]);set(gca,'FontSize',8)
        end
    end
end
subplot(345),bar(1,mean(mean(mean(mean(RT_m(:,1,2:4,:,[1 3]))))),bw,'FaceColor',C_j(2,:));hold on;errorbar(1,mean(mean(mean(mean(RT_m(:,1,2:4,:,[1 3]))))),std(mean(mean(mean(RT_m(:,1,2:4,:,[1 3]),5),4),3))/sqrt(n_subj-1));
bar(2,mean(mean(mean(mean(RT_m(:,2,2:4,:,[1 3]))))),bw,'FaceColor',C_j(5,:));errorbar(2,mean(mean(mean(mean(RT_m(:,2,2:4,:,[1 3]))))),std(mean(mean(mean(RT_m(:,2,2:4,:,[1 3]),5),4),3))/sqrt(n_subj-1));ylabel('response time (ms)');set(gca,'XTick',1:1:2);set(gca,'XTickLabel',{'fast','slow'});axis([0.5 2.5 0 260]);set(gca,'FontSize',8)
subplot(349),bar(1,mean(mean(mean(mean(MT_m(:,1,2:4,:,[1 3]))))),bw,'FaceColor',C_j(2,:));hold on;errorbar(1,mean(mean(mean(mean(MT_m(:,1,2:4,:,[1 3]))))),std(mean(mean(mean(MT_m(:,1,2:4,:,[1 3]),5),4),3))/sqrt(n_subj-1));
bar(2,mean(mean(mean(mean(MT_m(:,2,2:4,:,[1 3]))))),bw,'FaceColor',C_j(5,:));errorbar(2,mean(mean(mean(mean(MT_m(:,2,2:4,:,[1 3]))))),std(mean(mean(mean(MT_m(:,2,2:4,:,[1 3]),5),4),3))/sqrt(n_subj-1));ylabel('movement time');set(gca,'XTick',1:1:2);set(gca,'XTickLabel',{'fast','slow'});axis([0.5 2.5 0 610]);set(gca,'FontSize',8)
for k = 6:8
    subplot(3,4,k),rectangle('Position',[0,0,4.5,260],'EdgeColor',[.8 .8 .8],'FaceColor',[.8 .8 .8]);hold on;
end
for k = 10:12
    subplot(3,4,k),rectangle('Position',[0,0,4.5,610],'EdgeColor',[.8 .8 .8],'FaceColor',[.8 .8 .8]);hold on;
end
for d = 1:length(sled)
    for f = 1:length(sled_end)
        for j = 2:length(jumps)
            subplot(3,4,j+4),bar([1 2]+(d-1)*2+(f-1)*4,squeeze(mean(RT_m(:,d,j,f,[1 3]))),bw,'FaceColor',C_j((j-1)+(d-1)*3,:));errorbar([1 2]+(d-1)*2+(f-1)*4,squeeze(mean(RT_m(:,d,j,f,[1 3]))),squeeze(std(RT_m(:,d,j,f,[1 3])))/sqrt(n_subj-1));ylabel('response time (ms)');set(gca,'XTick',1:1:8);set(gca,'XTickLabel',{'L','R','L','R','L','R','L','R','L','R'});axis([0.5 8.5 0 260]);set(gca,'FontSize',8)
            subplot(3,4,j+8),bar([1 2]+(d-1)*2+(f-1)*4,squeeze(mean(MT_m(:,d,j,f,[1 3]))),bw,'FaceColor',C_j((j-1)+(d-1)*3,:));errorbar([1 2]+(d-1)*2+(f-1)*4,squeeze(mean(MT_m(:,d,j,f,[1 3]))),squeeze(std(MT_m(:,d,j,f,[1 3])))/sqrt(n_subj-1));ylabel('movement time (ms)');set(gca,'XTick',1:1:8);set(gca,'XTickLabel',{'L','R','L','R','L','R','L','R','L','R'});axis([0.5 8.5 0 610]);set(gca,'FontSize',8)
        end
    end
end
subplot(341);title('averages');set(gca,'FontSize',8);
subplot(342);title('100 ms jump');subplot(343);title('200 ms jump');subplot(344);title('350 ms jump');set(gca,'FontSize',8)
set(gcf,'Position',[10 10 400 400])

%main results lines
C_lines = [50 122 177; 231 51 0;]/255; %colours fast and slow
figure
subplot(351),plot(1:2,mean(mean(mean(abs(gain(:,:,:,:,:)),5),4),3),'k');hold on
subplot(351),plot(1:2,mean(mean(mean(mean(abs(gain(:,:,:,:,:)),5),4),3),1),'s','MarkerFaceColor',C_lines(1,:));errorbar(1:2,mean(mean(mean(mean(abs(gain(:,:,:,:,:)),5),4),3),1),std(mean(mean(mean(abs(gain(:,:,:,:,:)),5),4),3),0,1)/sqrt(n_subj-1));xlabel('sled acceleration');ylabel('abs gain (m/s^2)');set(gca,'XTick',1:1:2);set(gca,'XTickLabel',{'fast','slow'});axis([0.5 2.5 0 4.1]);set(gca,'FontSize',8)
subplot(356),plot(1:2,mean(mean(mean(RT_m(:,:,2:4,:,[1 3]),5),4),3),'k');hold on
subplot(356),plot(1:2,mean(mean(mean(mean(RT_m(:,:,2:4,:,[1 3]),5),4),3),1),'s','MarkerFaceColor',C_lines(1,:));errorbar(1:2,mean(mean(mean(mean(RT_m(:,:,2:4,:,[1 3]),5),4),3),1),std(mean(mean(mean(RT_m(:,:,2:4,:,[1 3]),5),4),3),0,1)/sqrt(n_subj-1));xlabel('sled acceleration');ylabel('response time (ms)');set(gca,'XTick',1:1:2);set(gca,'XTickLabel',{'fast','slow'});axis([0.5 2.5 0 300]);set(gca,'FontSize',8)
subplot(3,5,11),plot(1:2,mean(mean(mean(MT_m(:,:,2:4,:,[1 3]),5),4),3),'k');hold on
subplot(3,5,11),plot(1:2,mean(mean(mean(mean(MT_m(:,:,2:4,:,[1 3]),5),4),3),1),'s','MarkerFaceColor',C_lines(1,:));errorbar(1:2,mean(mean(mean(mean(MT_m(:,:,2:4,:,[1 3]),5),4),3),1),std(mean(mean(mean(MT_m(:,:,2:4,:,[1 3]),5),4),3),0,1)/sqrt(n_subj-1));xlabel('sled acceleration');ylabel('movement time');set(gca,'XTick',1:1:2);set(gca,'XTickLabel',{'fast','slow'});axis([0.5 2.5 0 620]);set(gca,'FontSize',8)
for d = 1:length(sled)
    for f = 1:length(sled_end)
        for j = 1:length(jumps)-1
            for t=1:length(target_pos)
                if f==1
                    subplot(3,5,[2,3]+(t-1)*2),plot(1:3,squeeze(mean(abs(gain(:,d,:,f,t)))),'o','Color',C_lines(d,:),'MarkerFaceColor',C_lines(d,:));hold on; errorbar(1:3,squeeze(mean(abs(gain(:,d,:,f,t)))),squeeze(std(abs(gain(:,d,:,f,t))))/sqrt(n_subj-1));xlabel('jump timing (ms)');set(gca,'XTick',1:1:3);set(gca,'XTickLabel',{'100','200','350'});axis([0.5 3.5 0 4.1]);set(gca,'FontSize',8);
                    if t==1
                        subplot(3,5,[7,8]+(t-1)*2),plot(1:3,squeeze(mean(RT_m(:,d,2:4,f,t))),'o','Color',C_lines(d,:),'MarkerFaceColor',C_lines(d,:));hold on; errorbar(1:3,squeeze(mean(RT_m(:,d,2:4,f,t))),squeeze(std(RT_m(:,d,2:4,f,t)))/sqrt(n_subj-1));xlabel('jump timing (ms)');set(gca,'XTick',1:1:3);set(gca,'XTickLabel',{'100','200','350'});axis([0.5 3.5 0 330]);set(gca,'FontSize',8)
                        subplot(3,5,[12,13]+(t-1)*2),plot(1:3,squeeze(mean(MT_m(:,d,2:4,f,t))),'o','Color',C_lines(d,:),'MarkerFaceColor',C_lines(d,:));hold on; errorbar(1:3,squeeze(mean(MT_m(:,d,2:4,f,t))),squeeze(std(MT_m(:,d,2:4,f,t)))/sqrt(n_subj-1));xlabel('jump timing (ms)');set(gca,'XTick',1:1:3);set(gca,'XTickLabel',{'100','200','350'});axis([0.5 3.5 0 620]);set(gca,'FontSize',8)
                    else
                        subplot(3,5,[7,8]+(t-1)*2),plot(1:3,squeeze(mean(RT_m(:,d,2:4,f,3))),'o','Color',C_lines(d,:),'MarkerFaceColor',C_lines(d,:));hold on;errorbar(1:3,squeeze(mean(RT_m(:,d,2:4,f,3))),squeeze(std(RT_m(:,d,2:4,f,3)))/sqrt(n_subj-1));xlabel('jump timing (ms)');set(gca,'XTick',1:1:3);set(gca,'XTickLabel',{'100','200','350'});axis([0.5 3.5 0 330]);set(gca,'FontSize',8)
                        subplot(3,5,[12,13]+(t-1)*2),plot(1:3,squeeze(mean(MT_m(:,d,2:4,f,3))),'o','Color',C_lines(d,:),'MarkerFaceColor',C_lines(d,:));hold on;errorbar(1:3,squeeze(mean(MT_m(:,d,2:4,f,3))),squeeze(std(MT_m(:,d,2:4,f,3)))/sqrt(n_subj-1));xlabel('jump timing (ms)');set(gca,'XTick',1:1:3);set(gca,'XTickLabel',{'100','200','350'});axis([0.5 3.5 0 620]);set(gca,'FontSize',8)
                    end
                else
                    subplot(3,5,[2,3]+(t-1)*2),plot(1:3,squeeze(mean(abs(gain(:,d,:,f,t)))),'^','Color',C_lines(d,:),'MarkerFaceColor',C_lines(d,:));hold on; errorbar(1:3,squeeze(mean(abs(gain(:,d,:,f,t)))),squeeze(std(abs(gain(:,d,:,f,t))))/sqrt(n_subj-1))
                    if t==1
                        subplot(3,5,[7,8]+(t-1)*2),plot(1:3,squeeze(mean(RT_m(:,d,2:4,f,t))),'^','Color',C_lines(d,:),'MarkerFaceColor',C_lines(d,:));errorbar(1:3,squeeze(mean(RT_m(:,d,2:4,f,t))),squeeze(std(RT_m(:,d,2:4,f,t)))/sqrt(n_subj-1));xlabel('jump timing (ms)');set(gca,'XTick',1:1:3);set(gca,'XTickLabel',{'100','200','350'});axis([0.5 3.5 0 330]);set(gca,'FontSize',8)
                        subplot(3,5,[12,13]+(t-1)*2),plot(1:3,squeeze(mean(MT_m(:,d,2:4,f,t))),'^','Color',C_lines(d,:),'MarkerFaceColor',C_lines(d,:));errorbar(1:3,squeeze(mean(MT_m(:,d,2:4,f,t))),squeeze(std(MT_m(:,d,2:4,f,t)))/sqrt(n_subj-1));xlabel('jump timing (ms)');set(gca,'XTick',1:1:3);set(gca,'XTickLabel',{'100','200','350'});axis([0.5 3.5 0 620]);set(gca,'FontSize',8)
                    else
                        subplot(3,5,[7,8]+(t-1)*2),plot(1:3,squeeze(mean(RT_m(:,d,2:4,f,3))),'^','Color',C_lines(d,:),'MarkerFaceColor',C_lines(d,:));errorbar(1:3,squeeze(mean(RT_m(:,d,2:4,f,3))),squeeze(std(RT_m(:,d,2:4,f,3)))/sqrt(n_subj-1));xlabel('jump timing (ms)');set(gca,'XTick',1:1:3);set(gca,'XTickLabel',{'100','200','350'});axis([0.5 3.5 0 330]);set(gca,'FontSize',8)
                        subplot(3,5,[12,13]+(t-1)*2),plot(1:3,squeeze(mean(MT_m(:,d,2:4,f,3))),'^','Color',C_lines(d,:),'MarkerFaceColor',C_lines(d,:));errorbar(1:3,squeeze(mean(MT_m(:,d,2:4,f,3))),squeeze(std(MT_m(:,d,2:4,f,3)))/sqrt(n_subj-1));xlabel('jump timing (ms)');set(gca,'XTick',1:1:3);set(gca,'XTickLabel',{'100','200','350'});axis([0.5 3.5 0 620]);set(gca,'FontSize',8)
                    end
                end
            end
        end
    end
end
subplot(351);title('averages');set(gca,'FontSize',8);
subplot(3,5,[2,3]);title('left jump');set(gca,'FontSize',8);
subplot(3,5,[4,5]);title('right jump');set(gca,'FontSize',8);
legend('fast forward','fast backward','slow forward','slow backward')
set(gcf,'Position',[10 10 400 500])

%figure early vs late phase
figure
subplot(141),bar([1 2],[mean(mean(mean(mean(abs(gain_2(:,1,:,:,:,1)))))) mean(mean(mean(mean(abs(gain_2(:,1,:,:,:,2))))))],bw,'FaceColor',C_j(2,:));hold on;errorbar([1 2],[mean(mean(mean(mean(abs(gain_2(:,1,:,:,:,1)))))) mean(mean(mean(mean(abs(gain_2(:,1,:,:,:,2))))))],[std(mean(mean(mean(abs(gain_2(:,1,:,:,:,1)),5),4),3))/sqrt(n_subj-1) std(mean(mean(mean(abs(gain_2(:,1,:,:,:,2)),5),4),3))/sqrt(n_subj-1)]);ylabel('abs gain (m/s^2)');
bar([3 4],[mean(mean(mean(mean(abs(gain_2(:,2,:,:,:,1)))))) mean(mean(mean(mean(abs(gain_2(:,2,:,:,:,2))))))],bw,'FaceColor',C_j(5,:));hold on;errorbar([3 4],[mean(mean(mean(mean(abs(gain_2(:,2,:,:,:,1)))))) mean(mean(mean(mean(abs(gain_2(:,2,:,:,:,2))))))],[std(mean(mean(mean(abs(gain_2(:,2,:,:,:,1)),5),4),3))/sqrt(n_subj-1) std(mean(mean(mean(abs(gain_2(:,2,:,:,:,2)),5),4),3))/sqrt(n_subj-1)]);ylabel('abs gain (m/s^2)');
ylabel('abs gain (m/s^2)');set(gca,'XTick',[1:1:4]);set(gca,'XTickLabel',{'early','late','early','late'});axis([0.5 4.5 0 4.5]);set(gca,'FontSize',18)
for d = 1:length(sled)
    for f = 1:length(sled_end)
        for j = 1:length(jumps)-1
            for t=1:length(target_pos)
                subplot(1,4,j+1),rectangle('Position',[0,0,8.5,8.5],'EdgeColor',[.8 .8 .8],'FaceColor',[.8 .8 .8]);hold on;
            end
        end
    end
end
for d = 1:length(sled)
    for f = 1:length(sled_end)
        for j = 1:length(jumps)-1
            for t=1:length(target_pos)
                subplot(1,4,j+1),bar([1 2]+(t-1)*2+(d-1)*4+(f-1)*8,squeeze(nanmean(abs(gain_2(:,d,j,f,t,:)))),bw,'FaceColor',C_j(j+(d-1)*3,:));errorbar([1 2]+(t-1)*2+(d-1)*4+(f-1)*8,squeeze(nanmean(abs(gain_2(:,d,j,f,t,:)))),squeeze(nanstd(abs(gain_2(:,d,j,f,t,:))))/sqrt(n_subj-1));ylabel('abs gain (m/s^2)');set(gca,'XTick',1:1:16);set(gca,'XTickLabel',{'L1','L2','R1','R2','L1','L2','R1','R2','L1','L2','R1','R2','L1','L2','R1','R2','L1','L2','R1','R2'});axis([0.5 16.5 0 4.5]);set(gca,'FontSize',18)
            end
        end
    end
end
subplot(141);title('averages');set(gca,'FontSize',18);
subplot(142);title('100 ms jump');subplot(143);title('200 ms jump');subplot(144);title('350 ms jump');set(gca,'FontSize',18)
set(gcf,'Position',[10 10 950 200])

%figure early vs late phase in lines
figure
for d = 1:length(sled)
    for j = 1:length(jumps)-1
        plot([1 1.5],squeeze(nanmean(nanmean(nanmean(nanmean(abs(gain_2(:,:,:,:,:,1:2)),5),4),3),2)),'k');hold on;
        for p = 1:2
            if p==1
                plot(1,nanmean(nanmean(nanmean(nanmean(nanmean(abs(gain_2(:,:,:,:,:,p)),5),4),3),2),1),'o','Color',C_lines(d,:),'MarkerFaceColor',C_lines(d,:));hold on; errorbar(1,nanmean(nanmean(nanmean(nanmean(nanmean(abs(gain_2(:,:,:,:,:,p)),5),4),3),2),1),std(nanmean(nanmean(nanmean(nanmean(abs(gain_2(:,:,:,:,:,p)),5),4),3),2))/sqrt(n_subj-1));
                plot(2.5:4.5,squeeze(nanmean(nanmean(nanmean(abs(gain_2(:,d,:,:,:,p)),5),4))),'o','Color',C_lines(d,:),'MarkerFaceColor',C_lines(d,:));hold on; errorbar(2.5:4.5,squeeze(nanmean(nanmean(nanmean(abs(gain_2(:,d,:,:,:,p)),5),4))),squeeze(std(nanmean(nanmean(abs(gain_2(:,d,:,:,:,p)),5),4)))/sqrt(n_subj-1));
            elseif p==2
                plot(1.5,nanmean(nanmean(nanmean(nanmean(nanmean(abs(gain_2(:,:,:,:,:,p)),5),4),3),2),1),'^','Color',C_lines(d,:),'MarkerFaceColor',C_lines(d,:));hold on; errorbar(1.5,nanmean(nanmean(nanmean(nanmean(nanmean(abs(gain_2(:,:,:,:,:,p)),5),4),3),2),1),std(nanmean(nanmean(nanmean(nanmean(abs(gain_2(:,:,:,:,:,p)),5),4),3),2))/sqrt(n_subj-1));
                plot(2.5:4.5,squeeze(nanmean(nanmean(nanmean(abs(gain_2(:,d,:,:,:,p)),5),4))),'^','Color',C_lines(d,:),'MarkerFaceColor',C_lines(d,:));hold on; errorbar(2.5:4.5,squeeze(nanmean(nanmean(nanmean(abs(gain_2(:,d,:,:,:,p)),5),4))),squeeze(std(nanmean(nanmean(abs(gain_2(:,d,:,:,:,p)),5),4)))/sqrt(n_subj-1))
            end
        end
    end
end
title('Experiment 1');set(gca,'FontSize',8);
xlabel('jump timing (ms)');ylabel('abs gain (m/s^2)');set(gca,'XTick',[1 1.5 2.5:1:4.5]);set(gca,'XTickLabel',{'Early','Late','100','200','350'});axis([0.5 5 0 4.1]);set(gca,'FontSize',8);
legend('early fast','late fast','early slow','late slow')
set(gcf,'Position',[10 10 200 200])

% %figure OFC predicted visuomotor gains
% load('/Users/leonieoostwoudwijdenes/surfdrive/Writings/Vestibular manual interaction/Figures/gainOFC.mat')
% figure
% for d = 1:length(sled)
%     for t=1:length(target_pos)
%         subplot(1,2,t),plot(1:3,squeeze(gain(d,1,t,:)),'-o','Color',C_lines(d,:),'MarkerFaceColor',C_lines(d,:));hold on;xlabel('jump timing (ms)');set(gca,'XTick',1:1:3);set(gca,'XTickLabel',{'100','200','350'});axis([0.5 3.5 0 4.1]);set(gca,'FontSize',8);
%         subplot(1,2,t),plot(1:3,squeeze(gain(d,2,t,:)),'--^','Color',C_lines(d,:),'MarkerFaceColor',C_lines(d,:));hold on
%     end
% end
% subplot(121);title('left jump');set(gca,'FontSize',8);
% subplot(122);title('right jump');set(gca,'FontSize',8);
% legend('fast forward','fast backward','slow forward','slow backward')
% set(gcf,'Position',[10 10 200 125])

%figure gain over sled acceleration
figure(20)
for d = 1:length(sled)
    for j = 1:length(jumps)-1
        for f = 1:length(sled_end)
            for t=1:length(target_pos)
                if f==1 && t==1
                    subplot(231), plot(repmat(acc_jump_exp1(j,d),n_subj,1),abs(gain(:,d,j,f,t)),'<','Color',C_j(j+(d-1)*3,:));hold on
                    subplot(232), plot(acc_jump200_exp1(j,d),abs(gain(:,d,j,f,t)),'<','Color',C_j(j+(d-1)*3,:));hold on
                    subplot(233), plot(time_to_target(:,d,j,f,t),abs(gain(:,d,j,f,t)),'<','Color',C_j(j+(d-1)*3,:));hold on
                elseif f==1 && t==2
                    subplot(231), plot(repmat(acc_jump_exp1(j,d),n_subj,1),gain(:,d,j,f,t),'>','Color',C_j(j+(d-1)*3,:))
                    subplot(232), plot(acc_jump200_exp1(j,d),gain(:,d,j,f,t),'>','Color',C_j(j+(d-1)*3,:))
                    subplot(233), plot(time_to_target(:,d,j,f,t),gain(:,d,j,f,t),'>','Color',C_j(j+(d-1)*3,:))
                elseif f==2 && t==1
                    subplot(231), plot(repmat(acc_jump_exp1(j,d),n_subj,1),abs(gain(:,d,j,f,t)),'s','Color',C_j(j+(d-1)*3,:))
                    subplot(232), plot(acc_jump200_exp1(j,d),abs(gain(:,d,j,f,t)),'s','Color',C_j(j+(d-1)*3,:))
                    subplot(233), plot(time_to_target(:,d,j,f,t),abs(gain(:,d,j,f,t)),'s','Color',C_j(j+(d-1)*3,:))
                elseif f==2 && t==2
                    subplot(231), plot(repmat(acc_jump_exp1(j,d),n_subj,1),gain(:,d,j,f,t),'d','Color',C_j(j+(d-1)*3,:))
                    subplot(232), plot(acc_jump200_exp1(j,d),gain(:,d,j,f,t),'d','Color',C_j(j+(d-1)*3,:))
                    subplot(233), plot(time_to_target(:,d,j,f,t),gain(:,d,j,f,t),'d','Color',C_j(j+(d-1)*3,:))
                end
            end
        end
    end
end
subplot(231), plot([0,3.5],regress(1)*[0 3.5] + regress(2),'k')
legend('forward left','forward right','back left','back right');ylabel('abs gain (m/s^2)'); xlabel('sled acceleration at moment of target jump (m/s^2)');set(gca,'FontSize',8);axis([.7 3.5 0 7.2]);title('Experiment 1')
subplot(232), plot([-1.5,3],regress200(1)*[-1.5 3] + regress200(2),'k')
ylabel('abs gain (m/s^2)'); xlabel('sled acceleration during initial part correction (m/s^2)');set(gca,'FontSize',8);axis([-1.5 3 0 7.2])
subplot(233) %, plot([0,750],regress_ttt(1)*[0 750] + regress_ttt(2),'k')
ylabel('gain (m/s^2)'); xlabel('time-to-target at moment of target jump (ms)');set(gca,'FontSize',8);axis([0 800 0 7.2])


%% arrange cells for R
%control conditions
c = length(sled)*length(sled_end);
R = reshape(repmat(1:n_subj,c,1),c*n_subj,1); %subject
R(:,2) = repmat(reshape(repmat(1:length(sled),c/length(sled),1),c,1),n_subj,1); %speed
R(:,3) = repmat(1:2,1,length(R(:,1))/2); %sled direction
MT_control1_R = [R reshape(permute(MT_m_nojump,[3 2 1]),[],1)]; 
RT_control1_R = [R reshape(permute(RT_m_nojump,[3 2 1]),[],1)]; 

%experimental conditions
c = length(sled)*(length(jumps)-1)*length(sled_end)*length(target_pos);
R = reshape(repmat(1:n_subj,c,1),c*n_subj,1); %subject
R(:,2) = repmat(reshape(repmat(1:length(sled),c/length(sled),1),c,1),n_subj,1); %speed
R(:,3) = repmat(reshape(repmat(1:(length(jumps)-1),c/(length(sled)*(length(jumps)-1)),1),c/length(sled),1),n_subj*length(sled),1); %jumps
R(:,4) = repmat(reshape(repmat(1:length(sled_end),c/(length(sled)*((length(jumps)-1))*length(sled_end)),1),c/(length(sled)*(length(jumps)-1)),1),n_subj*length(sled)*(length(jumps)-1),1); %sled direction
R(:,5) = repmat(1:2,1,length(R(:,1))/2); %jump direction
MT_exp1_R = [R reshape(permute(MT_m_jump,[5 4 3 2 1]),[],1)];
RT_exp1_R = [R reshape(permute(RT_m_jump,[5 4 3 2 1]),[],1)];
gain_exp1_R = [R reshape(permute(abs(gain),[5 4 3 2 1]),[],1)];
TT_exp1_R = [R reshape(permute(TT_m_jump,[5 4 3 2 1]),[],1)];

%exp without jump direction
c = length(sled)*(length(jumps)-1)*length(sled_end);
R = reshape(repmat(1:n_subj,c,1),c*n_subj,1); %subject
R(:,2) = repmat(reshape(repmat(1:length(sled),c/length(sled),1),c,1),n_subj,1); %speed
R(:,3) = repmat(reshape(repmat(1:(length(jumps)-1),c/(length(sled)*(length(jumps)-1)),1),c/length(sled),1),n_subj*length(sled),1); %jumps
R(:,4) = repmat(reshape(repmat(1:length(sled_end),c/(length(sled)*((length(jumps)-1))*length(sled_end)),1),c/(length(sled)*(length(jumps)-1)),1),n_subj*length(sled)*(length(jumps)-1),1); %sled direction
gain_alt_exp1_R = [R reshape(permute(gain_alt,[4 3 2 1]),[],1)];

%post-hoc distiction for gain between first and second half of trials
c = length(sled)*(length(jumps)-1)*length(sled_end)*length(target_pos)*2;
R = reshape(repmat(1:n_subj,c,1),c*n_subj,1); %subject
R(:,2) = repmat(reshape(repmat(1:length(sled),c/length(sled),1),c,1),n_subj,1); %speed
R(:,3) = repmat(reshape(repmat(1:(length(jumps)-1),c/(length(sled)*(length(jumps)-1)),1),c/length(sled),1),n_subj*length(sled),1); %jumps
R(:,4) = repmat(reshape(repmat(1:length(sled_end),c/(length(sled)*((length(jumps)-1))*length(sled_end)),1),c/(length(sled)*(length(jumps)-1)),1),n_subj*length(sled)*(length(jumps)-1),1); %sled direction
R(:,5) = repmat([1 1 2 2],1,length(R(:,1))/4); %jump direction
R(:,6) = repmat(1:2,1,length(R(:,1))/2); %half of trials
gain_exp1_R_2 = [R reshape(permute(abs(gain_2),[6 5 4 3 2 1]),[],1)];

%post-hoc sled acceleration at moment of jump versus sled condition

% save('/Users/leonieoostwoudwijdenes/surfdrive/Experiments/Donders/ManualReflex/RDM/derivatives/DataforRexp1', 'MT_control1_R','RT_control1_R','MT_exp1_R','RT_exp1_R','gain_exp1_R');
save('/Users/leonieoostwoudwijdenes/surfdrive/Experiments/Donders/ManualReflex/Data/DataforRob', 'ref_pos_x_means','ref_pos_y_means','labels','sf')