function average_click(listfile,EMGleg,path1,numpts,mass,leg_lengths,plotit,save_plt,rseries,rfile)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% average_click takes all of the .mot files listed in listfile, finds and
% normalizes their gait cycles, computes their means and standard
% deviations, and saves the output.  No circshifting is currently
% implemented. Also saves files with GC data and mechanical variables.
% This script differs from average_new only in that GC removal is
% done via select_curves.
%
% Inputs:
% listfile    - file containing the list of .mot files to be processed.
% EMGleg      - L or R; refers to leg with EMG electrodes.
% path1       - folder where .mot files are located.
% numpts      - number of points to break gait cycle into (100 for pct).
% mass        - mass of subject in kg.
% leg_lengths - structure of leg lengths (asis to lateral malleolus)
% plotit      - 0 for no plots, 1 for all plots, 2 for just summary plots. 
% save_plt    - option to save plot (0 for no, 1 for yes).
% rseries     - cell of gait cycles to discard.  R first, then L.
%              Cell should include two row vectors of gait cycle numbers.
% rfile       - File containing vectors of removed GC.  Supplants
%              rseries if included.
%
% Outputs: Five output files which include:
% (1) all normalized, mean, and sd arrays (muscles included).
% (2) training set with mean, sd arrays of train subset.
% (3) testing set with mean, sd arrays of test subset.
% (4) mechanical variables file with all mech data (including cells).
% (5) EMGleg, vectors of all removed GC for each leg.
%
% Example Usage:
% average_new('walk125','R','EM072611',100,78.8342,2,'~/Desktop')
%
% Jared Markowitz
% Spring 2012
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

fprintf(['Path: ' path1 '\nListfile: ' listfile '\n']);

% Set path and read list of input files:
path_full = ['~/Documents/MATLAB/data/' path1 '/processed/mot/simm_out'];

fid1  = fopen([path_full '/' listfile '.txt']);
tline = fgetl(fid1);
list1 = {};

spd = str2num(listfile(5:7))/100;

while ischar(tline)
  list1 = [list1; {tline}];  
  tline = fgetl(fid1);
end;  

fclose('all');

if plotit == 1
  close all;
end;  

% Initialize cells:

ankle_ang_r  = {}; ankle_ang_l  = {};
ankle_mom_r  = {}; ankle_mom_l  = {};
ankle_vel_r  = {}; ankle_vel_l  = {};
ankle_work_r = {}; ankle_work_l = {};
ankle_pow_r  = {}; ankle_pow_l  = {};
knee_ang_r   = {}; knee_ang_l   = {};
knee_mom_r   = {}; knee_mom_l   = {};
knee_vel_r   = {}; knee_vel_l   = {};
knee_work_r  = {}; knee_work_l  = {};
knee_pow_r   = {}; knee_pow_l   = {};
hip_ang_r    = {}; hip_ang_l    = {};
hip_mom_r    = {}; hip_mom_l    = {};
hip_vel_r    = {}; hip_vel_l    = {};
hip_work_r   = {}; hip_work_l   = {};
hip_pow_r    = {}; hip_pow_l    = {};

cop_l_x  = {};  cop_l_y  = {};  cop_l_z  = {};
grf_l_x  = {};  grf_l_y  = {};  grf_l_z  = {};
gtor_l_x = {};  gtor_l_y = {};  gtor_l_z = {};
cop_r_x  = {};  cop_r_y  = {};  cop_r_z  = {};
grf_r_x  = {};  grf_r_y  = {};  grf_r_z  = {};
gtor_r_x = {};  gtor_r_y = {};  gtor_r_z = {};
com_l_x  = {};  com_l_y  = {};  com_l_z  = {};
com_r_x  = {};  com_r_y  = {};  com_r_z  = {};

add_long_len    = {}; add_mag1_len    = {};
add_mag2_len    = {}; add_mag3_len    = {};
glut_med1_len   = {}; glut_med2_len   = {};
glut_med3_len   = {}; glut_max1_len   = {}; 
glut_max2_len   = {}; glut_max3_len   = {};
iliacus_len     = {}; psoas_len       = {};
semimem_len     = {}; semiten_len     = {};
bi_fem_lh_len   = {}; rectus_fem_len  = {}; 
bi_fem_sh_len   = {}; vas_lat_len     = {};
vas_med_len     = {}; vas_int_len     = {};
gas_med_len     = {}; gas_lat_len     = {}; 
soleus_len      = {}; tib_ant_len     = {};

add_long_ma_h   = {}; add_mag1_ma_h   = {};
add_mag2_ma_h   = {}; add_mag3_ma_h   = {};
glut_med1_ma_h  = {}; glut_med2_ma_h  = {};
glut_med3_ma_h  = {}; glut_max1_ma_h  = {};
glut_max2_ma_h  = {}; glut_max3_ma_h  = {};
iliacus_ma_h    = {}; psoas_ma_h      = {};
semimem_ma_h    = {}; semimem_ma_k    = {};
semiten_ma_h    = {}; semiten_ma_k    = {};
bi_fem_lh_ma_h  = {}; bi_fem_lh_ma_k  = {};
rectus_fem_ma_h = {}; rectus_fem_ma_k = {};
bi_fem_sh_ma_k  = {}; vas_lat_ma_k    = {}; 
vas_med_ma_k    = {}; vas_int_ma_k    = {}; 
gas_med_ma_k    = {}; gas_med_ma_a    = {}; 
gas_lat_ma_k    = {}; gas_lat_ma_a    = {}; 
soleus_ma_a     = {}; tib_ant_ma_a    = {};

gc_breaks_l     = {}; gc_breaks_r     = {};
endstance_l     = []; endstance_r     = [];

% Determine gait cycles:
             
for i = 1:length(list1)
  
  data = getData(list1{i},EMGleg,path1,[2 3 4 5 6]); 
  hs_l = [];  hs_r = [];
  to_l = [];  to_r = [];
  
  ldown = find(data.fz1 > 0); 
  rdown = find(data.fz2 > 0);
  
  for k = 2:length(ldown)
    if ldown(k)-ldown(k-1) > 35  
      hs_l = cat(1,hs_l,ldown(k));
      to_l = cat(1,to_l,(ldown(k-1)+1));
    end;
  end;

  for l = 2:length(rdown)
    if rdown(l)-rdown(l-1) > 35
      hs_r = cat(1,hs_r,rdown(l));
      to_r = cat(1,to_r,(rdown(l-1)+1));
    end;
  end;

