function [norm_all,DD,ind] = exp1_individual(VP_num,normalize,data);

%
% function [norm_all,DD,ind] = exp1_individual(VP_num,normalize,data);
%
% internal function called by figure2_3.m
%

tab = data{1,VP_num}.table;
DomDurs = data{1,VP_num}.DomDurs;
% trialtab: vel act rewL rewR attL attR leftred
% 1: vel = 1: grating presented to left eye moves left /=0 ...left moves right
% 2: act = 1: active trial --> button presses
% 3: rewL: leftwards moving stimulus rewarded
% 4: rewR: rightwards moving stimulus rewarded
% 5: attL: leftwards moving stimulus attended
% 6: attR: rightwards moving stimulus attended
% 7: leftred: grating presented to left eye is red

ind.rew_l_pass = tab(:,3) & ~tab(:,2);
ind.rew_l_act = tab(:,3) & tab(:,2);
ind.rew_r_pass = tab(:,4) & ~tab(:,2);
ind.rew_r_act = tab(:,4) & tab(:,2);
ind.att_l_pass = tab(:,5) & ~tab(:,2);
ind.att_l_act = tab(:,5) & tab(:,2);
ind.att_r_pass = tab(:,6) & ~tab(:,2);
ind.att_r_act = tab(:,6) & tab(:,2);
ind.BR_pass = ~tab(:,3) & ~tab(:,4) & ~tab(:,5) & ~tab(:,6) & ~tab(:,2);
ind.BR_act = ~tab(:,3) & ~tab(:,4) & ~tab(:,5) & ~tab(:,6) & tab(:,2);

ind.lefteye_rew = (tab(:,1) & tab(:,3)) | (~tab(:,1) & tab(:,4)) ;
ind.righteye_rew = (~tab(:,1) & tab(:,3)) | (tab(:,1) & ~tab(:,4));
ind.lefteye_att = (tab(:,1) & tab(:,5)) | (~tab(:,1) & tab(:,6)) ;
ind.righteye_att = (~tab(:,1) & tab(:,5)) | (tab(:,1) & ~tab(:,6));

ind.green_r = tab(:,7) & tab(:,1); % green grating moves rightwards
ind.green_l = tab(:,7) & ~tab(:,1); % green grating moves leftwards

% normalization
if normalize == 1
    norm = median([DomDurs.leftDir DomDurs.rightDir]);
elseif normalize == 0
    norm = 1;
end

