function [C_e, C_d] = elec_distance_consec(dist_e, dist_d)

% elec_distance_consec - Identify distance between nearest neighbour electrodes
% 
% Requires input data generated by 'elec_distance.m'
% Assumes using FUSA CNL electrode positions (Scan ECI-Cap)
%

% $Revision: 1.1 $ $Date: 2009-04-28 22:13:54 $

% Licence:  GNU GPL, no implied or express warranties
% History:  05/2000, Darren.Weber_at_radiology.ucsf.edu
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

  fprintf('\n%s\n\n', 'Calculating consecutive electrode distances.');


  End_e = strvcat('3', '5', '7', '9', '13', '22', '30', '41', '49', '54', '59', '67', '78', '86', '97', '101', '108', '110', '115', '117', '120', '124');

  C_e = cell(0);
  C_d = [];


  for row = 1:length(dist_d)
     
     % Check that row in dist_d matrix is non-zero

     NonZeroColumns = find(dist_d(row,:));

     if ~isempty(NonZeroColumns)

	  % Calculate consecutive electrode distance,
	  % excluding end electrodes defined in End_e
		
		if( (row+1) <= length(dist_e) )
     
			e = str2num(char(dist_e(row,row+1)));
			
			if(strmatch(num2str(e(1)), End_e, 'exact'))
	
				% found end electrode, do nothing
			else

				C_d(end + 1) = dist_d(row,row+1);
                                C_e(end + 1) = {num2str(e(1))};
			end
		end
	end
  end
  clear r x;
