function plot_train_test(path1,speeds,useleg,save_dir)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% plot_train_test.m produces plots that compare the mechanical variables
% of the training, testing, and full data sets for each subject.
%
% Inputs:
% path1    - subject ID
% speeds   - vector of requested speeds
% useleg   - leg to plot (typically one with EMG electrodes)
% save_dir - directory to save files (leave blank to not save)
%
% Outputs:
% Just plots.
% 
% Example Usage: plot_train_test('AM062411',[125 150],'L','~/Desktop')
%
% Jared Markowitz
% Summer 2011
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

path_train = ['~/Documents/MATLAB/data/' path1 '/processed/mot/mat/train/'];
path_test  = ['~/Documents/MATLAB/data/' path1 '/processed/mot/mat/test/'];
path_all   = ['~/Documents/MATLAB/data/' path1 '/processed/mot/mat/all/'];

colors = ['k';'b';'r'];

close all;

leg_cmd = ['legend(''all'',''train'',''test'',''location'',''best'')'];

for i = 1:length(speeds)
  if speeds(i) == 75;
    numstr = '075';
  else
    numstr = num2str(speeds(i));
  end;
    
  for j = 1:3
  
    if j==1
      cmd = ['load ' path_all 'walk' numstr '.mat'];    
    elseif j==2
      cmd = ['load ' path_train 'walk' numstr '.mat'];      
    else  
      cmd = ['load ' path_test 'walk' numstr '.mat'];      
    end;
      
    eval(cmd);

    if exist('useleg')==0
      useleg = EMGleg;
    end;

    cmd = ['m_ankle_ang = m_ankle_ang_' lower(useleg) ';'];
    eval(cmd);
    cmd = ['m_ankle_mom = m_ankle_mom_' lower(useleg) ';'];
    eval(cmd);
    cmd = ['m_knee_ang = m_knee_ang_' lower(useleg) ';'];
    eval(cmd);
    cmd = ['m_knee_mom = m_knee_mom_' lower(useleg) ';'];
    eval(cmd);
    cmd = ['m_hip_ang = m_hip_ang_' lower(useleg) ';'];
    eval(cmd);
    cmd = ['m_hip_mom = m_hip_mom_' lower(useleg) ';'];
    eval(cmd);
    cmd = ['sd_ankle_ang = sd_ankle_ang_' lower(useleg) ';'];
    eval(cmd);
    cmd = ['sd_ankle_mom = sd_ankle_mom_' lower(useleg) ';'];
    eval(cmd);
    cmd = ['sd_knee_ang = sd_knee_ang_' lower(useleg) ';'];
    eval(cmd);
    cmd = ['sd_knee_mom = sd_knee_mom_' lower(useleg) ';'];
    eval(cmd);
    cmd = ['sd_hip_ang = sd_hip_ang_' lower(useleg) ';'];
    eval(cmd);
    cmd = ['sd_hip_mom = sd_hip_mom_' lower(useleg) ';'];
    eval(cmd);

    numpts = length(m_ankle_ang_r);
  
    cmd = ['figure(' num2str(i) num2str(1) ');'];
    eval(cmd);
    hold on;
    cmd = ['niceBars(0:(numpts-1),m_ankle_ang,sd_ankle_ang,''' colors(j) ''',0.5);'];
    eval(cmd);
    cmd = ['figure(' num2str(i) num2str(2) ');'];
    eval(cmd);    
    hold on;
    cmd = ['niceBars(0:(numpts-1),-m_ankle_mom,sd_ankle_mom,''' colors(j) ''',0.5);'];
    eval(cmd);
    cmd = ['figure(' num2str(i) num2str(3) ');'];
    eval(cmd);    
    hold on;
    cmd = ['niceBars(0:(numpts-1),m_knee_ang,sd_knee_ang,''' colors(j) ''',0.5);'];
    eval(cmd);
    cmd = ['figure(' num2str(i) num2str(4) ');'];
    eval(cmd);
    hold on;
    cmd = ['niceBars(0:(numpts-1),m_knee_mom,sd_knee_mom,''' colors(j) ''',0.5);'];
    eval(cmd);
    cmd = ['figure(' num2str(i) num2str(5) ');'];
    eval(cmd);
    hold on;
    cmd = ['niceBars(0:(numpts-1),m_hip_ang,sd_hip_ang,''' colors(j) ''',0.5);'];
    eval(cmd);
    cmd = ['figure(' num2str(i) num2str(6) ');'];
    eval(cmd);
    hold on;
    cmd = ['niceBars(0:(numpts-1),m_hip_mom,sd_hip_mom,''' colors(j) ''',0.5);'];
    eval(cmd);    
  end;
  
  cmd = ['figure(' num2str(i) num2str(1) ');'];
  eval(cmd);
  grid on; axis tight;
  title_cmd = ['title(''Ankle Angle ' numstr ''',''fontsize'',' num2str(12) ');'];
  eval(title_cmd);
  xlabel('Percent GC','fontsize',12);
  ylabel('Dorsiflexion (^\circ)','fontsize',12);
  eval(leg_cmd);
  if exist('save_dir')
    save_cmd = ['print -dpng ''' save_dir '/ank_ang_tt_' numstr '.png'';'];
    eval(save_cmd);
  end;
  cmd = ['figure(' num2str(i) num2str(2) ');'];
  eval(cmd);
  grid on; axis tight;
  title_cmd = ['title(''Ankle Moment ' numstr ''',''fontsize'',' num2str(12) ');'];
  eval(title_cmd);
  xlabel('Percent GC','fontsize',12);
  ylabel('Plantarflexion Torque (Nm)','fontsize',12);
  eval(leg_cmd);
  if exist('save_dir')
    save_cmd = ['print -dpng ''' save_dir '/ank_mom_tt_' numstr '.png'';'];
    eval(save_cmd);
  end;
  cmd = ['figure(' num2str(i) num2str(3) ');'];
  eval(cmd);
  grid on; axis tight;
  title_cmd = ['title(''Knee Angle ' numstr ''',''fontsize'',' num2str(12) ');'];
  eval(title_cmd);
  xlabel('Percent GC','fontsize',12);
  ylabel('Knee Flexion (^\circ)','fontsize',12);
  eval(leg_cmd);
  if exist('save_dir')
    save_cmd = ['print -dpng ''' save_dir '/knee_ang_tt_' numstr '.png'';'];
    eval(save_cmd);
  end;
  cmd = ['figure(' num2str(i) num2str(4) ');'];
  eval(cmd);
  grid on; axis tight;
  title_cmd = ['title(''Knee Moment ' numstr ''',''fontsize'',' num2str(12) ');'];
  eval(title_cmd);
  xlabel('Percent GC','fontsize',12);
  ylabel('Knee Ext. Torque (Nm)','fontsize',12);
  eval(leg_cmd);
  if exist('save_dir')
    save_cmd = ['print -dpng ''' save_dir '/knee_mom_tt_' numstr '.png'';'];
    eval(save_cmd);
  end;
  cmd = ['figure(' num2str(i) num2str(5) ');'];
  eval(cmd);
  grid on; axis tight;
  title_cmd = ['title(''Hip Angle ' numstr ''',''fontsize'',' num2str(12) ');'];
  eval(title_cmd);
  xlabel('Percent GC','fontsize',12);
  ylabel('Hip Flexion (^\circ)','fontsize',12);
  eval(leg_cmd);
  if exist('save_dir')
    save_cmd = ['print -dpng ''' save_dir '/hip_ang_tt_' numstr '.png'';'];
    eval(save_cmd);
  end;
  cmd = ['figure(' num2str(i) num2str(6) ');'];
  eval(cmd);
  grid on; axis tight;
  title_cmd = ['title(''Hip Moment ' numstr ''',''fontsize'',' num2str(12) ');'];
  eval(title_cmd);
  xlabel('Percent GC','fontsize',12);
  ylabel('Hip Flex. Torque (Nm)','fontsize',12);
  eval(leg_cmd);
  if exist('save_dir')
    save_cmd = ['print -dpng ''' save_dir '/hip_mom_tt_' numstr '.png'';'];
    eval(save_cmd);
  end;  
end;