% reward
DD.rewL_pass_L = [DomDurs(ind.rew_l_pass').leftDir]./norm; 
DD.rewL_pass_R = [DomDurs(ind.rew_l_pass').rightDir]./norm; 
DD.rewR_pass_L = [DomDurs(ind.rew_r_pass').leftDir]./norm; 
DD.rewR_pass_R = [DomDurs(ind.rew_r_pass').rightDir]./norm; 
DD.rewL_act_L = [DomDurs(ind.rew_l_act').leftDir]./norm; 
DD.rewL_act_R = [DomDurs(ind.rew_l_act').rightDir]./norm; 
DD.rewR_act_L = [DomDurs(ind.rew_r_act').leftDir]./norm; 
DD.rewR_act_R = [DomDurs(ind.rew_r_act').rightDir]./norm; 
DD.rewL_act_L_But = [DomDurs(ind.rew_l_act').But_leftDir]./norm; 
DD.rewL_act_R_But = [DomDurs(ind.rew_l_act').But_rightDir]./norm; 
DD.rewR_act_L_But = [DomDurs(ind.rew_r_act').But_leftDir]./norm; 
DD.rewR_act_R_But = [DomDurs(ind.rew_r_act').But_rightDir]./norm; 
%attention
DD.attL_pass_L = [DomDurs(ind.att_l_pass').leftDir]./norm; 
DD.attL_pass_R = [DomDurs(ind.att_l_pass').rightDir]./norm; 
DD.attR_pass_L = [DomDurs(ind.att_r_pass').leftDir]./norm; 
DD.attR_pass_R = [DomDurs(ind.att_r_pass').rightDir]./norm; 
DD.attL_act_L = [DomDurs(ind.att_l_act').leftDir]./norm; 
DD.attL_act_R = [DomDurs(ind.att_l_act').rightDir]./norm; 
DD.attR_act_L = [DomDurs(ind.att_r_act').leftDir]./norm; 
DD.attR_act_R = [DomDurs(ind.att_r_act').rightDir]./norm; 
DD.attL_act_L_But = [DomDurs(ind.att_l_act').But_leftDir]./norm; 
DD.attL_act_R_But = [DomDurs(ind.att_l_act').But_rightDir]./norm; 
DD.attR_act_L_But = [DomDurs(ind.att_r_act').But_leftDir]./norm; 
DD.attR_act_R_But = [DomDurs(ind.att_r_act').But_rightDir]./norm; 
% BR
DD.BR_pass_L = [DomDurs(ind.BR_pass').leftDir]./norm;
DD.BR_pass_R = [DomDurs(ind.BR_pass').rightDir]./norm;
DD.BR_act_L = [DomDurs(ind.BR_act').leftDir]./norm;
DD.BR_act_R = [DomDurs(ind.BR_act').rightDir]./norm;
DD.BR_act_L_But = [DomDurs(ind.BR_act').But_leftDir]./norm;
DD.BR_act_R_But = [DomDurs(ind.BR_act').But_rightDir]./norm;


DD.BR_green_pass_R = [DomDurs(ind.BR_pass'& ind.green_r').rightDir]./norm;
DD.BR_green_pass_L = [DomDurs(ind.BR_pass'& ind.green_l').leftDir]./norm;
DD.BR_red_pass_L = [DomDurs(ind.BR_pass'& ind.green_r').leftDir]./norm;
DD.BR_red_pass_R = [DomDurs(ind.BR_pass'& ind.green_l').rightDir]./norm;
DD.BR_green_act_R = [DomDurs(ind.BR_act'& ind.green_r').rightDir]./norm;
DD.BR_green_act_L = [DomDurs(ind.BR_act'& ind.green_l').leftDir]./norm;
DD.BR_red_act_L = [DomDurs(ind.BR_act'& ind.green_r').leftDir]./norm;
DD.BR_red_act_R = [DomDurs(ind.BR_act'& ind.green_l').rightDir]./norm;

DD.BR_green_pass = [DD.BR_green_pass_R DD.BR_green_pass_L]; 
DD.BR_green_act = [DD.BR_green_act_R DD.BR_green_act_L]; 
DD.BR_red_pass = [DD.BR_red_pass_R DD.BR_red_pass_L]; 
DD.BR_red_act = [DD.BR_red_act_R DD.BR_red_act_L]; 

norm_all.pass.DomDur_BR = [DD.BR_pass_L DD.BR_pass_R];
norm_all.act.DomDur_BR = [DD.BR_act_L DD.BR_act_R];
norm_all.act.DomDur_BR_But = [DD.BR_act_L_But DD.BR_act_R_But];

norm_all.act.DomDur_rew = [DD.rewL_act_L DD.rewR_act_R] ;
norm_all.act.DomDur_rew_other = [DD.rewL_act_R DD.rewR_act_L] ;
norm_all.act.DomDur_rew_But = [DD.rewL_act_L_But DD.rewR_act_R_But] ;
norm_all.act.DomDur_rew_other_But = [DD.rewL_act_R_But DD.rewR_act_L_But] ;
norm_all.pass.DomDur_rew = [DD.rewL_pass_L DD.rewR_pass_R] ;
norm_all.pass.DomDur_rew_other = [DD.rewL_pass_R DD.rewR_pass_L] ;

norm_all.act.DomDur_att = [DD.attL_act_L DD.attR_act_R] ;
norm_all.act.DomDur_att_other = [DD.attL_act_R DD.attR_act_L] ;
norm_all.act.DomDur_att_But = [DD.attL_act_L_But DD.attR_act_R_But] ;
norm_all.act.DomDur_att_other_But = [DD.attL_act_R_But DD.attR_act_L_But] ;
norm_all.pass.DomDur_att = [DD.attL_pass_L DD.attR_pass_R] ;
norm_all.pass.DomDur_att_other = [DD.attL_pass_R DD.attR_pass_L] ;

time = 180*8;

norm_all.switchrate = [length(norm_all.act.DomDur_BR)  length(norm_all.pass.DomDur_BR) ...
    length([norm_all.act.DomDur_att norm_all.act.DomDur_att_other]) length([norm_all.pass.DomDur_att norm_all.pass.DomDur_att_other]) ... 
    length([norm_all.act.DomDur_rew norm_all.act.DomDur_rew_other]) length([norm_all.pass.DomDur_rew norm_all.pass.DomDur_rew_other])]./time; 
