function plot_speeds(path1,speeds,useleg,save_dir)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% plot_speeds.m plots the average (with s.d.) of the mechanical
% variables at the requested speed for the requested subject.  Only
% variables on the requested leg (usually that with the EMG sensors)
% are plotted.
%
% Inputs:
% path1    - subject name
% speeds   - vector of desired speeds
% useleg   - leg for which to plot ('L' or 'R')
% save_dir - directory to save files (leave blank to not save)
%
% Outputs:
% Just plots
%
% Example Usage:
% plot_speeds('AM062411',[075 100 125 150 175 200],'L','~/Desktop')
%
% Jared Markowitz
% Summer 2011
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

path_full = ['~/Documents/MATLAB/data/' path1 '/processed/mot/mat/all/'];

colors = ['g';'c';'b';'k';'m';'r'];
%colors = ['y';'g';'b';'k';'m';'r'];

close all;

leg_cmd = 'legend(''';

for h = 1:length(speeds)
  numstr = [num2str(speeds(h)/100,'%1.2f') ' m/s'];
  if h < length(speeds)
    leg_cmd = [leg_cmd numstr ''',''']; 
  else
    leg_cmd = [leg_cmd numstr ''',''location'',''best'');'];
  end;  
end;

for i=1:length(speeds)
  if speeds(i) == 75;
    numstr = '075';
  else
    numstr = num2str(speeds(i));
  end;

  cmd = ['load ' path_full 'walk' numstr '.mat'];
  eval(cmd);

  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);
  
  figure(1)
  hold on;
  cmd = ['niceBars(0:(numpts-1),m_ankle_ang,sd_ankle_ang,''' colors(i) ''',0.5);'];
  eval(cmd);
  figure(2)
  hold on;
  cmd = ['niceBars(0:(numpts-1),-m_ankle_mom,sd_ankle_mom,''' colors(i) ''',0.5);'];
  eval(cmd);
  figure(3)
  hold on;
  cmd = ['niceBars(0:(numpts-1),m_knee_ang,sd_knee_ang,''' colors(i) ''',0.5);'];
  eval(cmd);
  figure(4)
  hold on;
  cmd = ['niceBars(0:(numpts-1),m_knee_mom,sd_knee_mom,''' colors(i) ''',0.5);'];
  eval(cmd);
  figure(5)
  hold on;
  cmd = ['niceBars(0:(numpts-1),m_hip_ang,sd_hip_ang,''' colors(i) ''',0.5);'];
  eval(cmd);
  figure(6)
  hold on;
  cmd = ['niceBars(0:(numpts-1),m_hip_mom,sd_hip_mom,''' colors(i) ''',0.5);'];
  eval(cmd);
  
end;

figure(1);
grid on; axis tight;
title_cmd = ['title(''Ankle Angle ' path1 ''',''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 '/ankle_ang_' path1 '.png'';'];
  eval(save_cmd);
end;
figure(2)
grid on; axis tight;
title_cmd = ['title(''Ankle Moment ' path1 ''',''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 '/ankle_mom_' path1 '.png'';'];
  eval(save_cmd);
end;
figure(3);
grid on; axis tight;
title_cmd = ['title(''Knee Angle ' path1 ''',''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_' path1 '.png'';'];
  eval(save_cmd);
end;
figure(4)
grid on; axis tight;
title_cmd = ['title(''Knee Moment ' path1 ''',''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_' path1 '.png'';'];
  eval(save_cmd);
end;
figure(5)
grid on; axis tight;
title_cmd = ['title(''Hip Angle ' path1 ''',''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_' path1 '.png'';'];
  eval(save_cmd);
end;
figure(6)
grid on; axis tight;
title_cmd = ['title(''Hip Moment ' path1 ''',''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_' path1 '.png'';'];
  eval(save_cmd);
end;
