function y=getdigit(x,ix);
%y=getdigit(x,ix) get digits of x specified by ix
%
% y   integer output
% x   decimal number
% ix  array of digit location (default 1)
%
% example: getdigit( 123.456, [1 3 5] ) = 135

if nargin < 2, ix=1;  end
if min(ix) < 1, error('ix must contain positive integers'), end
if max(ix) > 16, error('ix>16 cannot be handled with double precision'), end

y=x;
in=find(x);

if length(in)>0
  
  x=abs(x(in))';
  
  % add small increment to avoid problems with powers of 10
  x=x+x/10^(max(ix)+2);
  e=.01;
  
  if length(ix)==1
    
    z=floor(x.*10.^(max(ix)-ceil(log10(x))));
    z=z-floor((z+e).*10.^(min(ix)-1-ceil(log10(z+e))))*10^length(ix);
    
  elseif max(diff(ix))==1
    
    z=floor(x.*10.^(max(ix)-ceil(log10(x))));
    z=z-floor((z+e).*10.^(min(ix)-1-ceil(log10(z+e))))*10^length(ix);
  
  else
    
    x=int2str(x.*10.^(max(ix)-floor(log10(x))));
    z=str2num(x(:,ix));
  
  end

  y(in)=z;
  
end
