function varargout=ltiview(plottype,varargin);
%LTIVIEW Open the LTI Viewer
%   LTIVIEW issued from the command line without any input arguments
%   initializes the LTI Viewer. This user-interface is used to perform the
%   various Control Toolbox response functions for any state space, transfer  
%   function, or zero-pole-gain system in the workspace.
%
%   LTIVIEW(PLOTTYPE) opens an LTI Viewer with the LTI system response
%   of a random model with two inputs, two outputs, and three states. 
%   PLOTTYPE may be any of the following strings.
%
%   PLOTTYPE may be: 1) 'step'
%		     2) 'impulse'
%		     3) 'bode'
%		     4) 'nyquist'
%		     5) 'nichols'
%		     6) 'sigma'
%		     7) 'lsim'
%		     8) 'initial'
%
%   LTIVIEW(PLOTTYPE,SYS) initializes a new LTI Viewer with the response type 
%   indicated by PLOTTYPE for the LTI system SYS.
%
%   LTIVIEW(PLOTTYPE,SYS1,SYS2,...SYSN) initializes a new LTI Viewer with the
%   responses of multiple LTI systems
%
%   LTIVIEW(PLOTTYPE,SYS1,PLOTSTR1,SYS2,PLOTSTR2,...SYSN,PLOTSTRN) initializes 
%   an LTI Viewer with the responses of multiple LTI systems, each represented
%   by a line with the characteristics in the associated PLOTSTR. A color
%   linestyle, and marker may be contained in each PLOTSTR string. For
%   example, 'r' is a solid red line, 'b--' is a dashed blue line, and
%   'm*:' is a dotted magenta line with astericks as markers.
%
%   LTIVIEW(PLOTTYPE,SYS,OPTIONS) allows the additional input 
%   arguments supported by the different PLOTTYPES to be entered.
%   These arguments are specified in accordance with the 
%   function given in PLOTTYPE. See the appropriate HELP text for more
%   detail on the format of these extra arguments. In the case of LSIM
%   and INITIAL, the OPTIONS field must include the input signal or
%   initial condition, respectively. OPTIONS can be added to the end
%   of any LTIVIEW calling syntax
%
%   Two additional PLOTTYPE options are available for manipulating 
%   previously opened LTI Viewers:
%
%   LTIVIEW('clear',Hviewers) clears the plots and data from the LTI Viewers
%   with the handles provided in Hviewers.
%
%   LTIVIEW('current',SYS1,PLOTSTR1,SYS2,PLOTSTR2,...SYSN,PLOTSTRN,Hviewers)
%   appends the responses for the systems in SYS# to the LTI Viewers with the
%   handles provided in Hviewers, if the systems have the same dimensions as
%   those currently in the LTI Viewer. If the dimensions are different, the LTI 
%   Viewer is first cleared and only the new responses are shown. The response  
%   type will be the one currently selected by the Plot Type popup menu. 
%
%   See also STEP, IMPULSE, BODE, NYQUIST, NICHOLS, SIGMA, LSIM, INITIAL

%   Karen Gondoly, 8-6-96
%   Copyright (c) 1986-97 by The MathWorks, Inc.
%   $Revision: 1.14 $

NumberOfSys=0;
NumPlotStr=0;

if nargin==0
   action='command';
   
