function RespObj = modeltog(RespObj);
%MODELTOG  Toggles response plot visibility.
%
%  RespObj = MODELTOG(RespObj) toggles the visibility of 
%  individual LTI models in response plots.
%
%	See also SET.

%   Karen Gondoly, 3-18-96
%   P. Gahinet 2-99
%   Copyright 1986-2000 The MathWorks, Inc. 
%   $Revision: 1.12 $  $Date: 2000/06/14 20:07:29 $

SysVis = RespObj.SystemVisibility(:);
ResponseHandles = RespObj.ResponseHandles;
SelectedChannels = RespObj.SelectedChannels;
OnModels = RespObj.SelectedModels;

% Special handling for bode/margin
if any(strcmp(RespObj.ResponseType,{'bode','margin'}))
   ny = size(SelectedChannels,1);
   SelectedChannels([1:2:2*ny,2:2:2*ny],:) = [SelectedChannels;SelectedChannels];
end   

% Turn visibility off for all response handles
% RE: 1) Hiding unselected axes won't hide its response lines!
%     2) Do it all at once because of shared handles such as axis lines in Nyquist plots
for k=1:length(SysVis)
   RHsys = ResponseHandles{k};
   RH = cat(1,RHsys{:});
   set(cat(1,RH{:}),'Visible','off')
end
   
% Turn visibility back on for selected channels of selected models/systems
for k=find(strcmp(SysVis,'on'))'
   RHsys = ResponseHandles{k};
   OM = find(OnModels{k}(:));
   if length(OM)==prod(size(OnModels{k}))
      % All models are on
      RH = cat(1,RHsys{SelectedChannels});
      set(cat(1,RH{:}),'Visible','on');
   else
      RH = RHsys(SelectedChannels);
      for io=1:prod(size(RH)),
         set(cat(1,RH{io}{OM}),'Visible','on')
      end
   end
end

% Update context menu
set(RespObj.UIContextMenu.Systems.Names,{'Checked'},SysVis)