function plot_subs(list,speeds,useleg,save_dir)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% plot_subs.m produces plots that compare the mechanical variables of
% different subjects during walking.  The plots include error bars
% and have the data for each requested subject on one plot.
%
% Inputs:
% list     - list of subjects
% speeds   - vector of requested speeds
% useleg   - leg for which to plot ('L','R', or 'EMG').
% save_dir - directory to save files (leave blank to not save)
%
% Outputs:
% Just plots.
% 
% Example Usage: plot_subs(['AM062411';'AD052611';'EM072611'],[125 150],'EMG','~/Desktop')
%
% Jared Markowitz
% Summer 2011
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%colors = ['b';'r';'k';'m';'g';'c';'y'];
colors = [0 0 1;1 0 0;0 .5 .2; .5 0 1; 1 .6 0; 0 .5 1; .1 .6 0;1 .9 0; 1 0 1;.4 .4 .4];
addpath('~/Dropbox/harvard/processMOT');
close all;

leg_cmd = 'legend(''';
for h = 1:size(list,1)
  if h < size(list,1)
    leg_cmd = [leg_cmd list(h,:) ''',''']; 
  else
    leg_cmd = [leg_cmd list(h,:) ''',''location'',''best'');'];
  end;  
end;

for i = 1:size(speeds,2)
  if speeds(i) == 75;
    numstr = '075';
  else
    numstr = num2str(speeds(i));
  end;
  
  for j= 1:size(list,1)
    path_full = ['~/Documents/MATLAB/data/' list(j,:) '/processed/mot/mat/all/'];
    cmd = ['load ' path_full 'walk' numstr '.mat'];
    eval(cmd);

    if exist('useleg')==0 || strcmp(useleg,'EMG')==1
      useleg1 = EMGleg;
    else
      useleg1 = useleg;
    end;
    
    cmd = ['m_ankle_ang = m_ankle_ang_' lower(useleg1) ';'];
    eval(cmd);
    cmd = ['m_ankle_mom = m_ankle_mom_' lower(useleg1) ';'];
    eval(cmd);
    cmd = ['m_knee_ang = m_knee_ang_' lower(useleg1) ';'];
    eval(cmd);
    cmd = ['m_knee_mom = m_knee_mom_' lower(useleg1) ';'];
    eval(cmd);
    cmd = ['m_hip_ang = m_hip_ang_' lower(useleg1) ';'];
    eval(cmd);
    cmd = ['m_hip_mom = m_hip_mom_' lower(useleg1) ';'];
    eval(cmd);
    cmd = ['sd_ankle_ang = sd_ankle_ang_' lower(useleg1) ';'];
    eval(cmd);
    cmd = ['sd_ankle_mom = sd_ankle_mom_' lower(useleg1) ';'];
    eval(cmd);
    cmd = ['sd_knee_ang = sd_knee_ang_' lower(useleg1) ';'];
    eval(cmd);
    cmd = ['sd_knee_mom = sd_knee_mom_' lower(useleg1) ';'];
    eval(cmd);
    cmd = ['sd_hip_ang = sd_hip_ang_' lower(useleg1) ';'];
    eval(cmd);
    cmd = ['sd_hip_mom = sd_hip_mom_' lower(useleg1) ';'];
    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,[' num2str(colors(mod(j,10)+1,:)) '],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,[' num2str(colors(mod(j,10)+1,:)) '],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,[' num2str(colors(mod(j,10)+1,:)) '],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,[' num2str(colors(mod(j,10)+1,:)) '],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,[' num2str(colors(mod(j,10)+1,:)) '],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,[' num2str(colors(mod(j,10)+1,:)) '],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_' 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_' 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_' 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 Flex. Torque (Nm)','fontsize',12);
  eval(leg_cmd);
  if exist('save_dir')
    save_cmd = ['print -dpng ''' save_dir '/knee_mom_' 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_' 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_' numstr '.png'';'];
    eval(save_cmd);
  end;
end;  