%  compare_fp_heel

  Fs = 125;  dt = 1/Fs;
  
  ankle_v_l  = diff(data.ankle_ang_l)*Fs*pi/180;  
  knee_v_l   = diff(data.knee_ang_l)*Fs*pi/180; 
  hip_v_l    = diff(data.hip_ang_l)*Fs*pi/180; 
  ankle_w_l  = cumtrapz(data.ankle_ang_l*pi/180,data.ankle_mom_l);
  knee_w_l   = cumtrapz(data.knee_ang_l*pi/180,data.knee_mom_l);
  hip_w_l    = cumtrapz(data.hip_ang_l*pi/180,data.hip_mom_l);  
  ankle_p_l  = Fs*diff(ankle_w_l);
  knee_p_l   = Fs*diff(knee_w_l);  
  hip_p_l    = Fs*diff(hip_w_l);  
  
  ankle_v_r  = diff(data.ankle_ang_r)*Fs*pi/180;  
  knee_v_r   = diff(data.knee_ang_r)*Fs*pi/180; 
  hip_v_r    = diff(data.hip_ang_r)*Fs*pi/180;   
  ankle_w_r  = cumtrapz(data.ankle_ang_r*pi/180,data.ankle_mom_r);
  knee_w_r   = cumtrapz(data.knee_ang_r*pi/180,data.knee_mom_r);
  hip_w_r    = cumtrapz(data.hip_ang_r*pi/180,data.hip_mom_r);
  ankle_p_r  = Fs*diff(ankle_w_r);
  knee_p_r   = Fs*diff(knee_w_r);  
  hip_p_r    = Fs*diff(hip_w_r);  

  for m = 1:(length(hs_l)-1)
    indstart = hs_l(m);
    indstop  = hs_l(m+1)-1;
    
    indto_l  = to_l(intersect(find(to_l>indstart),find(to_l<indstop))); 
    endstance_l       = [endstance_l; (indto_l-indstart+1)];
    
    ankle_ang_l       = [ankle_ang_l; {data.ankle_ang_l(indstart:indstop)}];
    knee_ang_l        = [knee_ang_l; {data.knee_ang_l(indstart:indstop)}];
    hip_ang_l         = [hip_ang_l; {data.hip_ang_l(indstart:indstop)}];
    ankle_mom_l       = [ankle_mom_l; {data.ankle_mom_l(indstart:indstop)}];
    knee_mom_l        = [knee_mom_l; {data.knee_mom_l(indstart:indstop)}];
    hip_mom_l         = [hip_mom_l; {data.hip_mom_l(indstart:indstop)}];
    ankle_vel_l       = [ankle_vel_l; {ankle_v_l(indstart:indstop)}];
    knee_vel_l        = [knee_vel_l; {knee_v_l(indstart:indstop)}];
    hip_vel_l         = [hip_vel_l; {hip_v_l(indstart:indstop)}];
    ankle_work_l      = [ankle_work_l; {ankle_w_l(indstart:indstop)}];
    knee_work_l       = [knee_work_l; {knee_w_l(indstart:indstop)}];
    hip_work_l        = [hip_work_l; {hip_w_l(indstart:indstop)}];    
    ankle_pow_l       = [ankle_pow_l; {ankle_p_l(indstart:indstop)}];
    knee_pow_l        = [knee_pow_l; {knee_p_l(indstart:indstop)}];
    hip_pow_l         = [hip_pow_l; {hip_p_l(indstart:indstop)}]; 

    tread     = 0:dt*spd:(indstop-indstart)*dt*spd;
    cop_l_x   = [cop_l_x; {data.cop_l_x(indstart:indstop)}];
    cop_l_y   = [cop_l_y; {data.cop_l_y(indstart:indstop)+tread'}];
    cop_l_z   = [cop_l_z; {data.cop_l_z(indstart:indstop)}];
    grf_l_x   = [grf_l_x; {data.grf_l_x(indstart:indstop)}];
    grf_l_y   = [grf_l_y; {data.grf_l_y(indstart:indstop)}];
    grf_l_z   = [grf_l_z; {data.grf_l_z(indstart:indstop)}];
    gtor_l_x  = [gtor_l_x; {data.gtor_l_x(indstart:indstop)}];
    gtor_l_y  = [gtor_l_y; {data.gtor_l_y(indstart:indstop)}];
    gtor_l_z  = [gtor_l_z; {data.gtor_l_z(indstart:indstop)}];
    com_l_x   = [com_l_x; {data.com_x(indstart:indstop)}];
    com_l_y   = [com_l_y; {data.com_y(indstart:indstop)+tread'}];
    com_l_z   = [com_l_z; {data.com_z(indstart:indstop)}];
  end;

  for n = 1:(length(hs_r)-1)
    indstart = hs_r(n);
    indstop  = hs_r(n+1)-1;
    
    indto_r  = to_r(intersect(find(to_r>indstart),find(to_r<indstop))); 
    endstance_r       = [endstance_r; (indto_r-indstart+1)];    
    
    ankle_ang_r       = [ankle_ang_r; {data.ankle_ang_r(indstart:indstop)}];
    knee_ang_r        = [knee_ang_r; {data.knee_ang_r(indstart:indstop)}];
    hip_ang_r         = [hip_ang_r; {data.hip_ang_r(indstart:indstop)}];
    ankle_mom_r       = [ankle_mom_r; {data.ankle_mom_r(indstart:indstop)}];
    knee_mom_r        = [knee_mom_r; {data.knee_mom_r(indstart:indstop)}];
    hip_mom_r         = [hip_mom_r; {data.hip_mom_r(indstart:indstop)}];
    ankle_vel_r       = [ankle_vel_r; {ankle_v_r(indstart:indstop)}];
    knee_vel_r        = [knee_vel_r; {knee_v_r(indstart:indstop)}];
    hip_vel_r         = [hip_vel_r; {hip_v_r(indstart:indstop)}];
    ankle_work_r      = [ankle_work_r; {ankle_w_r(indstart:indstop)}];
    knee_work_r       = [knee_work_r; {knee_w_r(indstart:indstop)}];
    hip_work_r        = [hip_work_r; {hip_w_r(indstart:indstop)}];      
    ankle_pow_r       = [ankle_pow_r; {ankle_p_r(indstart:indstop)}];
    knee_pow_r        = [knee_pow_r; {knee_p_r(indstart:indstop)}];
    hip_pow_r         = [hip_pow_r; {hip_p_r(indstart:indstop)}];  
    
    tread     = 0:dt*spd:(indstop-indstart)*dt*spd;
    cop_r_x   = [cop_r_x; {data.cop_r_x(indstart:indstop)}];
    cop_r_y   = [cop_r_y; {data.cop_r_y(indstart:indstop)+tread'}];
    cop_r_z   = [cop_r_z; {data.cop_r_z(indstart:indstop)}];
    grf_r_x   = [grf_r_x; {data.grf_r_x(indstart:indstop)}];
    grf_r_y   = [grf_r_y; {data.grf_r_y(indstart:indstop)}];
    grf_r_z   = [grf_r_z; {data.grf_r_z(indstart:indstop)}];
    gtor_r_x  = [gtor_r_x; {data.gtor_r_x(indstart:indstop)}];
    gtor_r_y  = [gtor_r_y; {data.gtor_r_y(indstart:indstop)}];
    gtor_r_z  = [gtor_r_z; {data.gtor_r_z(indstart:indstop)}];
    com_r_x   = [com_r_x; {data.com_x(indstart:indstop)}];
    com_r_y   = [com_r_y; {data.com_y(indstart:indstop)+tread'}];
    com_r_z   = [com_r_z; {data.com_z(indstart:indstop)}];
  end;

  if strcmp(EMGleg,'L') == 1
    hs_emg   = hs_l;
  elseif strcmp(EMGleg,'R') == 1
    hs_emg   = hs_r;    
  else
    fprintf('EMGleg not input correctly.');
  end;
  
  for nm = 1:(length(hs_emg)-1)
    indstart = hs_emg(nm);
    indstop  = hs_emg(nm+1)-1;  
   
    add_long_len    = [add_long_len; {data.add_long_len(indstart:indstop)}]; 
    add_mag1_len    = [add_mag1_len; {data.add_mag1_len(indstart:indstop)}];
    add_mag2_len    = [add_mag2_len; {data.add_mag2_len(indstart:indstop)}];
    add_mag3_len    = [add_mag3_len; {data.add_mag3_len(indstart:indstop)}];    
    glut_med1_len   = [glut_med1_len; {data.glut_med1_len(indstart:indstop)}];
    glut_med2_len   = [glut_med2_len; {data.glut_med2_len(indstart:indstop)}];    
    glut_med3_len   = [glut_med3_len; {data.glut_med3_len(indstart:indstop)}]; 
    glut_max1_len   = [glut_max1_len; {data.glut_max1_len(indstart:indstop)}];
    glut_max2_len   = [glut_max2_len; {data.glut_max2_len(indstart:indstop)}];    
    glut_max3_len   = [glut_max3_len; {data.glut_max3_len(indstart:indstop)}]; 
    iliacus_len     = [iliacus_len; {data.iliacus_len(indstart:indstop)}];
    psoas_len       = [psoas_len; {data.psoas_len(indstart:indstop)}];    
    semimem_len     = [semimem_len; {data.semimem_len(indstart:indstop)}];
    semiten_len     = [semiten_len; {data.semiten_len(indstart:indstop)}];    
    bi_fem_lh_len   = [bi_fem_lh_len; {data.bi_fem_lh_len(indstart:indstop)}];
    rectus_fem_len  = [rectus_fem_len; {data.rectus_fem_len(indstart:indstop)}];
    bi_fem_sh_len   = [bi_fem_sh_len; {data.bi_fem_sh_len(indstart:indstop)}];
    vas_lat_len     = [vas_lat_len; {data.vas_lat_len(indstart:indstop)}];
    vas_med_len     = [vas_med_len; {data.vas_med_len(indstart:indstop)}];
    vas_int_len     = [vas_int_len; {data.vas_int_len(indstart:indstop)}];
    gas_med_len     = [gas_med_len; {data.gas_med_len(indstart:indstop)}];
    gas_lat_len     = [gas_lat_len; {data.gas_lat_len(indstart:indstop)}];
    soleus_len      = [soleus_len; {data.soleus_len(indstart:indstop)}];
    tib_ant_len     = [tib_ant_len; {data.tib_ant_len(indstart:indstop)}];
    
    add_long_ma_h   = [add_long_ma_h; {data.add_long_ma_h(indstart:indstop)}];
    add_mag1_ma_h   = [add_mag1_ma_h; {data.add_mag1_ma_h(indstart:indstop)}];
    add_mag2_ma_h   = [add_mag2_ma_h; {data.add_mag2_ma_h(indstart:indstop)}];
    add_mag3_ma_h   = [add_mag3_ma_h; {data.add_mag3_ma_h(indstart:indstop)}];    
    glut_med1_ma_h  = [glut_med1_ma_h; {data.glut_med1_ma_h(indstart:indstop)}];
    glut_med2_ma_h  = [glut_med2_ma_h; {data.glut_med2_ma_h(indstart:indstop)}];
    glut_med3_ma_h  = [glut_med3_ma_h; {data.glut_med3_ma_h(indstart:indstop)}];
    glut_max1_ma_h  = [glut_max1_ma_h; {data.glut_max1_ma_h(indstart:indstop)}];
    glut_max2_ma_h  = [glut_max2_ma_h; {data.glut_max2_ma_h(indstart:indstop)}];
    glut_max3_ma_h  = [glut_max3_ma_h; {data.glut_max3_ma_h(indstart:indstop)}];
    iliacus_ma_h    = [iliacus_ma_h; {data.iliacus_ma_h(indstart:indstop)}];
    psoas_ma_h      = [psoas_ma_h; {data.psoas_ma_h(indstart:indstop)}];    
    semimem_ma_h    = [semimem_ma_h; {data.semimem_ma_h(indstart:indstop)}];
    semimem_ma_k    = [semimem_ma_k; {data.semimem_ma_k(indstart:indstop)}];
    semiten_ma_h    = [semiten_ma_h; {data.semiten_ma_h(indstart:indstop)}];
    semiten_ma_k    = [semiten_ma_k; {data.semiten_ma_k(indstart:indstop)}];
    bi_fem_lh_ma_h  = [bi_fem_lh_ma_h; {data.bi_fem_lh_ma_h(indstart:indstop)}];
    bi_fem_lh_ma_k  = [bi_fem_lh_ma_k; {data.bi_fem_lh_ma_k(indstart:indstop)}];
    rectus_fem_ma_h = [rectus_fem_ma_h; {data.rectus_fem_ma_h(indstart:indstop)}];
    rectus_fem_ma_k = [rectus_fem_ma_k; {data.rectus_fem_ma_k(indstart:indstop)}];    
    bi_fem_sh_ma_k  = [bi_fem_sh_ma_k; {data.bi_fem_sh_ma_k(indstart:indstop)}];    
    vas_lat_ma_k    = [vas_lat_ma_k; {data.vas_lat_ma_k(indstart:indstop)}];
    vas_med_ma_k    = [vas_med_ma_k; {data.vas_med_ma_k(indstart:indstop)}];
    vas_int_ma_k    = [vas_int_ma_k; {data.vas_int_ma_k(indstart:indstop)}];    
    gas_med_ma_k    = [gas_med_ma_k; {data.gas_med_ma_k(indstart:indstop)}];
    gas_med_ma_a    = [gas_med_ma_a; {data.gas_med_ma_a(indstart:indstop)}];
    gas_lat_ma_k    = [gas_lat_ma_k; {data.gas_lat_ma_k(indstart:indstop)}];
    gas_lat_ma_a    = [gas_lat_ma_a; {data.gas_lat_ma_a(indstart:indstop)}];            
    soleus_ma_a     = [soleus_ma_a; {data.soleus_ma_a(indstart:indstop)}];
    tib_ant_ma_a    = [tib_ant_ma_a; {data.tib_ant_ma_a(indstart:indstop)}];
    
  end;
  
  gc_breaks_l = [gc_breaks_l;{hs_l}];
  gc_breaks_r = [gc_breaks_r;{hs_r}];
  
  fprintf([list1{i} '\n']);
   
end;  

% Now convert to percent gait cycle and average:

% Initialize arrays:

n_ankle_ang_r  = []; n_ankle_ang_l  = [];
n_ankle_mom_r  = []; n_ankle_mom_l  = [];
n_ankle_vel_r  = []; n_ankle_vel_l  = [];
n_ankle_work_r = []; n_ankle_work_l = [];
n_ankle_pow_r  = []; n_ankle_pow_l  = [];
n_knee_ang_r   = []; n_knee_ang_l   = [];
n_knee_mom_r   = []; n_knee_mom_l   = [];
n_knee_vel_r   = []; n_knee_vel_l   = [];
n_knee_work_r  = []; n_knee_work_l  = [];
n_knee_pow_r   = []; n_knee_pow_l   = [];
n_hip_ang_r    = []; n_hip_ang_l    = [];
n_hip_mom_r    = []; n_hip_mom_l    = [];
n_hip_vel_r    = []; n_hip_vel_l    = [];
n_hip_work_r   = []; n_hip_work_l   = [];
n_hip_pow_r    = []; n_hip_pow_l    = [];

n_cop_l_x  = [];  n_cop_l_y  = [];  n_cop_l_z  = [];
n_grf_l_x  = [];  n_grf_l_y  = [];  n_grf_l_z  = [];
n_gtor_l_x = [];  n_gtor_l_y = [];  n_gtor_l_z = [];
n_cop_r_x  = [];  n_cop_r_y  = [];  n_cop_r_z  = [];
n_grf_r_x  = [];  n_grf_r_y  = [];  n_grf_r_z  = [];
n_gtor_r_x = [];  n_gtor_r_y = [];  n_gtor_r_z = [];
n_com_l_x  = [];  n_com_l_y  = [];  n_com_l_z  = [];
n_com_r_x  = [];  n_com_r_y  = [];  n_com_r_z  = [];

n_add_long_len    = []; n_add_mag1_len    = [];
n_add_mag2_len    = []; n_add_mag3_len    = [];
n_glut_med1_len   = []; n_glut_med2_len   = [];
n_glut_med3_len   = []; n_glut_max1_len   = []; 
n_glut_max2_len   = []; n_glut_max3_len   = [];
n_iliacus_len     = []; n_psoas_len       = [];
n_semimem_len     = []; n_semiten_len     = [];
n_bi_fem_lh_len   = []; n_rectus_fem_len  = []; 
n_bi_fem_sh_len   = []; n_vas_lat_len     = [];
n_vas_med_len     = []; n_vas_int_len     = [];
n_gas_med_len     = []; n_gas_lat_len     = []; 
n_soleus_len      = []; n_tib_ant_len     = [];

n_add_long_ma_h   = []; n_add_mag1_ma_h   = [];
n_add_mag2_ma_h   = []; n_add_mag3_ma_h   = [];
n_glut_med1_ma_h  = []; n_glut_med2_ma_h  = [];
n_glut_med3_ma_h  = []; n_glut_max1_ma_h  = [];
n_glut_max2_ma_h  = []; n_glut_max3_ma_h  = [];
n_iliacus_ma_h    = []; n_psoas_ma_h      = [];
n_semimem_ma_h    = []; n_semimem_ma_k    = [];
n_semiten_ma_h    = []; n_semiten_ma_k    = [];
n_bi_fem_lh_ma_h  = []; n_bi_fem_lh_ma_k  = [];
n_rectus_fem_ma_h = []; n_rectus_fem_ma_k = [];
n_bi_fem_sh_ma_k  = []; n_vas_lat_ma_k    = []; 
n_vas_med_ma_k    = []; n_vas_int_ma_k    = []; 
n_gas_med_ma_k    = []; n_gas_med_ma_a    = []; 
n_gas_lat_ma_k    = []; n_gas_lat_ma_a    = []; 
n_soleus_ma_a     = []; n_tib_ant_ma_a    = [];

numpts = numpts+1;

for p = 1:length(ankle_mom_r)
  x1 = 0:dt:(length(ankle_mom_r{p})-1)*dt;
  x2 = linspace(0,x1(end),numpts);

  n_ankle_ang_r  = [n_ankle_ang_r; interp1(x1,ankle_ang_r{p},x2,'spline')];
  n_knee_ang_r   = [n_knee_ang_r; interp1(x1,knee_ang_r{p},x2,'spline')];
  n_hip_ang_r    = [n_hip_ang_r; interp1(x1,hip_ang_r{p},x2,'spline')];
  n_ankle_mom_r  = [n_ankle_mom_r; interp1(x1,ankle_mom_r{p},x2,'spline')];
  n_knee_mom_r   = [n_knee_mom_r; interp1(x1,knee_mom_r{p},x2,'spline')];
  n_hip_mom_r    = [n_hip_mom_r; interp1(x1,hip_mom_r{p},x2,'spline')];
  n_ankle_vel_r  = [n_ankle_vel_r; interp1(x1,ankle_vel_r{p},x2,'spline')];
  n_knee_vel_r   = [n_knee_vel_r; interp1(x1,knee_vel_r{p},x2,'spline')];
  n_hip_vel_r    = [n_hip_vel_r; interp1(x1,hip_vel_r{p},x2,'spline')];
  n_ankle_work_r = [n_ankle_work_r; interp1(x1,ankle_work_r{p},x2,'spline')];
  n_knee_work_r  = [n_knee_work_r; interp1(x1,knee_work_r{p},x2,'spline')];
  n_hip_work_r   = [n_hip_work_r; interp1(x1,hip_work_r{p},x2,'spline')]; 
  n_ankle_pow_r  = [n_ankle_pow_r; interp1(x1,ankle_pow_r{p},x2,'spline')];
  n_knee_pow_r   = [n_knee_pow_r; interp1(x1,knee_pow_r{p},x2,'spline')];
  n_hip_pow_r    = [n_hip_pow_r; interp1(x1,hip_pow_r{p},x2,'spline')];
      
  n_cop_r_x  = [n_cop_r_x; interp1(x1,cop_r_x{p},x2,'spline')];
  n_cop_r_y  = [n_cop_r_y; interp1(x1,cop_r_y{p},x2,'spline')];
  n_cop_r_z  = [n_cop_r_z; interp1(x1,cop_r_z{p},x2,'spline')];
  n_grf_r_x  = [n_grf_r_x; interp1(x1,grf_r_x{p},x2,'spline')];
  n_grf_r_y  = [n_grf_r_y; interp1(x1,grf_r_y{p},x2,'spline')];
  n_grf_r_z  = [n_grf_r_z; interp1(x1,grf_r_z{p},x2,'spline')];
  n_gtor_r_x = [n_gtor_r_x; interp1(x1,gtor_r_x{p},x2,'spline')];
  n_gtor_r_y = [n_gtor_r_y; interp1(x1,gtor_r_y{p},x2,'spline')];
  n_gtor_r_z = [n_gtor_r_z; interp1(x1,gtor_r_z{p},x2,'spline')];
  n_com_r_x  = [n_com_r_x; interp1(x1,com_r_x{p},x2,'spline')];
  n_com_r_y  = [n_com_r_y; interp1(x1,com_r_y{p},x2,'spline')];
  n_com_r_z  = [n_com_r_z; interp1(x1,com_r_z{p},x2,'spline')];    
      
end;

for q = 1:length(ankle_mom_l)
  x1 = 0:dt:(length(ankle_mom_l{q})-1)*dt;
  x2 = linspace(0,x1(end),numpts);

  n_ankle_ang_l  = [n_ankle_ang_l; interp1(x1,ankle_ang_l{q},x2,'spline')];
  n_knee_ang_l   = [n_knee_ang_l; interp1(x1,knee_ang_l{q},x2,'spline')];
  n_hip_ang_l    = [n_hip_ang_l; interp1(x1,hip_ang_l{q},x2,'spline')];
  n_ankle_mom_l  = [n_ankle_mom_l; interp1(x1,ankle_mom_l{q},x2,'spline')];
  n_knee_mom_l   = [n_knee_mom_l; interp1(x1,knee_mom_l{q},x2,'spline')];
  n_hip_mom_l    = [n_hip_mom_l; interp1(x1,hip_mom_l{q},x2,'spline')];
  n_ankle_vel_l  = [n_ankle_vel_l; interp1(x1,ankle_vel_l{q},x2,'spline')];
  n_knee_vel_l   = [n_knee_vel_l; interp1(x1,knee_vel_l{q},x2,'spline')];
  n_hip_vel_l    = [n_hip_vel_l; interp1(x1,hip_vel_l{q},x2,'spline')];
  n_ankle_work_l = [n_ankle_work_l; interp1(x1,ankle_work_l{q},x2,'spline')];
  n_knee_work_l  = [n_knee_work_l; interp1(x1,knee_work_l{q},x2,'spline')];
  n_hip_work_l   = [n_hip_work_l; interp1(x1,hip_work_l{q},x2,'spline')];  
  n_ankle_pow_l  = [n_ankle_pow_l; interp1(x1,ankle_pow_l{q},x2,'spline')];
  n_knee_pow_l   = [n_knee_pow_l; interp1(x1,knee_pow_l{q},x2,'spline')];
  n_hip_pow_l    = [n_hip_pow_l; interp1(x1,hip_pow_l{q},x2,'spline')];  

  n_cop_l_x  = [n_cop_l_x; interp1(x1,cop_l_x{q},x2,'spline')];
  n_cop_l_y  = [n_cop_l_y; interp1(x1,cop_l_y{q},x2,'spline')];
  n_cop_l_z  = [n_cop_l_z; interp1(x1,cop_l_z{q},x2,'spline')];
  n_grf_l_x  = [n_grf_l_x; interp1(x1,grf_l_x{q},x2,'spline')];
  n_grf_l_y  = [n_grf_l_y; interp1(x1,grf_l_y{q},x2,'spline')];
  n_grf_l_z  = [n_grf_l_z; interp1(x1,grf_l_z{q},x2,'spline')];
  n_gtor_l_x = [n_gtor_l_x; interp1(x1,gtor_l_x{q},x2,'spline')];
  n_gtor_l_y = [n_gtor_l_y; interp1(x1,gtor_l_y{q},x2,'spline')];
  n_gtor_l_z = [n_gtor_l_z; interp1(x1,gtor_l_z{q},x2,'spline')];  
  n_com_l_x  = [n_com_l_x; interp1(x1,com_l_x{q},x2,'spline')];
  n_com_l_y  = [n_com_l_y; interp1(x1,com_l_y{q},x2,'spline')];
  n_com_l_z  = [n_com_l_z; interp1(x1,com_l_z{q},x2,'spline')];

end;  

for u = 1:length(gas_med_len)
  x1 = 0:dt:(length(gas_med_len{u})-1)*dt;
  x2 = linspace(0,x1(end),numpts);

  n_add_long_len    = [n_add_long_len; interp1(x1,add_long_len{u},x2,'spline')];
  n_add_mag1_len    = [n_add_mag1_len; interp1(x1,add_mag1_len{u},x2,'spline')];
  n_add_mag2_len    = [n_add_mag2_len; interp1(x1,add_mag2_len{u},x2,'spline')];
  n_add_mag3_len    = [n_add_mag3_len; interp1(x1,add_mag3_len{u},x2,'spline')];  
  n_glut_med1_len   = [n_glut_med1_len; interp1(x1,glut_med1_len{u},x2,'spline')];
  n_glut_med2_len   = [n_glut_med2_len; interp1(x1,glut_med2_len{u},x2,'spline')];  
  n_glut_med3_len   = [n_glut_med3_len; interp1(x1,glut_med3_len{u},x2,'spline')];
  n_glut_max1_len   = [n_glut_max1_len; interp1(x1,glut_max1_len{u},x2,'spline')];
  n_glut_max2_len   = [n_glut_max2_len; interp1(x1,glut_max2_len{u},x2,'spline')];  
  n_glut_max3_len   = [n_glut_max3_len; interp1(x1,glut_max3_len{u},x2,'spline')];
  n_iliacus_len     = [n_iliacus_len; interp1(x1,iliacus_len{u},x2,'spline')];
  n_psoas_len       = [n_psoas_len; interp1(x1,psoas_len{u},x2,'spline')];
  n_semimem_len     = [n_semimem_len; interp1(x1,semimem_len{u},x2,'spline')];
  n_semiten_len     = [n_semiten_len; interp1(x1,semiten_len{u},x2,'spline')];
  n_bi_fem_lh_len   = [n_bi_fem_lh_len; interp1(x1,bi_fem_lh_len{u},x2,'spline')];
  n_rectus_fem_len  = [n_rectus_fem_len; interp1(x1,rectus_fem_len{u},x2,'spline')];
  n_bi_fem_sh_len   = [n_bi_fem_sh_len; interp1(x1,bi_fem_sh_len{u},x2,'spline')];
  n_vas_lat_len     = [n_vas_lat_len; interp1(x1,vas_lat_len{u},x2,'spline')];
  n_vas_med_len     = [n_vas_med_len; interp1(x1,vas_med_len{u},x2,'spline')];
  n_vas_int_len     = [n_vas_int_len; interp1(x1,vas_int_len{u},x2,'spline')]; 
  n_gas_med_len     = [n_gas_med_len; interp1(x1,gas_med_len{u},x2,'spline')];
  n_gas_lat_len     = [n_gas_lat_len; interp1(x1,gas_lat_len{u},x2,'spline')];
  n_soleus_len      = [n_soleus_len; interp1(x1,soleus_len{u},x2,'spline')];
  n_tib_ant_len     = [n_tib_ant_len; interp1(x1,tib_ant_len{u},x2,'spline')];

  n_add_long_ma_h   = [n_add_long_ma_h; interp1(x1,add_long_ma_h{u},x2,'spline')];
  n_add_mag1_ma_h   = [n_add_mag1_ma_h; interp1(x1,add_mag1_ma_h{u},x2,'spline')];
  n_add_mag2_ma_h   = [n_add_mag2_ma_h; interp1(x1,add_mag2_ma_h{u},x2,'spline')];
  n_add_mag3_ma_h   = [n_add_mag3_ma_h; interp1(x1,add_mag3_ma_h{u},x2,'spline')];
  n_glut_med1_ma_h  = [n_glut_med1_ma_h; interp1(x1,glut_med1_ma_h{u},x2,'spline')];
  n_glut_med2_ma_h  = [n_glut_med2_ma_h; interp1(x1,glut_med2_ma_h{u},x2,'spline')];
  n_glut_med3_ma_h  = [n_glut_med3_ma_h; interp1(x1,glut_med3_ma_h{u},x2,'spline')];
  n_glut_max1_ma_h  = [n_glut_max1_ma_h; interp1(x1,glut_max1_ma_h{u},x2,'spline')];
  n_glut_max2_ma_h  = [n_glut_max2_ma_h; interp1(x1,glut_max2_ma_h{u},x2,'spline')];
  n_glut_max3_ma_h  = [n_glut_max3_ma_h; interp1(x1,glut_max3_ma_h{u},x2,'spline')];
  n_iliacus_ma_h    = [n_iliacus_ma_h; interp1(x1,iliacus_ma_h{u},x2,'spline')];
  n_psoas_ma_h      = [n_psoas_ma_h; interp1(x1,psoas_ma_h{u},x2,'spline')];  
  n_semimem_ma_h    = [n_semimem_ma_h; interp1(x1,semimem_ma_h{u},x2,'spline')];
  n_semimem_ma_k    = [n_semimem_ma_k; interp1(x1,semimem_ma_k{u},x2,'spline')];
  n_semiten_ma_h    = [n_semiten_ma_h; interp1(x1,semiten_ma_h{u},x2,'spline')];
  n_semiten_ma_k    = [n_semiten_ma_k; interp1(x1,semiten_ma_k{u},x2,'spline')];
  n_bi_fem_lh_ma_h  = [n_bi_fem_lh_ma_h; interp1(x1,bi_fem_lh_ma_h{u},x2,'spline')];
  n_bi_fem_lh_ma_k  = [n_bi_fem_lh_ma_k; interp1(x1,bi_fem_lh_ma_k{u},x2,'spline')];
  n_rectus_fem_ma_h = [n_rectus_fem_ma_h; interp1(x1,rectus_fem_ma_h{u},x2,'spline')];
  n_rectus_fem_ma_k = [n_rectus_fem_ma_k; interp1(x1,rectus_fem_ma_k{u},x2,'spline')];
  n_bi_fem_sh_ma_k  = [n_bi_fem_sh_ma_k; interp1(x1,bi_fem_sh_ma_k{u},x2,'spline')];
  n_vas_lat_ma_k    = [n_vas_lat_ma_k; interp1(x1,vas_lat_ma_k{u},x2,'spline')];
  n_vas_med_ma_k    = [n_vas_med_ma_k; interp1(x1,vas_med_ma_k{u},x2,'spline')];
  n_vas_int_ma_k    = [n_vas_int_ma_k; interp1(x1,vas_int_ma_k{u},x2,'spline')];
  n_gas_med_ma_k    = [n_gas_med_ma_k; interp1(x1,gas_med_ma_k{u},x2,'spline')];
  n_gas_med_ma_a    = [n_gas_med_ma_a; interp1(x1,gas_med_ma_a{u},x2,'spline')];
  n_gas_lat_ma_k    = [n_gas_lat_ma_k; interp1(x1,gas_lat_ma_k{u},x2,'spline')];
  n_gas_lat_ma_a    = [n_gas_lat_ma_a; interp1(x1,gas_lat_ma_a{u},x2,'spline')];    
  n_soleus_ma_a     = [n_soleus_ma_a; interp1(x1,soleus_ma_a{u},x2,'spline')];
  n_tib_ant_ma_a    = [n_tib_ant_ma_a; interp1(x1,tib_ant_ma_a{u},x2,'spline')];
end;  
keyboard
% Remove outlier GC's
if plotit == 1
  in_r = ones(size(n_ankle_ang_r,1),1)*(0:(numpts-1));

  rseries_r1 = select_curves(in_r,n_ankle_ang_r,10,'Ankle Angle R','Percent GC','Angle');
  rseries_r2 = select_curves(in_r,n_ankle_mom_r,10,'Ankle Moment R','Percent GC','Moment');
  rseries_r3 = select_curves(in_r,n_knee_ang_r,10,'Knee Angle R','Percent GC','Angle');
  rseries_r4 = select_curves(in_r,n_knee_mom_r,10,'Knee Moment R','Percent GC','Moment');
  rseries_r5 = select_curves(in_r,n_hip_ang_r,10,'Hip Angle R','Percent GC','Angle');
  rseries_r6 = select_curves(in_r,n_hip_mom_r,10,'Hip Moment R','Percent GC','Moment');

  rseries_r  = cat(1,rseries_r1,rseries_r2,rseries_r3,rseries_r4,rseries_r5,rseries_r6);
  rseries_r  = unique(rseries_r) 

  in_l = ones(size(n_ankle_ang_l,1),1)*(0:(numpts-1));

  rseries_l1 = select_curves(in_l,n_ankle_ang_l,10,'Ankle Angle L','Percent GC','Angle');
  rseries_l2 = select_curves(in_l,n_ankle_mom_l,10,'Ankle Moment L','Percent GC','Moment');
  rseries_l3 = select_curves(in_l,n_knee_ang_l,10,'Knee Angle L','Percent GC','Angle');
  rseries_l4 = select_curves(in_l,n_knee_mom_l,10,'Knee Moment L','Percent GC','Moment');
  rseries_l5 = select_curves(in_l,n_hip_ang_l,10,'Hip Angle L','Percent GC','Angle');
  rseries_l6 = select_curves(in_l,n_hip_mom_l,10,'Hip Moment L','Percent GC','Moment');

  rseries_l  = cat(1,rseries_l1,rseries_l2,rseries_l3,rseries_l4,rseries_l5,rseries_l6);
  rseries_l  = unique(rseries_l) 
else
  if exist('rfile') == 1 && length(rfile) > 0
    eval(['load ~/Documents/MATLAB/data/' path1 '/processed/mot/mat/removed/' rfile '.mat;']);
    rseries = {};
  end;        
  if exist('rseries') == 1 && isempty(rseries) == 0    
    rseries_r = rseries{1}     
    rseries_l = rseries{2}  
  else
    rseries_r = input('Enter vector of R gait cycles to be removed:');
    rseries_l = input('Enter vector of L gait cycles to be removed:');
  end;
end;

close all;

if strcmp(EMGleg,'L') == 1
  rseries_emg   = rseries_l;
elseif strcmp(EMGleg,'R') == 1
  rseries_emg   = rseries_r;    
else
  fprintf('EMGleg not input correctly.');
end;

endstance_r(rseries_r)    = []; endstance_l(rseries_l)    = [];

ankle_ang_r(rseries_r)    = []; ankle_ang_l(rseries_l)    = [];
knee_ang_r(rseries_r)     = []; knee_ang_l(rseries_l)     = [];
hip_ang_r(rseries_r)      = []; hip_ang_l(rseries_l)      = [];
ankle_mom_r(rseries_r)    = []; ankle_mom_l(rseries_l)    = []; 
knee_mom_r(rseries_r)     = []; knee_mom_l(rseries_l)     = [];
hip_mom_r(rseries_r)      = []; hip_mom_l(rseries_l)      = [];
ankle_vel_r(rseries_r)    = []; ankle_vel_l(rseries_l)    = []; 
knee_vel_r(rseries_r)     = []; knee_vel_l(rseries_l)     = [];
hip_vel_r(rseries_r)      = []; hip_vel_l(rseries_l)      = [];
ankle_work_r(rseries_r)   = []; ankle_work_l(rseries_l)   = []; 
knee_work_r(rseries_r)    = []; knee_work_l(rseries_l)    = [];
hip_work_r(rseries_r)     = []; hip_work_l(rseries_l)     = [];
ankle_pow_r(rseries_r)    = []; ankle_pow_l(rseries_l)    = []; 
knee_pow_r(rseries_r)     = []; knee_pow_l(rseries_l)     = [];
hip_pow_r(rseries_r)      = []; hip_pow_l(rseries_l)      = [];

cop_l_x(rseries_l)  = [];  cop_l_y(rseries_l)  = [];  cop_l_z(rseries_l)  = [];
grf_l_x(rseries_l)  = [];  grf_l_y(rseries_l)  = [];  grf_l_z(rseries_l)  = [];
gtor_l_x(rseries_l) = [];  gtor_l_y(rseries_l) = [];  gtor_l_z(rseries_l) = [];
cop_r_x(rseries_r)  = [];  cop_r_y(rseries_r)  = [];  cop_r_z(rseries_r)  = [];
grf_r_x(rseries_r)  = [];  grf_r_y(rseries_r)  = [];  grf_r_z(rseries_r)  = [];
gtor_r_x(rseries_r) = [];  gtor_r_y(rseries_r) = [];  gtor_r_z(rseries_r) = [];
com_l_x(rseries_l)  = [];  com_l_y(rseries_l)  = [];  com_l_z(rseries_l)  = [];
com_r_x(rseries_r)  = [];  com_r_y(rseries_r)  = [];  com_r_z(rseries_r)  = [];

add_long_len(rseries_emg)    = []; add_mag1_len(rseries_emg)    = [];
add_mag2_len(rseries_emg)    = []; add_mag3_len(rseries_emg)    = [];
glut_med1_len(rseries_emg)   = []; glut_med2_len(rseries_emg)   = [];
glut_med3_len(rseries_emg)   = []; glut_max1_len(rseries_emg)   = []; 
glut_max2_len(rseries_emg)   = []; glut_max3_len(rseries_emg)   = [];
iliacus_len(rseries_emg)     = []; psoas_len(rseries_emg)       = [];
semimem_len(rseries_emg)     = []; semiten_len(rseries_emg)     = [];
bi_fem_lh_len(rseries_emg)   = []; rectus_fem_len(rseries_emg)  = []; 
bi_fem_sh_len(rseries_emg)   = []; vas_lat_len(rseries_emg)     = [];
vas_med_len(rseries_emg)     = []; vas_int_len(rseries_emg)     = [];
gas_med_len(rseries_emg)     = []; gas_lat_len(rseries_emg)     = []; 
soleus_len(rseries_emg)      = []; tib_ant_len(rseries_emg)     = [];

add_long_ma_h(rseries_emg)   = []; add_mag1_ma_h(rseries_emg)   = [];
add_mag2_ma_h(rseries_emg)   = []; add_mag3_ma_h(rseries_emg)   = [];
glut_med1_ma_h(rseries_emg)  = []; glut_med2_ma_h(rseries_emg)  = [];
glut_med3_ma_h(rseries_emg)  = []; glut_max1_ma_h(rseries_emg)  = [];
glut_max2_ma_h(rseries_emg)  = []; glut_max3_ma_h(rseries_emg)  = [];
iliacus_ma_h(rseries_emg)    = []; psoas_ma_h(rseries_emg)      = [];
semimem_ma_h(rseries_emg)    = []; semimem_ma_k(rseries_emg)    = [];
semiten_ma_h(rseries_emg)    = []; semiten_ma_k(rseries_emg)    = [];
bi_fem_lh_ma_h(rseries_emg)  = []; bi_fem_lh_ma_k(rseries_emg)  = [];
rectus_fem_ma_h(rseries_emg) = []; rectus_fem_ma_k(rseries_emg) = [];
bi_fem_sh_ma_k(rseries_emg)  = []; vas_lat_ma_k(rseries_emg)    = []; 
vas_med_ma_k(rseries_emg)    = []; vas_int_ma_k(rseries_emg)    = []; 
gas_med_ma_k(rseries_emg)    = []; gas_med_ma_a(rseries_emg)    = []; 
gas_lat_ma_k(rseries_emg)    = []; gas_lat_ma_a(rseries_emg)    = []; 
soleus_ma_a(rseries_emg)     = []; tib_ant_ma_a(rseries_emg)    = [];

n_ankle_ang_r(rseries_r,:)    = []; n_ankle_ang_l(rseries_l,:)    = [];
n_knee_ang_r(rseries_r,:)     = []; n_knee_ang_l(rseries_l,:)     = [];
n_hip_ang_r(rseries_r,:)      = []; n_hip_ang_l(rseries_l,:)      = [];
n_ankle_mom_r(rseries_r,:)    = []; n_ankle_mom_l(rseries_l,:)    = []; 
n_knee_mom_r(rseries_r,:)     = []; n_knee_mom_l(rseries_l,:)     = [];
n_hip_mom_r(rseries_r,:)      = []; n_hip_mom_l(rseries_l,:)      = [];
n_ankle_vel_r(rseries_r,:)    = []; n_ankle_vel_l(rseries_l,:)    = []; 
n_knee_vel_r(rseries_r,:)     = []; n_knee_vel_l(rseries_l,:)     = [];
n_hip_vel_r(rseries_r,:)      = []; n_hip_vel_l(rseries_l,:)      = [];
n_ankle_work_r(rseries_r,:)   = []; n_ankle_work_l(rseries_l,:)   = []; 
n_knee_work_r(rseries_r,:)    = []; n_knee_work_l(rseries_l,:)    = [];
n_hip_work_r(rseries_r,:)     = []; n_hip_work_l(rseries_l,:)     = [];
n_ankle_pow_r(rseries_r,:)    = []; n_ankle_pow_l(rseries_l,:)    = []; 
n_knee_pow_r(rseries_r,:)     = []; n_knee_pow_l(rseries_l,:)     = [];
n_hip_pow_r(rseries_r,:)      = []; n_hip_pow_l(rseries_l,:)      = [];

n_cop_l_x(rseries_l,:)  = [];  n_cop_l_y(rseries_l,:)  = [];  n_cop_l_z(rseries_l,:)  = [];
n_grf_l_x(rseries_l,:)  = [];  n_grf_l_y(rseries_l,:)  = [];  n_grf_l_z(rseries_l,:)  = [];
n_gtor_l_x(rseries_l,:) = [];  n_gtor_l_y(rseries_l,:) = [];  n_gtor_l_z(rseries_l,:) = [];
n_cop_r_x(rseries_r,:)  = [];  n_cop_r_y(rseries_r,:)  = [];  n_cop_r_z(rseries_r,:)  = [];
n_grf_r_x(rseries_r,:)  = [];  n_grf_r_y(rseries_r,:)  = [];  n_grf_r_z(rseries_r,:)  = [];
n_gtor_r_x(rseries_r,:) = [];  n_gtor_r_y(rseries_r,:) = [];  n_gtor_r_z(rseries_r,:) = [];
n_com_l_x(rseries_l,:)  = [];  n_com_l_y(rseries_l,:)  = [];  n_com_l_z(rseries_l,:)  = [];
n_com_r_x(rseries_r,:)  = [];  n_com_r_y(rseries_r,:)  = [];  n_com_r_z(rseries_r,:)  = [];

n_add_long_len(rseries_emg,:)    = []; n_add_mag1_len(rseries_emg,:)    = [];
n_add_mag2_len(rseries_emg,:)    = []; n_add_mag3_len(rseries_emg,:)    = [];
n_glut_med1_len(rseries_emg,:)   = []; n_glut_med2_len(rseries_emg,:)   = [];
n_glut_med3_len(rseries_emg,:)   = []; n_glut_max1_len(rseries_emg,:)   = []; 
n_glut_max2_len(rseries_emg,:)   = []; n_glut_max3_len(rseries_emg,:)   = [];
n_iliacus_len(rseries_emg,:)     = []; n_psoas_len(rseries_emg,:)       = [];
n_semimem_len(rseries_emg,:)     = []; n_semiten_len(rseries_emg,:)     = [];
n_bi_fem_lh_len(rseries_emg,:)   = []; n_rectus_fem_len(rseries_emg,:)  = []; 
n_bi_fem_sh_len(rseries_emg,:)   = []; n_vas_lat_len(rseries_emg,:)     = [];
n_vas_med_len(rseries_emg,:)     = []; n_vas_int_len(rseries_emg,:)     = [];
n_gas_med_len(rseries_emg,:)     = []; n_gas_lat_len(rseries_emg,:)     = []; 
n_soleus_len(rseries_emg,:)      = []; n_tib_ant_len(rseries_emg,:)     = [];

n_add_long_ma_h(rseries_emg,:)   = []; n_add_mag1_ma_h(rseries_emg,:)   = [];
n_add_mag2_ma_h(rseries_emg,:)   = []; n_add_mag3_ma_h(rseries_emg,:)   = [];
n_glut_med1_ma_h(rseries_emg,:)  = []; n_glut_med2_ma_h(rseries_emg,:)  = [];
n_glut_med3_ma_h(rseries_emg,:)  = []; n_glut_max1_ma_h(rseries_emg,:)  = [];
n_glut_max2_ma_h(rseries_emg,:)  = []; n_glut_max3_ma_h(rseries_emg,:)  = [];
n_iliacus_ma_h(rseries_emg,:)    = []; n_psoas_ma_h(rseries_emg,:)      = [];
n_semimem_ma_h(rseries_emg,:)    = []; n_semimem_ma_k(rseries_emg,:)    = [];
n_semiten_ma_h(rseries_emg,:)    = []; n_semiten_ma_k(rseries_emg,:)    = [];
n_bi_fem_lh_ma_h(rseries_emg,:)  = []; n_bi_fem_lh_ma_k(rseries_emg,:)  = [];
n_rectus_fem_ma_h(rseries_emg,:) = []; n_rectus_fem_ma_k(rseries_emg,:) = [];
n_bi_fem_sh_ma_k(rseries_emg,:)  = []; n_vas_lat_ma_k(rseries_emg,:)    = []; 
n_vas_med_ma_k(rseries_emg,:)    = []; n_vas_int_ma_k(rseries_emg,:)    = []; 
n_gas_med_ma_k(rseries_emg,:)    = []; n_gas_med_ma_a(rseries_emg,:)    = []; 
n_gas_lat_ma_k(rseries_emg,:)    = []; n_gas_lat_ma_a(rseries_emg,:)    = []; 
n_soleus_ma_a(rseries_emg,:)     = []; n_tib_ant_ma_a(rseries_emg,:)    = [];

if plotit == 1 || plotit == 2
  for ppp = 1:length(ankle_mom_r)
    figure(3)
    subplot(2,3,1)
    plot(n_ankle_ang_r(ppp,:));
    hold on;
    subplot(2,3,4)
    plot(n_ankle_mom_r(ppp,:));
    hold on;
    subplot(2,3,2)
    plot(n_knee_ang_r(ppp,:));
    hold on;
    subplot(2,3,5)
    plot(n_knee_mom_r(ppp,:));
    hold on;
    subplot(2,3,3)
    plot(n_hip_ang_r(ppp,:));
    hold on;
    subplot(2,3,6)
    plot(n_hip_mom_r(ppp,:));
    hold on;
  end;
  subplot(2,3,1);
  title('Ankle Ang R');
  grid on; axis tight;
  subplot(2,3,4);
  title('Ankle Mom R');
  grid on; axis tight;
  subplot(2,3,2);
  title('Knee Ang R');
  grid on; axis tight;
  subplot(2,3,5);
  title('Knee Mom R');
  grid on; axis tight;
  subplot(2,3,3);
  title('Hip Ang R');
  grid on; axis tight;
  subplot(2,3,6);
  title('Hip Mom R'); 
  grid on; axis tight;
  for qqq = 1:length(ankle_mom_l)
    figure(4)
    subplot(2,3,1)
    plot(n_ankle_ang_l(qqq,:));
    hold on;
    subplot(2,3,4)
    plot(n_ankle_mom_l(qqq,:));
    hold on;
    subplot(2,3,2)
    plot(n_knee_ang_l(qqq,:));
    hold on;
    subplot(2,3,5)
    plot(n_knee_mom_l(qqq,:));
    hold on;
    subplot(2,3,3)
    plot(n_hip_ang_l(qqq,:));
    hold on;
    subplot(2,3,6)
    plot(n_hip_mom_l(qqq,:));
    hold on;
  end;
  subplot(2,3,1);
  title('Ankle Ang L');
  grid on; axis tight;
  subplot(2,3,4);
  title('Ankle Mom L');
  grid on; axis tight;
  subplot(2,3,2);
  title('Knee Ang L');
  grid on; axis tight;
  subplot(2,3,5);
  title('Knee Mom L');
  grid on; axis tight;
  subplot(2,3,3);
  title('Hip Ang L');
  grid on; axis tight;
  subplot(2,3,6);
  title('Hip Mom L');
  grid on; axis tight;
end;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Create training and testing sets:
%train_test;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

m_ankle_ang_r  = mean(n_ankle_ang_r,1);  m_ankle_ang_l  = mean(n_ankle_ang_l,1);
m_ankle_mom_r  = mean(n_ankle_mom_r,1);  m_ankle_mom_l  = mean(n_ankle_mom_l,1);
m_ankle_vel_r  = mean(n_ankle_vel_r,1);  m_ankle_vel_l  = mean(n_ankle_vel_l,1);
m_ankle_work_r = mean(n_ankle_work_r,1); m_ankle_work_l = mean(n_ankle_work_l,1);
m_ankle_pow_r  = mean(n_ankle_pow_r,1);  m_ankle_pow_l  = mean(n_ankle_pow_l,1);
m_knee_ang_r   = mean(n_knee_ang_r,1);   m_knee_ang_l   = mean(n_knee_ang_l,1);
m_knee_mom_r   = mean(n_knee_mom_r,1);   m_knee_mom_l   = mean(n_knee_mom_l,1);
m_knee_vel_r   = mean(n_knee_vel_r,1);   m_knee_vel_l   = mean(n_knee_vel_l,1);
m_knee_work_r  = mean(n_knee_work_r,1);  m_knee_work_l  = mean(n_knee_work_l,1);
m_knee_pow_r   = mean(n_knee_pow_r,1);   m_knee_pow_l   = mean(n_knee_pow_l,1);
m_hip_ang_r    = mean(n_hip_ang_r,1);    m_hip_ang_l    = mean(n_hip_ang_l,1);
m_hip_mom_r    = mean(n_hip_mom_r,1);    m_hip_mom_l    = mean(n_hip_mom_l,1);
m_hip_vel_r    = mean(n_hip_vel_r,1);    m_hip_vel_l    = mean(n_hip_vel_l,1);
m_hip_work_r   = mean(n_hip_work_r,1);   m_hip_work_l   = mean(n_hip_work_l,1);
m_hip_pow_r    = mean(n_hip_pow_r,1);    m_hip_pow_l    = mean(n_hip_pow_l,1);

m_cop_l_x = mean(n_cop_l_x,1);   m_cop_l_y = mean(n_cop_l_y,1);    m_cop_l_z = mean(n_cop_l_z,1); 
m_grf_l_x = mean(n_grf_l_x,1);   m_grf_l_y = mean(n_grf_l_y,1);    m_grf_l_z = mean(n_grf_l_z,1); 
m_gtor_l_x = mean(n_gtor_l_x,1); m_gtor_l_y  = mean(n_gtor_l_y,1); m_gtor_l_z = mean(n_gtor_l_z,1);
m_cop_r_x = mean(n_cop_r_x,1);   m_cop_r_y = mean(n_cop_r_y,1);    m_cop_r_z = mean(n_cop_r_z,1); 
m_grf_r_x = mean(n_grf_r_x,1);   m_grf_r_y = mean(n_grf_r_y,1);    m_grf_r_z = mean(n_grf_r_z,1); 
m_gtor_r_x = mean(n_gtor_r_x,1); m_gtor_r_y = mean(n_gtor_r_y,1);  m_gtor_r_z = mean(n_gtor_r_z,1);
m_com_l_x = mean(n_com_l_x,1);   m_com_l_y = mean(n_com_l_y,1);    m_com_l_z = mean(n_com_l_z,1); 
m_com_r_x = mean(n_com_r_x,1);   m_com_r_y = mean(n_com_r_y,1);    m_com_r_z = mean(n_com_r_z,1); 

m_add_long_len = mean(n_add_long_len,1);   m_add_mag1_len = mean(n_add_mag1_len,1);   
m_add_mag2_len = mean(n_add_mag2_len,1);   m_add_mag3_len = mean(n_add_mag3_len,1);   
m_glut_med1_len = mean(n_glut_med1_len,1); m_glut_med2_len = mean(n_glut_med2_len,1);  
m_glut_med3_len = mean(n_glut_med3_len,1); m_glut_max1_len = mean(n_glut_max1_len,1);   
m_glut_max2_len = mean(n_glut_max2_len,1); m_glut_max3_len = mean(n_glut_max3_len,1);  
m_iliacus_len = mean(n_iliacus_len,1);     m_psoas_len = mean(n_psoas_len,1);      
m_semimem_len = mean(n_semimem_len,1);     m_semiten_len = mean(n_semiten_len,1);    
m_bi_fem_lh_len = mean(n_bi_fem_lh_len,1); m_rectus_fem_len = mean(n_rectus_fem_len,1);  
m_bi_fem_sh_len = mean(n_bi_fem_sh_len,1); m_vas_lat_len = mean(n_vas_lat_len,1);    
m_vas_med_len = mean(n_vas_med_len,1);     m_vas_int_len = mean(n_vas_int_len,1);    
m_gas_med_len = mean(n_gas_med_len,1);     m_gas_lat_len = mean(n_gas_lat_len,1);     
m_soleus_len = mean(n_soleus_len,1);       m_tib_ant_len = mean(n_tib_ant_len,1);    

m_add_long_ma_h = mean(n_add_long_ma_h,1);     m_add_mag1_ma_h = mean(n_add_mag1_ma_h,1);  
m_add_mag2_ma_h = mean(n_add_mag2_ma_h,1);     m_add_mag3_ma_h = mean(n_add_mag3_ma_h,1);  
m_glut_med1_ma_h = mean(n_glut_med1_ma_h,1);   m_glut_med2_ma_h = mean(n_glut_med2_ma_h,1); 
m_glut_med3_ma_h = mean(n_glut_med3_ma_h,1);   m_glut_max1_ma_h = mean(n_glut_max1_ma_h,1); 
m_glut_max2_ma_h = mean(n_glut_max2_ma_h,1);   m_glut_max3_ma_h = mean(n_glut_max3_ma_h,1); 
m_iliacus_ma_h = mean(n_iliacus_ma_h,1);       m_psoas_ma_h = mean(n_psoas_ma_h,1);     
m_semimem_ma_h = mean(n_semimem_ma_h,1);       m_semimem_ma_k = mean(n_semimem_ma_k,1);   
m_semiten_ma_h = mean(n_semiten_ma_h,1);       m_semiten_ma_k = mean(n_semiten_ma_k,1);   
m_bi_fem_lh_ma_h = mean(n_bi_fem_lh_ma_h,1);   m_bi_fem_lh_ma_k = mean(n_bi_fem_lh_ma_k,1); 
m_rectus_fem_ma_h = mean(n_rectus_fem_ma_h,1); m_rectus_fem_ma_k = mean(n_rectus_fem_ma_k,1);
m_bi_fem_sh_ma_k = mean(n_bi_fem_sh_ma_k,1);   m_vas_lat_ma_k = mean(n_vas_lat_ma_k,1);    
m_vas_med_ma_k = mean(n_vas_med_ma_k,1);       m_vas_int_ma_k = mean(n_vas_int_ma_k,1);    
m_gas_med_ma_k = mean(n_gas_med_ma_k,1);       m_gas_med_ma_a = mean(n_gas_med_ma_a,1);    
m_gas_lat_ma_k = mean(n_gas_lat_ma_k,1);       m_gas_lat_ma_a = mean(n_gas_lat_ma_a,1);    
m_soleus_ma_a = mean(n_soleus_ma_a,1);         m_tib_ant_ma_a = mean(n_tib_ant_ma_a,1);   

sd_ankle_ang_r  = std(n_ankle_ang_r,0,1);  sd_ankle_ang_l  = std(n_ankle_ang_l,0,1);
sd_ankle_mom_r  = std(n_ankle_mom_r,0,1);  sd_ankle_mom_l  = std(n_ankle_mom_l,0,1);
sd_ankle_vel_r  = std(n_ankle_vel_r,0,1);  sd_ankle_vel_l  = std(n_ankle_vel_l,0,1);
sd_ankle_work_r = std(n_ankle_work_r,0,1); sd_ankle_work_l = std(n_ankle_work_l,0,1);
sd_ankle_pow_r  = std(n_ankle_pow_r,0,1);  sd_ankle_pow_l  = std(n_ankle_pow_l,0,1);
sd_knee_ang_r   = std(n_knee_ang_r,0,1);   sd_knee_ang_l   = std(n_knee_ang_l,0,1);
sd_knee_mom_r   = std(n_knee_mom_r,0,1);   sd_knee_mom_l   = std(n_knee_mom_l,0,1);
sd_knee_vel_r   = std(n_knee_vel_r,0,1);   sd_knee_vel_l   = std(n_knee_vel_l,0,1);
sd_knee_work_r  = std(n_knee_work_r,0,1);  sd_knee_work_l  = std(n_knee_work_l,0,1);
sd_knee_pow_r   = std(n_knee_pow_r,0,1);   sd_knee_pow_l   = std(n_knee_pow_l,0,1);
sd_hip_ang_r    = std(n_hip_ang_r,0,1);    sd_hip_ang_l    = std(n_hip_ang_l,0,1);
sd_hip_mom_r    = std(n_hip_mom_r,0,1);    sd_hip_mom_l    = std(n_hip_mom_l,0,1);
sd_hip_vel_r    = std(n_hip_vel_r,0,1);    sd_hip_vel_l    = std(n_hip_vel_l,0,1);
sd_hip_work_r   = std(n_hip_work_r,0,1);   sd_hip_work_l   = std(n_hip_work_l,0,1);
sd_hip_pow_r    = std(n_hip_pow_r,0,1);    sd_hip_pow_l    = std(n_hip_pow_l,0,1);

sd_cop_l_x = std(n_cop_l_x,0,1);   sd_cop_l_y = std(n_cop_l_y,0,1);    sd_cop_l_z = std(n_cop_l_z,0,1); 
sd_grf_l_x = std(n_grf_l_x,0,1);   sd_grf_l_y = std(n_grf_l_y,0,1);    sd_grf_l_z = std(n_grf_l_z,0,1); 
sd_gtor_l_x = std(n_gtor_l_x,0,1); sd_gtor_l_y  = std(n_gtor_l_y,0,1); sd_gtor_l_z = std(n_gtor_l_z,0,1);
sd_cop_r_x = std(n_cop_r_x,0,1);   sd_cop_r_y = std(n_cop_r_y,0,1);    sd_cop_r_z = std(n_cop_r_z,0,1); 
sd_grf_r_x = std(n_grf_r_x,0,1);   sd_grf_r_y = std(n_grf_r_y,0,1);    sd_grf_r_z = std(n_grf_r_z,0,1); 
sd_gtor_r_x = std(n_gtor_r_x,0,1); sd_gtor_r_y = std(n_gtor_r_y,0,1);  sd_gtor_r_z = std(n_gtor_r_z,0,1);
sd_com_l_x = std(n_com_l_x,0,1);   sd_com_l_y = std(n_com_l_y,0,1);    sd_com_l_z = std(n_com_l_z,0,1); 
sd_com_r_x = std(n_com_r_x,0,1);   sd_com_r_y = std(n_com_r_y,0,1);    sd_com_r_z = std(n_com_r_z,0,1); 

sd_add_long_len = std(n_add_long_len,0,1);   sd_add_mag1_len = std(n_add_mag1_len,0,1);   
sd_add_mag2_len = std(n_add_mag2_len,0,1);   sd_add_mag3_len = std(n_add_mag3_len,0,1);   
sd_glut_med1_len = std(n_glut_med1_len,0,1); sd_glut_med2_len = std(n_glut_med2_len,0,1);  
sd_glut_med3_len = std(n_glut_med3_len,0,1); sd_glut_max1_len = std(n_glut_max1_len,0,1);   
sd_glut_max2_len = std(n_glut_max2_len,0,1); sd_glut_max3_len = std(n_glut_max3_len,0,1);  
sd_iliacus_len = std(n_iliacus_len,0,1);     sd_psoas_len = std(n_psoas_len,0,1);      
sd_semimem_len = std(n_semimem_len,0,1);     sd_semiten_len = std(n_semiten_len,0,1);    
sd_bi_fem_lh_len = std(n_bi_fem_lh_len,0,1); sd_rectus_fem_len = std(n_rectus_fem_len,0,1);  
sd_bi_fem_sh_len = std(n_bi_fem_sh_len,0,1); sd_vas_lat_len = std(n_vas_lat_len,0,1);    
sd_vas_med_len = std(n_vas_med_len,0,1);     sd_vas_int_len = std(n_vas_int_len,0,1);    
sd_gas_med_len = std(n_gas_med_len,0,1);     sd_gas_lat_len = std(n_gas_lat_len,0,1);     
sd_soleus_len = std(n_soleus_len,0,1);       sd_tib_ant_len = std(n_tib_ant_len,0,1);    

sd_add_long_ma_h = std(n_add_long_ma_h,0,1);     sd_add_mag1_ma_h = std(n_add_mag1_ma_h,0,1);  
sd_add_mag2_ma_h = std(n_add_mag2_ma_h,0,1);     sd_add_mag3_ma_h = std(n_add_mag3_ma_h,0,1);  
sd_glut_med1_ma_h = std(n_glut_med1_ma_h,0,1);   sd_glut_med2_ma_h = std(n_glut_med2_ma_h,0,1); 
sd_glut_med3_ma_h = std(n_glut_med3_ma_h,0,1);   sd_glut_max1_ma_h = std(n_glut_max1_ma_h,0,1); 
sd_glut_max2_ma_h = std(n_glut_max2_ma_h,0,1);   sd_glut_max3_ma_h = std(n_glut_max3_ma_h,0,1); 
sd_iliacus_ma_h = std(n_iliacus_ma_h,0,1);       sd_psoas_ma_h = std(n_psoas_ma_h,0,1);     
sd_semimem_ma_h = std(n_semimem_ma_h,0,1);       sd_semimem_ma_k = std(n_semimem_ma_k,0,1);   
sd_semiten_ma_h = std(n_semiten_ma_h,0,1);       sd_semiten_ma_k = std(n_semiten_ma_k,0,1);   
sd_bi_fem_lh_ma_h = std(n_bi_fem_lh_ma_h,0,1);   sd_bi_fem_lh_ma_k = std(n_bi_fem_lh_ma_k,0,1); 
sd_rectus_fem_ma_h = std(n_rectus_fem_ma_h,0,1); sd_rectus_fem_ma_k = std(n_rectus_fem_ma_k,0,1);
sd_bi_fem_sh_ma_k = std(n_bi_fem_sh_ma_k,0,1);   sd_vas_lat_ma_k = std(n_vas_lat_ma_k,0,1);    
sd_vas_med_ma_k = std(n_vas_med_ma_k,0,1);       sd_vas_int_ma_k = std(n_vas_int_ma_k,0,1);    
sd_gas_med_ma_k = std(n_gas_med_ma_k,0,1);       sd_gas_med_ma_a = std(n_gas_med_ma_a,0,1);    
sd_gas_lat_ma_k = std(n_gas_lat_ma_k,0,1);       sd_gas_lat_ma_a = std(n_gas_lat_ma_a,0,1);    
sd_soleus_ma_a = std(n_soleus_ma_a,0,1);         sd_tib_ant_ma_a = std(n_tib_ant_ma_a,0,1);   

tot_len_l = 0;
tot_len_r = 0;

for ll = 1:length(ankle_ang_l)
  tot_len_l = tot_len_l + length(ankle_ang_l{ll});
end;
for rr = 1:length(ankle_ang_r)
  tot_len_r = tot_len_r + length(ankle_ang_r{rr});
end;

m_stridetime_l = tot_len_l/length(ankle_ang_l)/Fs;
m_stridetime_r = tot_len_r/length(ankle_ang_r)/Fs;

if (plotit == 1) || (plotit == 2)
  figure(5)
  %hold on;
  subplot(2,3,1)
  niceBars(0:(numpts-1),m_ankle_ang_l,sd_ankle_ang_l,'r',0.5);
  hold on;
  niceBars(0:(numpts-1),m_ankle_ang_r,sd_ankle_ang_r,'b',0.5);
  grid on; axis tight;
  title('Ankle Angle','fontsize',12);
  xlabel('Percent GC','fontsize',12);
  ylabel('Dorsiflexion Angle (^\circ)','fontsize',12);
  %legend('Left','Right');
  %figure(6)
  subplot(2,3,4)
  niceBars(0:(numpts-1),-m_ankle_mom_l,sd_ankle_mom_l,'r',0.5);
  hold on;
  niceBars(0:(numpts-1),-m_ankle_mom_r,sd_ankle_mom_r,'b',0.5);
  grid on; axis tight;
  title('Ankle Moment','fontsize',12);
  xlabel('Percent GC','fontsize',12);
  ylabel('Plantarflexion Torque (Nm)','fontsize',12);
  %legend('Left','Right');
  %figure(7)
  subplot(2,3,2)
  niceBars(0:(numpts-1),m_knee_ang_l,sd_knee_ang_l,'r',0.5);
  hold on;
  niceBars(0:(numpts-1),m_knee_ang_r,sd_knee_ang_r,'b',0.5);
  grid on; axis tight;
  title('Knee Angle','fontsize',12);
  xlabel('Percent GC','fontsize',12);
  ylabel('Knee Flexion Angle (^\circ)','fontsize',12);
  %legend('Left','Right');
  %figure(8)
  subplot(2,3,5)
  niceBars(0:(numpts-1),m_knee_mom_l,sd_knee_mom_l,'r',0.5);
  hold on;
  niceBars(0:(numpts-1),m_knee_mom_r,sd_knee_mom_r,'b',0.5);
  grid on; axis tight;
  title('Knee Moment','fontsize',12);
  xlabel('Percent GC','fontsize',12);
  ylabel('Knee Flexion Torque (Nm)','fontsize',12);
  %legend('Left','Right');
  %figure(9)
  subplot(2,3,3)
  niceBars(0:(numpts-1),m_hip_ang_l,sd_hip_ang_l,'r',0.5);
  hold on;
  niceBars(0:(numpts-1),m_hip_ang_r,sd_hip_ang_r,'b',0.5);
  grid on; axis tight;
  title('Hip Angle','fontsize',12);
  xlabel('Percent GC','fontsize',12);
  ylabel('Hip Flexion Angle (^\circ)','fontsize',12);
  %legend('Left','Right','location','southeast');
  %figure(10)
  subplot(2,3,6)
  niceBars(0:(numpts-1),m_hip_mom_l,sd_hip_mom_l,'r',0.5);
  hold on;
  niceBars(0:(numpts-1),m_hip_mom_r,sd_hip_mom_r,'b',0.5);
  grid on; axis tight;
  title('Hip Moment','fontsize',12);
  xlabel('Percent GC','fontsize',12);
  ylabel('Hip Flexion Torque (Nm)','fontsize',12);
  %legend('Left','Right');
  if exist('save_plt') && save_plt==1
    save_dir = ['~/Documents/MATLAB/data/' path1 '/plots'];
    if exist(save_dir) == 0;
      eval(['mkdir ' save_dir]);
    end;
    eval(['print -dpng ''' save_dir '/' listfile '_' path1 '.png'';']);
  end;  
end;

path_out1 = ['~/Documents/MATLAB/data/' path1 '/processed/mot/mat/all'];
path_out2 = ['~/Documents/MATLAB/data/' path1 '/processed/mot/mat/mech'];
path_out3 = ['~/Documents/MATLAB/data/' path1 '/processed/mot/mat/removed'];

if exist(path_out1) == 0;
  eval(['mkdir ' path_out1]);
end;
if exist(path_out2) == 0;
  eval(['mkdir ' path_out2]);
end;
if exist(path_out3) == 0;
  eval(['mkdir ' path_out3]);
end;
  
outfile1  = [path_out1 '/' listfile '.mat'];
outfile2  = [path_out2 '/' listfile '.mat'];
outfile3  = [path_out3 '/' listfile '.mat'];

clear m;

% instead of saving list, gc, etc save rseries_l, rseries_r
save(outfile1,'n_*','m_*','sd_*','EMGleg','mass','leg_lengths');
save(outfile2,'endstance*','*vel*','*work*','*pow*','*ankle_ang*','*knee_ang*','*hip_ang*','*ankle_mom*','*knee_mom*','*hip_mom*','*cop*','*grf*','*gtor*','*com*','mass','leg_lengths');
save(outfile3,'rseries*','EMGleg');