function fig = newfig
%NEWFIG Returns the "right" figure in which to plot.
%       FIG = NEWFIG returns the "right" figure in which to plot.
%       It differs from newplot in the following ways:
%       * No axes are created.
%       * All figures are searched for one which does not have a NextPlot
%         of new before a new figure is created; newplot only checks gcf
%         before creating a new figure.
%       Note that newfig makes FIG the CurrentFigure.
%
%       See also: NEWPLOT.

%       Author(s): A. Potvin, 6-1-94
%       Copyright (c) 1994 by The MathWorks, Inc.
%       $Revision: 1.3 $  $Date: 1995/01/17 13:42:57 $

no = nargout;
figs = get(0,'Children');
nextfig = [];
for i=1:length(figs),
   NextPlot = get(figs(i),'NextPlot');
   if ~strcmp(NextPlot,'new'),
      nextfig = figs(i);
      if strcmp(NextPlot,'replace'),
         delete(get(nextfig,'Children'))
         reset(nextfig)
         set(fig,'NextPlot','add')
      end
      figure(nextfig)
      break
   end
end

if isempty(nextfig),
   nextfig = figure;
end

if no==1,
   fig = nextfig;
end

% end newfig
