function hh = jhist(data,  varargin)
%hh = jhist(data,  varargin)
% data: matrix of values - do hist on all
% varargin :
%  name                value                              what
% -------              -------                   -----------------------
% histvals             10 groups                   centers of histogram 
% color                 'b'                         fill (or line) color
% filltype      'fill', 'hollow', 'tops'               bar type
% logscale              0                     plot number of pts on log10 scale
% linethick             1                          line thickness
%
% eg:
% hh = jhist(data, 'histvals', [-3:1:3], 'color', 'r')


cfg.histvals = [];
cfg.color = 'b';
cfg.filltype = 'fill';
cfg.logscale = 0;
cfg.linethick = 1;

cfg = fillstruct(cfg, varargin, 0);

if (isempty(cfg.histvals))
   valrange = max(data) - min(data);
   cfg.histvals = min(data) : valrange/9 : max(data);
end
[nn, xx] = hist(data(:), cfg.histvals);


if (cfg.logscale)
   yy = nn;
   yy(nn==0) = 10^(-1);
else
   yy = nn;
end


cfg.histvals = cfg.histvals(:);

if strcmp(cfg.filltype, 'fill')
   hh = bar(xx, yy, cfg.color); 
   set(hh, 'edgecolor', cfg.color, 'facecolor', cfg.color, ...
           'linewidth', cfg.linethick)
elseif (strcmp(cfg.filltype, 'hollow'))
   hh = bar(xx, yy, cfg.color); 
   set(hh, 'edgecolor', cfg.color, 'facecolor', 'none',...
           'linewidth', cfg.linethick)
elseif (strcmp(cfg.filltype, 'tops'))
   midx = mid(cfg.histvals);
   d1 = diff(cfg.histvals([1 2]))/2;   %dist from x1 to first midpoint
   dn = diff(cfg.histvals([end-1 end]))/2;   %dist from last midpt to last x
   x1 = cfg.histvals(1) - d1;
   xn = cfg.histvals(end) + dn;
   
   xtops = reshape([[x1;midx; xn] [x1;midx; xn]]', 2*(length(midx)+2),1);
   yy = [yy(:) yy(:)]';
   yy = [0; yy(:); 0];

   hh = plot(xtops,yy,'color', cfg.color', 'linewidth', cfg.linethick);
end


if (cfg.logscale)
   set(gca, 'yscale', 'log')
end


