% readelp() - read electrode position in .elp file
%
% Usage:
%   >> [eloc, elocnames, X, Y, Z] = readelp( filename );
%
% Inputs:
%   filename - name of the .elp file containing the electrode
%              localization
%
% Outputs:
%   eloc      - structure containing the name and localization of
%               the channels
%   elocnames - cell array with the name of the electrodes
%   X,Y,Z     - vectors containing the location only of the electrodes
%
% Author: Arnaud Delorme, CNL / Salk Institute, 28 Feb 2002
%
% See also: readloc(), snapread(), floatread()

%123456789012345678901234567890123456789012345678901234567890123456789012

% Copyright (C) Arnaud Delorme, CNL / Salk Institute, 28 Feb 2002
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

% $Log: readelp.m,v $
% Revision 1.1  2002/04/05 17:39:45  jorn
% Initial revision
%

function [eloc, names, x, y, z] = readelp( filename ); 

if nargin < 1
	help readelp;
	return;
end;

% open file
% ---------
fid = fopen(filename, 'r');
if fid == -1
  disp('Can not open file'); return;
end;
for index=1:12	fgetl(fid); end;

% scan file
% ---------
index = 1;
tmpstr = fgetl(fid);
noteof = 1;
while noteof
  if ~((tmpstr(1) == '/') & (tmpstr(2) == '/'))
    if (tmpstr(1) == '%') & (tmpstr(2) == 'N')
      tmpstr = deblank( tmpstr(end:-1:3) );
      eloc(index).labels = deblank(tmpstr(end:-1:1));
      tmpstr = fgetl(fid);
      tmp = sscanf(tmpstr, '%f');
      eloc(index).X  = tmp(1); x(index) = tmp(1);
      eloc(index).Y  = tmp(2); y(index) = tmp(2);
      eloc(index).Z  = tmp(3); z(index) = tmp(3);
      index = index + 1;
    end;
  end;
  tmpstr = fgetl(fid);
  if ~isstr(tmpstr) & tmpstr == -1
    noteof = 0;
  end;  
end;  

names = { eloc.labels };
