function [histmat, xout, yout, custmap] = hist2dplot3(data, xbins, ybins, xmin, xmax, ymin, ymax, xlab, ylab, zlab, tit)
%
% function [histmat, xout, yout, custmap] = hist2dplot3([xdat, ydat], xbins, ybins, xmin, xmax, ymin, ymax, xlabel, ylabel, zlabel, title)
%
%    HIST2DPLOT3 - Create and plot 2 dimensional histogram with additional
%    profile plots.
%
%    hist2dplot([xdat, ydat]) - plots a 2D histogram with 10 horizontal and
%    vertical bins and automatically calculates the horizontal and vertical
%    ranges based on the input dataset.  XDAT contains the X position and
%    YDAT the Y position for the data to be histogrammed.  X, Y and Z label
%    data for the plot must be added afterwards.
%
%    Profiles for X and Y data are also plotted, using the same colour map
%    as the main plot.
%
%    The syntax is the same as HIST2DPLOT (which is called by HIST2DPLOT3),
%    with the following extra input parameters:
%
%    hist2dplot3([xdat, ydat], xbins, ybins, xmin, xmax, ymin, ymax, xlabel, ylabel, zlabel, title)
%    - the variables 'xlabel', 'ylabel', 'zlabel' and 'title' specify the
%    axis labels to be used by each of the 3 plots.  Since the data is the
%    same, the axis titles can be shared between all 3 plots.

    if ( nargin > 11 )
        error('Too many input variables: syntax is hist2dplot3([xdat, ydat], xbins, ybins, xmin, xmax, ymin, ymax, xlabel, ylabel, zlabel, title)') ;
    elseif ( nargin == 4 ) | ( nargin == 5 ) | ( nargin == 6 )
        error('Must specify X and Y minimum and maximum for histogram: syntax is hist2dplot3([xdat, ydat], xbins, ybins, xmin, xmax, ymin, ymax)') ;
    elseif ( nargin == 2 )
        error('Must specify number of bins in X and Y: syntax is hist2dplot3([xdat, ydat], xbins, ybins)') ;
    elseif ( nargout > 4 )
        error('Too many output variables: syntax is [histdat, xout, yout, custmap] = hist2dplot3([xdat, ydat])') ;
    elseif ( nargout == 2 )
        error('Must specify both X and Y data: syntax is [histdat, xout, yout] = hist2dplot3([xdat, ydat])') ;
    end

    [nrow, ncol] = size(data) ;

    if ncol ~= 2
        error('Input data must specify X and Y data in two columns') ;
    end

    xdat = data(:,1) ; ydat = data(:,2) ;

    if ( nargin < 11 )
        xlab = [] ; ylab = [] ; zlab = [] ; tit = [] ;
    end
    if ( nargin < 7 )
        ymax = max(ydat) ;
    end
    if ( nargin < 6 )
        ymin = min(ydat) ;
    end
    if ( nargin < 5 )
        xmax = max(xdat) ;
    end
    if ( nargin < 4 )
        xmin = min(xdat) ; 
    end
    if ( nargin < 3 )
        xbins = 32 ; ybins = 32 ;
    end

    fmain = figure ;
    [histmat, xout, yout, custmap] = hist2dplot(data, xbins, ybins, xmin, xmax, ymin, ymax) ;
    figure(fmain), xlabel(xlab), ylabel(ylab), zlabel(zlab), title(tit) ;

    xsum = sum(histmat) ; ysum = sum(histmat') ;
    histmatx = histmat ; histmaty = histmat ;
    [nrows, ncols] = size(histmat) ;
    midrow = round(nrows/2) ; midcol = round(ncols/2) ;
    histmatx(midrow,:) = xsum ; histmaty(:,midcol) = ysum ;

    fx = figure ;
    surfc(xout,yout,histmatx), axis tight, shading interp ;
    colormap(jet) ; view(0,0) ;
    figure(fx), xlabel(xlab), ylabel(ylab), zlabel(zlab), title(tit) ;

    fy = figure ;
    surfc(xout,yout,histmaty), axis tight, shading interp ;
    colormap(jet) ; view(90,0) ;
    figure(fy), xlabel(xlab), ylabel(ylab), zlabel(zlab), title(tit) ;

    return