else, %if nargin==1,
   if ~ischar(plottype),
      error('The first input argument must be a valid LTIVIEW string');
   end
   
   %---Check the string in plottype
   switch plottype
   case {'current','clear'},
      action = plottype;
      if isempty(varargin),
         error(['The CURRENT and CLEAR options require Valid LTI Viewer handles'])
      else
         ViewerHandles=varargin{end};
         if any(~ishandle(ViewerHandles)),
            if length(ViewerHandles)>1,
               %---When invalid handles are in a vector 
               ViewerHandles = ViewerHandles(find(ishandle(ViewerHandles)));
            else
               %---When invalid handle is an LTI object or char.
               ViewerHandles=[]; 
            end
         end
         if isempty(ViewerHandles),
            error(['The CURRENT and CLEAR options require Valid LTI Viewer handles'])
         else
            lengthvarargin=length(varargin)-1;
         end, %if/else ~any(ishandle...
      end, % if/else isempty(varargin)
      
   case {'step','impulse','bode','nyquist','nichols','sigma','lsim','initial'}
      action='initialize';
      lengthvarargin=length(varargin);
      if lengthvarargin,
         if ~isa(varargin{1},'lti')
            error('The second input argument must be an LTI object');   
         end
      end
      
   otherwise,
      error('The first input argument is not a valid LTIVIEW string');
   end         
   
   %---Get any lti objects
   if nargin==1, % Can only get in here if a response PLOTTYPE is entered
      sys=rss(3,2,2);
      FunctionStr='sys';
      NumberOfSys=1;
      PlotStr={};
   else,
      ModelNames={};
      ModelObjects={};
      PlotStr={};
      sArg=0;
      if isa(varargin{1},'lti'),
         NumberOfSys=1;
         if ~isempty(inputname(2)),
            eval([inputname(2),'=varargin{1};']);
            ModelNames{NumberOfSys}=inputname(2);
         else
            n=tempname;
            d=tempdir;
            eval(['LTI',n(length(d)+1:end),'=varargin{1};']);
            ModelNames{NumberOfSys}=['LTI',n(length(d)+1:end)];
         end, % if/else ~isempty(inputname(2))
         
         ModelObjects{NumberOfSys}=varargin{1};
         [numModelIn,numModelOut]=size(ModelObjects{1});
         FunctionStr=ModelNames{NumberOfSys};
      end
      for ct=2:lengthvarargin,
         if isa(varargin{ct},'lti'),
            NumberOfSys=NumberOfSys+1;
            if ~isempty(inputname(1+ct)),
               eval([inputname(1+ct),'=varargin{ct};']);
               ModelNames{NumberOfSys}=inputname(ct+1);
            else
	            n=tempname;
   	         d=tempdir;
      	      eval(['LTI',n(length(d)+1:end),'=varargin{ct};']);
               ModelNames{NumberOfSys}=['LTI',n(length(d)+1:end)];
            end
            FunctionStr=[FunctionStr,','ModelNames{NumberOfSys}];
            
            ModelObjects{NumberOfSys}=varargin{ct};
            [numNewIn,numNewOut]=size(ModelObjects{NumberOfSys});
            if ~isequal(numNewIn,numModelIn) | ~isequal(numNewOut,numModelOut),
               error('All systems must have the same dimensions');
            end
         elseif isa(varargin{ct},'char'),
            NumPlotStr=NumPlotStr+1;
            FunctionStr=[FunctionStr,',''',varargin{ct},''''];
            PlotStr{NumPlotStr}=varargin{ct};
         else
            sArg=sArg+1;
            eval(['ExtraArg',num2str(sArg),'=varargin{ct};']);
            FunctionStr=[FunctionStr,',ExtraArg',num2str(sArg)];
         end
      end % for ct
      if ~isequal(NumberOfSys,NumPlotStr) & NumPlotStr,
         error('The plot string must be defined for each system');
      end
   end % if/else nargin==1
end % if nargin

switch action
   
case {'initialize','command'},
   %---Open a new LTI Viewer 
   %---Modified 3/30/97: Now, a new Viewer is *always* opened. The CURRENT
   %---option for the PLOTTYPE variable was added to address the concept
   %---of modifying the current LTI Viewer from a callback or command line
   AllViewerFigs = findobj(allchild(0),'Tag','ResponseGUI');
   
   [fignum]=LocalOpenFig(NumberOfSys); 
   
   %---Add the desired plots
   if strcmp(action,'initialize'),
      eval(['rguiset(fignum,plottype,',FunctionStr,');']);
      
      %---Check if a time or frequency vector was entered
      if sArg
         RespPrefGUI=0;
         if ~isempty(AllViewerFigs),
            RespPrefGUI = get(findobj(AllViewerFigs(1),'Tag','RespMenu'),'Userdata');
         end
         PossibleVec=eval(['ExtraArg',num2str(sArg)]);
         if ~ischar(PossibleVec),
            if ~RespPrefGUI,
               RespPrefGUI = rguiropts('initialize',fignum);
            end
            udRPG = get(RespPrefGUI,'UserData');
            set(RespPrefGUI,'visible','off');
            switch plottype
            case {'step','impulse'}, % Look for time
               set(udRPG.Handles.DefaultTime,'Value',0);
               udRPG.Revert.DefaultTime=0;
               set(udRPG.Handles.DefineTime,'Value',1);
               udRPG.Revert.DefineTime=1;
               set(udRPG.Handles.TimeVector,'Enable','on');
            case {'bode','nyquist','nichols','sigma'}, % Look for Freq
               set(udRPG.Handles.DefaultFreq,'Value',0);
               udRPG.Revert.DefaultFreq=0;
               set(udRPG.Handles.DefineFreq,'Value',1);
               udRPG.Revert.DefineFreq=1;
               set(udRPG.Handles.FreqVector,'Enable','on');
            end, % switch plottype
            set(RespPrefGUI,'UserData',udRPG)
         end, % if ~ischar         
      end, % if sArg
      
      %---Check for linestyle preferences
      if ~isempty(PlotStr),
         PlotPrefGUI = rguipopts('initialize',fignum);
         set(PlotPrefGUI,'visible','off')
      end, % if ischar(PossibleVec...  
      
   end, % if strcmp(action,'initialize')
   
   %---Turn the LTI Viewers HandleVisibility to callback
   set(fignum,'HandleVisibility','Callback');

case 'current',
   if NumberOfSys,
      [NumNewIn,NumNewOut]=size(ModelObjects{1});
      
      %----Update the specified LTI Viewers with the new systems
      %----Retain the current plottype
      for ctViewer=1:length(ViewerHandles),
         udViewer = get(ViewerHandles(ctViewer),'UserData');
         plotnum=get(findobj(ViewerHandles(ctViewer),'Tag','PlotPopup'),'Value');
         if isempty(udViewer);
            OldNames='';
            SystemStr='';
            PlotInd=[];
            udViewer(1).plotstr='r';
         else
            [NumOldIn,NumOldOut]=size(udViewer(1).system);
            %---If the old systems are a different dimension, delete them
            if ~isequal(NumOldIn,NumNewIn) | ~isequal(NumOldOut,NumNewOut),
               ltiview('clear',ViewerHandles(ctViewer));
               OldPlotStr = udViewer(1).plotstr;
               udViewer = get(ViewerHandles(ctViewer),'UserData');
               udViewer(1).plotstr=OldPlotStr;
               OldNames='';
            else
               OldNames=cellstr(strvcat(udViewer.name));               
            end
            PlotInd = get(findobj(ViewerHandles(ctViewer),'Tag','SystemSelector'),'UserData');
            SystemStr = get(findobj(ViewerHandles(ctViewer),'Tag','SystemSelector'),'String');
         end
         
         %---Update the LTI Viewer UserData         
         for ctModels = 1:length(ModelNames);
            indsys = strmatch(ModelNames{ctModels},OldNames);
            if isempty(indsys), % Append a new system
               indsys=length(SystemStr)+1;
               PlotInd=[PlotInd,1];
               SystemStr{indsys,1} = ['+ ',ModelNames{ctModels},' ()'];
               udViewer(indsys,1).plottype=plotnum;
            end
            if NumPlotStr,
               udViewer(indsys,1).plotstr=PlotStr{ctModels};
            end
            udViewer(indsys,1).name=ModelNames{ctModels};
            udViewer(indsys,1).system=ModelObjects{ctModels};
            
         end % for ctModels
         
         set(findobj(ViewerHandles(ctViewer),'Tag','SystemSelector'),'UserData',PlotInd,...
            'String',SystemStr);
         
         set(ViewerHandles(ctViewer),'UserData',udViewer)
         
         %---Make sure RGUIFCN can "see" the current LTI Viewer
         figure(ViewerHandles(ctViewer))
         set(ViewerHandles(ctViewer),'HandleVisibility','on')
         
         %---Update the Viewer response plots
         rguifcn('popupcallback',ViewerHandles(ctViewer));
         
         %---Hide the current LTI Viewer, again
         set(ViewerHandles(ctViewer),'HandleVisibility','callback')
         
      end % for ctViewers
   end, % if NumberOfSys
   fignum=ViewerHandles;   
   
case 'clear',
   %----Clear any old systems out of the specified LTI Viewers
   for ctViewer=1:length(ViewerHandles),
      rguiset(ViewerHandles(ctViewer),'initdata',0); % Erase Userdata
      %---Clear System Selector
      set(findobj(ViewerHandles(ctViewer),'Tag','SystemSelector'),'String','','UserData',[]);
      
      %---Clear I/O selection field
      delete(get(findobj(ViewerHandles(ctViewer),'Tag','selector'),'children'))
      
      %---Clear display axes
      B=findobj(ViewerHandles(ctViewer),'Tag','BackgroundAxes');
      Axpos=get(B,'Position');
      AxUnit=get(B,'Unit');
      delete(B);
      %---Put BackgroundAxes back
      NewB=axes('Parent',ViewerHandles(ctViewer),'Unit',AxUnit',...
         'Position',Axpos,'Visible','off','Tag','BackgroundAxes');
      set([get(NewB,'Xlabel'),get(NewB,'Ylabel'),get(NewB,'Title')],'Visible','on')
      
      %---Turn off any Plot Options
      set(findobj(ViewerHandles(ctViewer),'style','checkbox'),'Value',0)
 
   end % for ctViewer
   fignum=ViewerHandles;
end

no=nargout;
if no,
   varargout(1)={fignum};
end

%--------------------------------Internal Functions----------------------------
%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%% LocalOpenFig %%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%
function [fig]=LocalOpenFig(NumberOfSys);

currentU = get(0,'Units');
set(0,'Units','points');
ScreenSize = get(0,'ScreenSize');
set(0,'Units',currentU);

%---Open the initial figure

%---Set up the standard object property list
StdUnit = 'points';
StdColor = [0.8 0.8 0.8];

figpos = [20 20 450 325];

DeleteFcn=['AllFigs=allchild(0);', ...
      'AllRespGUIs=findobj(AllFigs,''Tag'',''ResponseGUI'');', ...
      'PlotFig=get(findobj(gcf,''Tag'',''PlotMenu''),''UserData'');', ...
      'if ~isequal(PlotFig,0) & (length(AllRespGUIs)==1),', ...
      'delete(PlotFig);end;', ...
      'RespFig=get(findobj(gcf,''Tag'',''RespMenu''),''UserData'');', ...
      'if ~isequal(RespFig,0) & (length(AllRespGUIs)==1),', ...
      'delete(RespFig);end;', ...
      'kids=findobj(allchild(gcf),''Tag'',''LTIresponseLines'');', ...
      'kids=[kids;findobj(allchild(gcf),''Tag'',''BackgroundLine'')];', ...
      'set(kids,''DeleteFcn'','''');', ...
      'clear AllFigs AllRespGUIs PlotFig RespFig kids'];

fig = figure('BackingStore','off', ...
   'Number','off', ...
   'Name',prepender('LTI Viewer'),...
   'Color',StdColor, ...
   'DeleteFcn',DeleteFcn, ...
   'Tag','ResponseGUI', ...
   'Menu','none', ...
   'Unit',StdUnit , ...
   'Position',figpos, ...
   'UserData',[], ...
   'Visible','off');

%---Add the BackgroundAxes
ax_main=axes('parent',fig, ...
   'unit',StdUnit, ...
   'Position',[30 155 406 152], ...
   'Tag','BackgroundAxes', ...
   'visible','off');

X=xlabel(' ');
Y=ylabel(' ');
T=title(' ');
set([X,Y,T],'visible','on');

%---Add the other controls
BoundingAxes=axes('parent',fig, ...
   'color',StdColor, ...
   'box','on', ...
   'xtick',[], ...
   'ytick',[], ...
   'HandleVisibility','callback', ...
   'tag','ZoomAxes', ...
   'unit',StdUnit, ...
   'Position',[312 125 131 19]);   

ZoomText=uicontrol(fig, ...
   'style','text', ...
   'back',StdColor, ...
   'string','Zoom:', ...
   'Tag','ZoomText', ...
   'UserData',128, ...
   'horiz','left', ...
   'unit',StdUnit, ...
   'Position',[315 128 29 11]);

CallStr=['rguifcn(''zoomcallback'');';
   'rguifcn(''zoomcallback'');';
   'rguifcn(''zoomcallback'');';
   'rguifcn(''unzoom'');      '];

ButtonIdStr=['XZoom ';
   'YZoom ';
   'XYZoom';
   'UnZoom'];

% common text properties:
tp = [',''color'',''k'',''horizontalalignment'',''center'',''fontsize'',8'];  

%---Xzoom
btnicon1 = [ ...
      '[text(.5,.2,''In-X''' tp '),' ...
      ' line([.1 .1 NaN .1 .9 NaN .9 .9],' ...
      '[.65 .95 NaN .8 .8 NaN .95 .65],''color'',''k'')]'];       

%---Yzoom
btnicon2 = [ ...
      '[text(.5,.2,''In-Y''' tp '),'...
      ' line([.35 .65 NaN .5 .5 NaN .35 .65],' ...
      '[.63 .63 NaN .63 .95 NaN .95 .95],''color'',''k'')]'];

%---XY zoom
btnicon3 = [ ...
      '[line([.2 .8 .8 .2 .2],[.65 .65 .95 .95 .65],''linestyle'','':'','...
      '''color'',''k'') text(.5,.2,''X-Y''' tp '),'...
      ' line([.2 .8],[.95 .65],''linestyle'',''none'',''marker'',''+'','...
      '''color'',''k'')  ]'];

%---Revert
btnicon4 = [ ...
      '[text(.5,.7,''Full''' tp ')'...
      ' text(.5,.2,''View''' tp ')]'];

ButtonLabels = str2mat(btnicon1,btnicon2,btnicon3,btnicon4);

ZoomButtons=btngroup(fig, ...
   'GroupID','ZoomFcn', ...
   'ButtonID',ButtonIdStr, ...
   'IconFunctions',ButtonLabels, ...
   'Unit',StdUnit, ...
   'Position',[345 126 97 16], ...
   'Callback',CallStr, ...
   'ButtonColor',[.75 .75 .75], ...
   'Exclusive','yes', ...
   'PressType','toggle', ...
   'GroupSize',[1 4]);

%-----System selection field

%---Add system listbox---store the default color vector in the userdata
C={'r','b','g','c','m','y','k'};
SystemFrame(1)=uicontrol(fig, ...
   'style','frame', ...
   'Unit',StdUnit, ...
   'position',[4 18 162 99], ...
   'back',StdColor);

SystemText(1)=uicontrol(fig, ...
   'style','text', ...
   'UserData',C, ...
   'Tag','SystemColors', ...
   'Unit',StdUnit, ...
   'position',[65 113 41 11], ...
   'back',StdColor, ...
   'string','Systems');

SystemFrame(2)=uicontrol(fig, ...
   'style','frame', ...
   'Unit',StdUnit, ...
   'position',[8 20 72 89], ...
   'back',StdColor);

SystemText(2)=uicontrol(fig, ...
   'style','text', ...
   'Unit',StdUnit, ...
   'position',[17 105 55 11], ...
   'back',StdColor, ...
   'string','Workspace');

SystemBox=uicontrol(fig, ...
   'style','listbox', ...
   'Max',2, ...
   'back',[1 1 1], ...
   'Unit',StdUnit, ...
   'position',[12 34 67 70], ...
   'String',{' '}, ...
   'Tag','SystemList', ...
   'Value',[], ...
   'UserData',[], ...
   'CallBack','rguifcn(''listcallback'');');

SystemUpdate=uicontrol('Parent',fig, ...
   'Style','PushButton', ...
   'Unit',StdUnit, ...
   'position',[10 22 35 11], ...
   'String','Refresh', ...
   'Tag','UpdateButton', ...
   'Callback','rguifcn(''systemlist'');');

SystemClear=uicontrol('Parent',fig, ...
   'Style','PushButton', ...
   'Unit',StdUnit, ...
   'position',[46 22 33 11], ...
   'String','Select>', ...
   'Enable','off', ...
   'Tag','AddButton', ...
   'Callback','rguifcn(''addcallback'');');

SystemFrame(3)=uicontrol(fig, ...
   'style','frame', ...
   'Unit',StdUnit, ...
   'position',[84 20 78 89], ...
   'back',[.8 .8 .8]);

SysSelText=uicontrol(fig, ...
   'style','text', ...
   'Unit',StdUnit, ...
   'position',[103 105 47 11], ...
   'back',[.8 .8 .8], ...
   'string','Selected');

ActiveBox = uicontrol('Parent',fig, ...
   'BackgroundColor',[1 1 1], ...
   'Unit',StdUnit, ...
   'Position',[88 34 71 70], ...
   'Style','listbox', ...
   'Max',2, ...
   'Callback','rguifcn(''setplotcheck'');', ...
   'Tag','SystemSelector');

DeleteButton = uicontrol('Parent',fig, ...
   'Unit',StdUnit, ...
   'Position',[88 22 33 11], ...
   'String','<Delete', ...
   'Enable','off', ...
   'Callback','rguifcn(''deletecallback'');', ...
   'Tag','DeleteButton');

PlotButton = uicontrol('Parent',fig, ...
   'Style','checkbox', ...
   'Unit',StdUnit, ...
   'Position',[123 22 35 11], ...
   'String','Plot', ...
   'back',StdColor, ...
   'enable','off', ...
   'Value',0, ...
   'CallBack','rguifcn(''plotcheck'');', ...
   'Tag','PlotCheck');

%-----I/O channel selector axes
SelText=uicontrol(fig, ...
   'style','text', ...
   'Unit',StdUnit, ...
   'position',[176 113 113 11], ...
   'back',StdColor, ...
   'string','Displayed I/O Channels');

ax1=axes('Parent',fig, ...
   'ButtonDownFcn','rguifcn(''selectrbbox'');', ...
   'box','on', ...
   'color',StdColor, ...
   'Xlim',[0 1], ...
   'XlimMode','manual', ...
   'Xtick',[], ...
   'Xgrid','off', ...
   'Ylim',[0 1], ...
   'YlimMode','manual', ...
   'Ytick',[], ...
   'Ydir','reverse', ...
   'Ygrid','off', ...
   'unit',StdUnit, ...
   'Position',[172 18 122 99],...
   'Tag','selector');

axes(ax1);
axis ij

PlotFrame=uicontrol(fig, ...
   'style','frame', ...
   'Unit',StdUnit, ...
   'position',[298 86 65 31], ...
   'back',StdColor);
PlotText=uicontrol(fig, ...
   'style','text', ...
   'Unit',StdUnit, ...
   'Position',[305 113 51 11], ...
   'back',StdColor, ...
   'string','Plot Type');
PlotPopup=uicontrol(fig, ...
   'style','popup', ...
   'back',[1 1 1], ...
   'Unit',StdUnit, ...
   'position',[302 94 57 15], ...
   'value',1, ...
   'UserData',0, ...
   'Tag','PlotPopup', ...
   'CallBack','rguifcn(''popupcallback'');', ...
   'string','Step|Impulse|Bode|Nyquist|Nichols|Sigma|Pole-Zero|Lsim|Initial');

%---Add display options field
AxesFrame=uicontrol('Parent',fig, ...
   'style','frame', ...
   'Unit',StdUnit, ...
   'position',[370 18 74 99], ...
   'back',StdColor, ...
   'tag','DispOpts');

PlotText=uicontrol(fig, ...
   'style','text', ...
   'Unit',StdUnit, ...
   'position',[377 113 57 11], ...
   'back',StdColor, ...
   'string','Plot Options');

%---Add invisible checkboxes to use for display options
%---Set up different display options for each response type

%---For Time Domain
TagStr=[{'TimePeakCheck'};{'SetCheck'};{'RiseCheck'};{'DcCheck'}];
CheckStr=[{'Peak Response'};{'Settling Time'};{'Rise Time'};{'Steady State'}];
for ct=1:4,
   DispOpts(ct)=uicontrol('Parent',fig, ...
      'style','checkbox', ...
      'String',CheckStr{ct}, ...
      'back',StdColor, ...
      'visible','off', ...
      'callback','rguifcn(''checkcallback'');', ...
      'Tag',TagStr{ct}, ...
      'horiz','left', ...
      'Unit',StdUnit, ...
      'position',[371 112-(ct*16) 71 13]);
end

%---For Freq Domain
TagStr=[{'MarginCheck'};{'FreqPeakCheck'}];
CheckStr=[{'Gain/Phase Margin'};{'Max. Response'}];
for ct=1:2,
   DispOpts(ct)=uicontrol('Parent',fig, ...
      'style','checkbox', ...
      'String',CheckStr{ct}, ...
      'back',StdColor, ...
      'visible','off', ...
      'callback','rguifcn(''checkcallback'');', ...
      'Tag',TagStr{ct}, ...
      'horiz','left', ...
      'Unit',StdUnit, ...
      'position',[371 112-(ct*16) 71 13]);
end

%---Add radio buttons for configuring plotting axes
AxesFrame=uicontrol(fig, ...
   'style','frame', ...
   'Unit',StdUnit, ...
   'position',[299 18 65 61], ...
   'back',StdColor);
AxesText=uicontrol(fig, ...
   'style','text', ...
   'Unit',StdUnit, ...
   'position',[300 74 61 11], ...
   'back',StdColor, ...
   'string','Configuration');

AllButton = uicontrol('Parent',fig, ...
   'BackgroundColor',StdColor, ...
   'Unit',StdUnit, ...
   'Position',[301 61 58 13], ...
   'Horiz','left', ...
   'String','Group all', ...
   'CallBack','rguifcn(''configcallback'');', ...
   'Style','radiobutton', ...
   'Tag','SingleButton');
InButton = uicontrol('Parent',fig, ...
   'BackgroundColor',StdColor, ...
   'Horiz','left', ...
   'Unit',StdUnit, ...
   'Position',[301 48 58 13], ...
   'String','Group in', ...
   'CallBack','rguifcn(''configcallback'');', ...
   'Style','radiobutton', ...
   'Tag','InButton');
OutButton = uicontrol('Parent',fig, ...
   'BackgroundColor',StdColor, ...
   'Unit',StdUnit, ...
   'Position',[301 35 58 13], ...
   'Horiz','left', ...
   'String','Group out', ...
   'CallBack','rguifcn(''configcallback'');', ...
   'Style','radiobutton', ...
   'Tag','OutButton');
ArrayButton=uicontrol(fig, ...
   'style','radiobutton', ...
   'back',StdColor, ...
   'value',1, ...
   'Unit',StdUnit, ...
   'position',[301 22 58 13], ...
   'Tag','ArrayButton', ...
   'Horiz','left', ...
   'CallBack','rguifcn(''configcallback'');', ...
   'string','Array');

%---Set Userdata for making radio-buttons mutually exclusive
set(ArrayButton,'UserData',[AllButton,InButton,OutButton]);
set(AllButton,'UserData',[ArrayButton,InButton,OutButton]);
set(InButton,'UserData',[AllButton,ArrayButton,OutButton]);
set(OutButton,'UserData',[AllButton,InButton,ArrayButton]);

%---Add Status text
StatusFrame=uicontrol(fig, ...
   'style','frame', ...
   'back',StdColor, ...
   'Unit',StdUnit, ...
   'Position',[4 2 439 13]);
StatusText=uicontrol(fig, ...
   'style','text', ...
   'back',StdColor, ...
   'string','Select the desired system and type of response to display', ...
   'Tag','StatusText', ...
   'horiz','left', ...
   'Unit',StdUnit, ...         
   'position',[8 3 415 11]);
      
%---Add uimenus
FileMenu=uimenu(fig, ...
   'label','File');

NewMenu=uimenu(FileMenu, ...
   'label','New Viewer...', ...
   'Accelerator','n', ...
   'Tag','NewMenu', ...
   'Callback','ltiview;');

%   SaveMenu=uimenu(FileMenu, ...
%	'enable','off', ...
%	'label','Save');

PrintMenu=uimenu(FileMenu, ...
   'Separator','on', ...
   'Callback','rguifcn(''printfig'');', ...
   'Tag','PrintMenu', ...
   'Accelerator','p', ...
   'label','Print Response');

CloseMenu=uimenu(FileMenu, ...
   'Accelerator','w', ...
   'Separator','on', ...
   'Tag','CloseMenu', ...
   'label','Close Viewer', ...
   'CallBack','rguifcn(''closewindow'');');

OptMenu=uimenu(fig, ...
   'tag','OptMenu', ...
   'label','Tools');

%---Check for old preferences windows
allwins=allchild(0);
RP=findobj(allwins,'Tag','RespPrefs');
if isempty(RP),
   RP=0;
end
PP=findobj(allwins,'Tag','PlotPrefs');
if isempty(PP),
   PP=0;
end

GuiCheck=uimenu(OptMenu, ...
   'label','Viewer Controls...', ...
   'Tag','GuiCheck', ...
   'checked','on', ...
   'Accelerator','v', ...
   'Callback','rguifcn(''showguis'');');

RespMenu=uimenu(OptMenu, ...
   'label','Response Preferences', ...
   'UserData',RP, ...
   'Separator','on', ...
   'Tag','RespMenu', ...
   'Accelerator','r', ...
   'Callback','rguiropts;');

LineMenu=uimenu(OptMenu, ...
   'label','Linestyle Preferences', ...
   'UserData',PP, ...
   'Accelerator','l', ...
   'Tag','PlotMenu', ...
   'Callback','rguipopts;');

PlotMenu=uimenu(fig, ...
   'label','Plots');

GridMenu=uimenu(PlotMenu, ...
   'label','Grid on', ...
   'checked','off', ...
   'Callback','rguifcn(''gridcallback'');', ...
   'Tag','GridButton');

HelpMenu=uimenu(fig, ...
   'label','Help');

ControlHelp=uimenu(HelpMenu, ...
   'Callback','rguifcn(''help'');', ...
   'label','Viewer Controls...');

ControlHelp=uimenu(HelpMenu, ...
   'Separator','on', ...
   'Callback','rguiropts(''help'');', ...
   'label','Response Preferences...');

ControlHelp=uimenu(HelpMenu, ...
   'Callback','rguipopts(''help'');', ...
   'label','Linestyle Preferences...');

%---Set up the userdata
rguiset(fig,'initdata',NumberOfSys);
rguifcn('systemlist'); % initialize the workspace list
k = allchild(fig);
set(findobj(k,'units',StdUnit),'Unit','norm');
set(fig,'resize','on','visible','on')
