function hh = scatter3(varargin)
%SCATTER3 3-D Scatter plot.
%   SCATTER3(X,Y,Z,S,C) displays colored circles at the locations
%   specified by the vectors X,Y,Z (which must all be the same size).  The
%   area of each marker is determined by the values in the vector S (in
%   points^2) and the colors of each marker are based on the values in C.  S
%   can be a scalar, in which case all the markers are drawn the same
%   size, or a vector the same length as X,Y, and Z.
%   
%   When C is a vector the same length as X,Y, and Z, the values in C
%   are linearly mapped to the colors in the current colormap.  
%   When C is a LENGTH(X)-by-3 matrix, the values in C specify the
%   colors of the markers as RGB values.  C can also be a color string.
%
%   SCATTER3(X,Y,Z) draws the markers with the default size and color.
%   SCATTER3(X,Y,Z,S) draws the markers with a single color.
%   SCATTER3(...,M) uses the marker M instead of 'o'.
%   SCATTER3(...,'filled') fills the markers.
%
%   H = SCATTER(...) returns handles the PATCHES created.
%
%   Use PLOT3 for single color, single marker size 3-D scatter plots.
%
%   Example
%      [x,y,z] = sphere(16);
%      X = [x(:)*.5 x(:)*.75 x(:)];
%      Y = [y(:)*.5 y(:)*.75 y(:)];
%      Z = [z(:)*.5 z(:)*.75 z(:)];
%      S = repmat([1 .75 .5]*10,prod(size(x)),1);
%      C = repmat([1 2 3],prod(size(x)),1);
%      scatter3(X(:),Y(:),Z(:),S(:),C(:),'filled'), view(-60,60)
%
%   See also SCATTER, PLOT3.

%   Copyright (c) 1984-98 by The MathWorks, Inc.
%   $Revision: 1.4 $ $Date: 1997/11/21 23:46:46 $

error(nargchk(3,7,nargin))
ax = newplot;
filled = 0;
scaled = 0;
marker = 'o';
c = '';

% Parse optional trailing arguments (in any order)
nin = nargin;
while nin > 0 & isstr(varargin{nin})
  if strcmp(varargin{nin},'filled'),
     filled = 1;
  else
     [l,ctmp,m,msg] = colstyle(varargin{nin});
     error(msg)
     if ~isempty(m), marker = m; end
     if ~isempty(ctmp), c = ctmp; end
  end
  nin = nin-1;
end
if isempty(marker), marker = 'o'; end
co = get(ax,'colororder');

switch nin
case 3  % scatter3(x,y,z)
  x = varargin{1};
  y = varargin{2};
  z = varargin{3};
  if isempty(c),
    c = co(1,:);
  end
  s = get(ax,'defaultlinemarkersize')^2;
case 4 % scatter3(x,y,z,s)
  x = varargin{1};
  y = varargin{2};
  z = varargin{3};
  s = varargin{4};
  if isempty(c),
    c = co(1,:);
  end
case 5  % scatter3(x,y,z,s,c)
  x = varargin{1};
  y = varargin{2};
  z = varargin{3};
  s = varargin{4};
  c = varargin{5};
otherwise
  error('Wrong number of input arguments.');
end

if any([length(x) length(y) length(z) ...
        prod(size(x)) prod(size(y)) prod(size(y))] ~= length(x))
  error('X,Y, and Z must be vectors of the same length.');
end

% Map colors into colormap colors if necessary.
if isstr(c) | isequal(size(c),[1 3]); % string color or scalar rgb
   color = repmat(c,length(x),1);
elseif length(c)==prod(size(c)), % is C a vector?
   scaled = 1;
elseif isequal(size(c),[length(x) 3]), % vector of rgb's
   color = c;
else
  error(['C must be a single color, a vector the same length as X, ',...
         'or an M-by-3 matrix.'])
end

% Scalar expand the marker size if necessary
if length(s)==1, 
  s = repmat(s,length(x),1); 
elseif length(s)~=prod(size(s)) | length(s)~=length(x)
  error('S must be a scalar or a vector the same length as X.')
end

% Now draw the plot, one patch per point.
h = [];
for i=1:length(x),
   h = [h;patch('xdata',x(i),'ydata',y(i),'zdata',z(i),...
         'linestyle','none','facecolor','none',...
         'markersize',sqrt(s(i)), ...
         'marker',marker)];
   if scaled,
      set(h(end),'cdata',c(i),'edgecolor','flat','markerfacecolor','flat');
   else
      set(h(end),'edgecolor',color(i,:),'markerfacecolor',color(i,:));
   end
   if ~filled,
      set(h(end),'markerfacecolor','none');
   end
end
if ~ishold, view(3), grid on, end

if nargout>0, hh = h; end
