function muscles = getMuscleData(infile,path1,EMGleg)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% getMuscleData reads the muscle parameters for a subject
% from a SIMM .msl file.
%
% Inputs:
% infile - SIMM .msl file (from RPose)
% path1  - directory containing infile (subject,date)
% EMGleg - leg with EMG electrodes (L or R)
% supp   - option to load supp hip muscles as well as original set. 
%
% Outputs:
% muscles - data structure with all queried muscle parameters.
%
% Jared Markowitz
% Spring 2011
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Load SIMM muscle file
fname = ['~/Documents/MATLAB/data/' path1 '/scaling/' infile '.msl'];
fid = fopen(fname,'r');
if fid < 0
   error(['Could not open ' fname ' for input']);
end;

EMGleg = lower(EMGleg);
nvars  = 0;
use    = 0;

% Get variables of interest

while (~feof(fid))
  buffer = fgetl(fid);
  if length(buffer)>13 && strcmp(buffer(13:end),['glut_max1_' EMGleg]) == 1
    suf = 'GLU1'; use = 1;
  elseif length(buffer)>13 && strcmp(buffer(13:end),['glut_max2_' EMGleg]) == 1
    suf = 'GLU2'; use = 1;
  elseif length(buffer)>13 && strcmp(buffer(13:end),['glut_max3_' EMGleg]) == 1
    suf = 'GLU3'; use = 1;
  elseif length(buffer)>13 && strcmp(buffer(13:end),['iliacus_' EMGleg]) == 1
    suf = 'ILCS'; use = 1;
  elseif length(buffer)>13 && strcmp(buffer(13:end),['psoas_' EMGleg]) == 1
    suf = 'PSO';  use = 1;
  elseif length(buffer)>13 && strcmp(buffer(13:end),['semimem_' EMGleg]) == 1
    suf = 'SMEM'; use = 1;
  elseif length(buffer)>13 && strcmp(buffer(13:end),['semiten_' EMGleg]) == 1
    suf = 'STEN'; use = 1;       
  elseif length(buffer)>13 && strcmp(buffer(13:end),['bi_fem_lh_' EMGleg]) == 1
    suf = 'BFLH'; use = 1;    
  elseif length(buffer)>13 && strcmp(buffer(13:end),['bi_fem_sh_' EMGleg]) == 1
    suf = 'BFSH'; use = 1;    
  elseif length(buffer)>13 && strcmp(buffer(13:end),['rectus_fem_' EMGleg]) == 1  
    suf = 'RF';   use = 1;    
  elseif length(buffer)>13 && strcmp(buffer(13:end),['vas_med_' EMGleg]) == 1
    suf = 'VMED'; use = 1;    
  elseif length(buffer)>13 && strcmp(buffer(13:end),['vas_int_' EMGleg]) == 1
    suf = 'VINT'; use = 1;    
  elseif length(buffer)>13 && strcmp(buffer(13:end),['vas_lat_' EMGleg]) == 1
    suf = 'VLAT'; use = 1;    
  elseif length(buffer)>13 && strcmp(buffer(13:end),['gas_med_' EMGleg]) == 1
    suf = 'MGAS'; use = 1;    
  elseif length(buffer)>13 && strcmp(buffer(13:end),['gas_lat_' EMGleg]) == 1
    suf = 'LGAS'; use = 1;    
  elseif length(buffer)>13 && strcmp(buffer(13:end),['soleus_' EMGleg]) == 1
    suf = 'SOL';  use = 1;    
  elseif length(buffer)>13 && strcmp(buffer(13:end),['tib_ant_' EMGleg]) == 1
    suf = 'TA';   use = 1;
  elseif length(buffer)>13 && strcmp(buffer(13:end),['glut_med1_' EMGleg]) == 1
    suf = 'GMED1'; use = 1;
  elseif length(buffer)>13 && strcmp(buffer(13:end),['glut_med2_' EMGleg]) == 1
    suf = 'GMED2'; use = 1;
  elseif length(buffer)>13 && strcmp(buffer(13:end),['glut_med3_' EMGleg]) == 1
    suf = 'GMED3'; use = 1;
  elseif length(buffer)>13 && strcmp(buffer(13:end),['add_long_' EMGleg]) == 1
    suf = 'ADDL'; use = 1;
  elseif length(buffer)>13 && strcmp(buffer(13:end),['add_mag1_' EMGleg]) == 1
    suf = 'ADDM1';  use = 1;
  elseif length(buffer)>13 && strcmp(buffer(13:end),['add_mag2_' EMGleg]) == 1
    suf = 'ADDM2'; use = 1;
  elseif length(buffer)>13 && strcmp(buffer(13:end),['add_mag3_' EMGleg]) == 1
    suf = 'ADDM3'; use = 1;       
  elseif length(buffer)>9 && strcmp(buffer(1:9),'max_force') == 1 && use==1  
    cmd = sprintf(['muscles.Fmax' suf ' = str2num(buffer(10:end));']);
    eval(cmd); nvars = nvars+1;
  elseif length(buffer)>20 && strcmp(buffer(1:20),'optimal_fiber_length') == 1 && use==1  
    cmd = sprintf(['muscles.lopt' suf ' = str2num(buffer(21:end));']);
    eval(cmd); nvars = nvars+1; 
  elseif length(buffer)>19 && strcmp(buffer(1:19),'tendon_slack_length') == 1 && use==1  
    cmd = sprintf(['muscles.lsl' suf ' = str2num(buffer(20:end));']);
    eval(cmd); nvars = nvars+1;
  elseif length(buffer)>15 && strcmp(buffer(1:15),'pennation_angle') == 1 && use==1  
    cmd = sprintf(['muscles.alpha' suf ' = str2num(buffer(16:end))*pi/180;']);
    eval(cmd);  nvars = nvars+1;
    use = 0;
  else  
  end;
  if nvars == 96
    break;
  end;
end; 

fclose(fid);