function h=meshc(x,y,z,c)
%MESHC  Combination mesh/contour plot.
%   MESHC(...) is the same as MESH(...) except that a contour plot
%   is drawn beneath the mesh.
%
%   Because CONTOUR does not handle irregularly spaced data, this 
%   routine only works for surfaces defined on a rectangular grid.
%   The matrices or vectors X and Y define the axis limits only.
%
%   See also MESH.

%   Clay M. Thompson 4-10-91
%   Copyright (c) 1984-98 by The MathWorks, Inc.
%   $Revision: 5.4 $  $Date: 1997/11/21 23:46:27 $

error(nargchk(1,4,nargin));

if nargin==1,  % Generate x,y matrices for surface z.
  z = x;
  [m,n] = size(z);
  [x,y] = meshgrid(1:n,1:m);

elseif nargin==2,
  z = x; c = y;
  [m,n] = size(z);
  [x,y] = meshgrid(1:n,1:m);

end

if min(size(z))==1,
  error('The surface Z must contain more than one row or column.');
end

% Determine state of system
cax = newplot;
next = lower(get(cax,'NextPlot'));
hold_state = ishold;

% Plot mesh.
if nargin==2 | nargin==4,
  hm=mesh(x,y,z,c);
else
  hm=mesh(x,y,z);
end

hold on;

a = get(gca,'zlim');

zpos = a(1); % Always put contour below the plot.

% Get D contour data
[cc,hh] = contour3(x,y,z);

%%% size zpos to match the data

for i = 1:length(hh)
    zz = get(hh(i),'Zdata');
    set(hh(i),'Zdata',zpos*ones(size(zz)));
end

if ~hold_state, set(cax,'NextPlot',next); end
if nargout > 0
    h = [hm; hh(:)];
end
