function [ViewerObj,ToAdd] = arrangeview(ViewerObj,NewActivePlotTypes)
%ARRANGEVIEW   Changes the Response Plot arrangement in the LTI Viewer

%   Authors: K. Gondoly, 1-5-98
%   Revised: A. DiVergilio
%   Copyright 1986-2000 The MathWorks, Inc. 
%   $Revision: 1.3 $  $Date: 2000/06/14 20:03:09 $

OldActivePlotTypes = ViewerObj.ActivePlotTypes;
OldConfig = length(OldActivePlotTypes);
NewConfig = length(NewActivePlotTypes);
ViewerFig = ViewerObj.Handle;
BackgroundAxes = ViewerObj.BackgroundAxes;
UIContextMenu = ViewerObj.UIContextMenu;

%---Initialize the new BackgroundAxes and UIContextMenu
NewBackground = zeros(NewConfig,1);
NewMenus = zeros(NewConfig,1);

%---Determine which plots can simply be remapped
OldPlots = strvcat(OldActivePlotTypes{:});
NewPlots = strvcat(NewActivePlotTypes{:});
[RemapPlots,RemapTo,RemapFrom] = intersect(NewPlots,OldPlots,'rows');
NewBackground(RemapTo) = BackgroundAxes(RemapFrom);
%---If any response objects are present, also remap UIContextMenu
if ~isempty(UIContextMenu)
   NewMenus(RemapTo) = UIContextMenu(RemapFrom);
end

%---Update Background axes and UI context menu handles
ViewerObj.BackgroundAxes = NewBackground;
ViewerObj.UIContextMenu = NewMenus;
ViewerObj.ActivePlotTypes = NewActivePlotTypes;

%---Size all Background axes (will also add any other needed ones)
ViewerObj = plotpos(ViewerObj);

%---List of added plots
ToAdd = 1:NewConfig;
ToAdd(RemapTo) = [];

%---Delete anything left over
ToDelete = 1:OldConfig;
ToDelete(RemapFrom) = [];
%---If any response objects are present, also clear axes and delete UIContextMenu
if ~isempty(UIContextMenu)
   for ct = ToDelete
      RespObj = get(UIContextMenu(ct),'UserData');
      if isa(RespObj,'response')
         cla(RespObj,1);  %---This will also remove the UIContextMenu of the respobj
      end
   end
end
delete(BackgroundAxes(ToDelete));
