% Function to numerically calculate segmented h values 
function h = hSeg(flag,hfun,dL,varargin)
  L=cumsum(dL);
  L=cat(find(size(L)>1),[0],L);
  if flag=='T'
    hL=L.*hfun(L);
    hL(find(L==0))=0;
    h=diff(hL)./diff(L);
  elseif flag=='H'
    Lh=L./hfun(L);
    Lh(find(L==0))=0;
    h=diff(L)./diff(Lh);
  else
    error('Unknown flag, ''T'' or ''H'' required');
  end
  if length(varargin)
    h=h(varargin{1});
  end
end
