function [RespObj,HoldFlag,NumOldSys] = ...
   nextplot(RespObj,NumRows,NumColumns,NewPlotType,SystemNames,InputLabels,OutputLabels)
%NEXTPLOT  Updates or clears existing response object before plotting new one.
%
%   RespObj is the response object associated with current background axis.

%   Karen Gondoly and P. Gahinet.
%   Copyright 1986-2000 The MathWorks, Inc. 
%   $Revision: 1.3 $   $Date: 2000/06/14 20:07:30 $

BackAx = RespObj.BackgroundAxes;
HoldValue = RespObj.NextPlot;
PlotAxes = RespObj.PlotAxes;

%---Check if the NextPlot Value was toggled at the command line
AllAxes = [BackAx;PlotAxes(:)];
AllHold = get(AllAxes,'NextPlot');
if ~all(strcmp(AllHold,HoldValue)), 
   % A NextPlot was changed. Use new value
   HoldStates = {'replace';'add'};
   HoldValue = HoldStates{~strcmp(HoldValue,HoldStates)};
   RespObj.NextPlot = HoldValue;
   set(AllAxes,'NextPlot',HoldValue);
end

%---Set up based on hold value
if strcmp(HoldValue,'add'),
   % Hold is on
   if ~strcmp(NewPlotType,RespObj.ResponseType),
      %---Can only overlay plots of same plot type
      error('Cannot plot different response types on the same axes.');
   elseif ~isequal(size(PlotAxes),[NumRows,NumColumns]),
      %---Can not plot systems of different dimensions
      error('Cannot plot models with different I/O dimensions on the same axes.')
   end
   
   % Set hold flag on
   HoldFlag = 1; 
   NumOldSys = length(RespObj.SystemNames);
   
   % Update input and output labels and merge system names
   RespObj.InputLabel = InputLabels;
   RespObj.OutputLabel = OutputLabels;
   RespObj.SystemNames = [RespObj.SystemNames ; SystemNames];
   
else
   %---Reinitialize the Response Object
   HoldFlag = 0;
   NumOldSys = 0;
   cla(RespObj);
   RespObj = response(NumRows,NumColumns,BackAx,NewPlotType,SystemNames,InputLabels,OutputLabels);
end % if/else strcmp(HoldValue)


