function hout=streamline(varargin)
%STREAMLINE  Streamlines from 2D or 3D vector data.
%   H = STREAMLINE(X,Y,Z,U,V,W,STARTX,STARTY,STARTZ) creates streamlines
%   from 3D vector data U,V,W. The arrays X,Y,Z define the coordinates for
%   U,V,W and must be monotonic and 3D plaid (as if produced by MESHGRID). 
%   STARTX, STARTY, and STARTZ define the starting positions of the stream
%   lines. A vector of line handles is returned.
%   
%   H = STREAMLINE(U,V,W,STARTX,STARTY,STARTZ) assumes 
%         [X Y Z] = meshgrid(1:N, 1:M, 1:P) where [M,N,P]=SIZE(U). 
%   
%   H = STREAMLINE(XYZ) assumes XYZ is a precomputed cell array of vertex
%       arrays (as if produced by STREAM3).
%   
%   H = STREAMLINE(X,Y,U,V,STARTX,STARTY) creates streamlines from 2D
%   vector data U,V. The arrays X,Y define the coordinates for U,V and
%   must be monotonic and 2D plaid (as if produced by MESHGRID). STARTX
%   and STARTY define the starting positions of the streamlines. A vector
%   of line handles is returned.
%   
%   H = STREAMLINE(U,V,STARTX,STARTY) assumes 
%         [X Y] = meshgrid(1:N, 1:M) where [M,N]=SIZE(U). 
%   
%   H = STREAMLINE(XY) assumes XY is a precomputed cell array of vertex
%       arrays (as if produced by STREAM2).
%   
%   H = STREAMLINE(...,OPTIONS) specifies the options used in creating
%   the streamlines. OPTIONS is specified as a one or two element vector
%   containing the step size and maximum number of vertices in a stream
%   line.  If OPTIONS is not specified the default step size is 0.1 (one
%   tenth of a cell) and the default maximum number of vertices is
%   10000. OPTIONS can either be [stepsize] or [stepsize maxverts].
%   
%   Example:
%      load wind
%      [sx sy sz] = meshgrid(80, 20:10:50, 0:5:15);
%      h=streamline(x,y,z,u,v,w,sx,sy,sz);
%      set(h, 'Color', 'red');
%      view(3);
%
%   See also STREAM3, STREAM2, CONEPLOT, ISOSURFACE, SMOOTH3, SUBVOLUME,
%            REDUCEVOLUME.

%   Copyright 1984-2000 The MathWorks, Inc. 
%   $Revision: 1.7 $  $Date: 2000/06/02 04:30:56 $

[verts x y z u v w sx sy sz options] = parseargs(nargin,varargin);

if isempty(verts)
  if isempty(w)       % 2D
    if isempty(x)
      verts = stream2(u,v,sx,sy,options);
    else
      verts = stream2(x,y,u,v,sx,sy,options);
    end
  else                % 3D
    if isempty(x)
      verts = stream3(u,v,w,sx,sy,sz,options);
    else
      verts = stream3(x,y,z,u,v,w,sx,sy,sz,options);
    end
  end
end

h = [];
for k = 1:length(verts);
  vv = verts{k};
  if ~isempty(vv)
    if size(vv,2)==3
      h = [h ; line(vv(:,1), vv(:,2), vv(:,3))];
    else
      h = [h ; line(vv(:,1), vv(:,2))];
    end
  end
end

if nargout>0
  hout=h;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [verts, x, y, z, u, v, w, sx, sy, sz, options] = parseargs(nin, vargin)

verts = [];
x = [];
y = [];
z = [];
u = [];
v = [];
w = [];
sx = [];
sy = [];
sz = [];
options = [];

if nin==1  % streamline(xyz) or  streamline(xy) 
  verts = vargin{1};
  if ~iscell(verts)
    error('Stream vertices must be passed in as a cell array')
  end
elseif nin==4 | nin==5           % streamline(u,v,sx,sy)
  u = vargin{1};
  v = vargin{2};
  sx = vargin{3};
  sy = vargin{4};
  if nin==5, options = vargin{5}; end
elseif nin==6 | nin==7        % streamline(u,v,w,sx,sy,sz) or streamline(x,y,u,v,sx,sy)
  u = vargin{1};
  v = vargin{2};
  if ndims(u)==3
    w = vargin{3};
    sx = vargin{4};
    sy = vargin{5};
    sz = vargin{6};
  else
    x = u;
    y = v;
    u = vargin{3};
    v = vargin{4};
    sx = vargin{5};
    sy = vargin{6};
  end
  if nin==7, options = vargin{7}; end
elseif nin==9 | nin==10     % streamline(x,y,z,u,v,w,sx,sy,sz)
  x = vargin{1};
  y = vargin{2};
  z = vargin{3};
  u = vargin{4};
  v = vargin{5};
  w = vargin{6};
  sx = vargin{7};
  sy = vargin{8};
  sz = vargin{9};
  if nin==10, options = vargin{10}; end
else
  error('Wrong number of input arguments.'); 
end

sx = sx(:); 
sy = sy(:); 
sz = sz(:); 


