function rguiset(H,Property,varargin);

%RGUISET Assign properties to the LTI Viewer
%   RGUISET(H,Property,varargin) is used to interface the LTI response
%   commands with the LTI Viewer as well as assign other Viewer properties
%
%   Possible properties
%		1) 'initdata' --> Initializes the LTI Viewer userdata
%		2) 'step' --> Put step response in the LTI Viewer userdata
%		3) 'impulse' --> Put impulse responseino the LTI Viewer userdata
%		4) 'lsim' --> Put lsim response in the LTI Viewer userdata
%		5) 'initial' --> Put initial condition response in the LTI Viewer userdata
%		6) 'bode' --> Put bode response in the LTI Viewer userdata
%		7) 'margin' --> Put gain/phase margin data in the LTI Viewer userdata
%		8) 'nyquist' --> Put nyquist plots in the LTI Viewer userdata
%		9) 'nichols' --> Put nichols plots in the LTI Viewer userdata
%	   10) 'sigma' --> Put sigma data in the LTI Viewer userdata
%	   11) 'Size' --> 'Tall' = Display Viewer Controls
%		  	  --> 'Short' = Hide Viewer Controls
%
%   See also LTIVIEW

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

ud=get(H,'UserData');
[numsys,garb]=size(ud);
currentSys=0;
CallbackFig = gcbf;
Property=lower(Property);

if ~strcmp(Property,'size') & ~strcmp(Property,'initdata'),
   % Parse input list
   ni=nargin;
   no=nargout;
   nsys = 0;      % counts LTI systems
   nstr = 0;      % counts plot style strings 
   nt = 0;
   sys = cell(1,ni-2);
   sysname = cell(1,ni-2);
   pstyle = cell(1,ni-2);
   ExtraArg = cell(1,ni-2); 
   for j=1:ni-2,
      argj=varargin{j};
      if isa(argj,'lti'),
         nsys = nsys+1;   sys{nsys} = argj;
         if ~isproper(argj),
            error('Not available for improper systems.')
         end   
         [ny,nu] = size(sys{1});
         [nyj,nuj] = size(sys{nsys});
         if nyj~=ny | nuj~=nu,
            error('All systems must have the same number of inputs and outputs.')
         end
         sysname{nsys} = inputname(j+2);
      elseif isa(argj,'char')
         nstr = nstr+1;   pstyle{nstr} = argj;
      else
         nt = nt+1;   ExtraArg{nt} = argj;
      end
   end
   Ts = zeros(1,nsys);
   for j=1:nsys,
      sysj = sys{j};
      Ts(j) = get(sysj,'ts');
   end
   % Check sample times
   if any(Ts==-1),
      if any(Ts~=-1),
         error('Must specify all sample times when at least one is specified.')
      elseif ~isequal(tfinal,round(tfinal)),
         error(...
            'Final time must be an integer (# of samples) when sample time is unspecified.');
      else
         % Systems with unspecified Ts: TFINAL is an integer
         tfinal = tfinal-1;   % n samples -> 0 to n-1
      end
   end
end % if ~strcmp

switch Property
   
case 'initdata' 
   NumberOfSys=varargin{1};
   if ~NumberOfSys,
      NumberOfSys=0; % 
   end	  
   
   F={ 'name', ...
         'system', ...
         'step', ...
         'impulse', ...
         'bode', ...
         'nyquist', ...
         'nichols', ...
         'sigma', ...
         'data', ...
         'plottype', ...
         'plotstr'};
   
   Fstep={'response','time','dcgain'};
   
   Fimpulse={'response','time'};
   
   Fbode={'magnitude','phase','frequency'};
   
   Fnyquist={'real', 'imaginary', 'frequency'};
   
   Fnichols={'magnitude','phase','frequency'};
   
   Fsigma={'values','frequency'};
   
   Fdata = {'DataType','Xdata','Ydata','Pdata'};
   Fdata2 = {'vector','unit'};
   
   udnew=initstructs([NumberOfSys,1],F);
   for ct=1:NumberOfSys,
      udnew(ct).step=initstructs(1,Fstep);
      udnew(ct).impulse=initstructs(1,Fimpulse);
      udnew(ct).bode=initstructs(1,Fbode);
      udnew(ct).nyquist=initstructs(1,Fnyquist);
      udnew(ct).nichols=initstructs(1,Fnichols);
      udnew(ct).sigma=initstructs(1,Fsigma);
      udnew(ct).data=initstructs(1,Fdata);
      udnew(ct).data.Xdata=initstructs(1,Fdata2);
      udnew(ct).data.Ydata=initstructs(1,Fdata2);
      udnew(ct).data.Pdata=initstructs(1,Fdata2);
      udnew(ct).plottype=0;
   end % for ct
   
   % If a hold is on the figure...make sure the old userdata is retained
   if strcmp(get(findobj(H,'Tag','HoldButton'),'check'),'on') ...
         & strcmp(get(findobj(H,'Tag','HoldButton'),'enable'),'on'), % Hold is on
      set(H,'Userdata',[ud;udnew]);
   else
      set(H,'Userdata',udnew);
   end
   
case 'step',
   % Get time vector input
   [tvecs] = LocalMakeTimeVec('step',nsys,sys,ExtraArg,[],Ts);
   
   for ctsys=1:nsys,
      ud(ctsys).system=sys{ctsys};
      if isempty(sysname{ctsys})
         ud(ctsys).name = ['tempsys',num2str(ctsys)];
      else
         ud(ctsys).name = sysname{ctsys};
      end
      ud(ctsys).plottype = 1;
      if isempty(pstyle{ctsys}),
         ud(ctsys).plotstr = LocalSetPlotStr(ctsys);
      else,
         ud(ctsys).plotstr=pstyle{ctsys};
      end % if isempty
      [Y] = step(sys{ctsys},tvecs{ctsys});
      K = dcgain(sys{ctsys});
      ud(ctsys).step.time = tvecs{ctsys};
      ud(ctsys).step.response = Y;
      ud(ctsys).step.dcgain = K;
   end
   set(H,'UserData',ud);
   
   if ~isequal(get(CallbackFig,'Tag'),'ResponseGUI'),  
      LocalUpdateGUI(H,ud)
   end
   rguifcn('plotfinish');
   
case 'impulse'
   % Get time vector input
   [tvecs]=LocalMakeTimeVec('impulse',nsys,sys,ExtraArg,[],Ts);
   
   for ctsys=1:nsys,
      ud(ctsys).system=sys{ctsys};
      if isempty(sysname{ctsys})
         ud(ctsys).name=['tempsys',num2str(ctsys)];
      else
         ud(ctsys).name=sysname{ctsys};
      end
      ud(ctsys).plottype=2;
      if isempty(pstyle{ctsys}),
         ud(ctsys).plotstr=LocalSetPlotStr(ctsys);
      else,
         ud(ctsys).plotstr=pstyle{ctsys};
      end % if isempty
      [Y]=impulse(sys{ctsys},tvecs{ctsys});
      ud(ctsys).impulse.time=tvecs{ctsys};
      ud(ctsys).impulse.response=Y;
   end
   set(H,'UserData',ud);
   
   if ~isequal(get(CallbackFig,'Tag'),'ResponseGUI'), 
      LocalUpdateGUI(H,ud)
   end
   rguifcn('plotfinish');
   
case {'lsim','initial'}
   if strcmp(Property,'lsim'),
      plottype=8;
     % Get time vector input
     U=ExtraArg{1};
     [tvecs]=LocalMakeTimeVec('lsim',nsys,sys,ExtraArg(2),[],Ts);
     X0=ExtraArg{3};
   elseif strcmp(Property,'initial'),
      plottype=9;
      % Get time vector input
      X0=ExtraArg{1};
     [tvecs]=LocalMakeTimeVec('initial',nsys,sys,ExtraArg(2),X0',Ts);
   end
      
   for ctsys=1:nsys,
      ud(ctsys).system=sys{ctsys};
      if isempty(sysname{ctsys})
         ud(ctsys).name=['tempsys',num2str(ctsys)];
      else
         ud(ctsys).name=sysname{ctsys};
      end
      ud(ctsys).plottype=plottype;
      if isempty(pstyle{ctsys}),
         ud(ctsys).plotstr=LocalSetPlotStr(ctsys);
      else,
         ud(ctsys).plotstr=pstyle{ctsys};
      end % if isempty
      if strcmp(Property,'lsim'),
         [Y]=lsim(sys{ctsys},U,tvecs{ctsys},X0);
      else
         [Y]=initial(sys{ctsys},X0,tvecs{ctsys});
      end
      ud(ctsys).step.time=tvecs{ctsys};
      ud(ctsys).step.response=Y;
   end
   set(H,'UserData',ud);
   
   if ~isequal(get(CallbackFig,'Tag'),'ResponseGUI'), 
      LocalUpdateGUI(H,ud)
   end
   rguifcn('plotfinish'); 
   
case 'bode'
   % Get time vector input
   [wvecs]=LocalMakeFreqVec('bode',nsys,sys,ExtraArg{1});
   
   for ctsys=1:nsys,
      ud(ctsys).system=sys{ctsys};
      if isempty(sysname{ctsys})
         ud(ctsys).name=['tempsys',num2str(ctsys)];
      else
         ud(ctsys).name=sysname{ctsys};
      end
      ud(ctsys).plottype=3;
      if isempty(pstyle{ctsys}),
         ud(ctsys).plotstr=LocalSetPlotStr(ctsys);
      else,
         ud(ctsys).plotstr=pstyle{ctsys};
      end % if isempty
      [mag,ph]=bode(sys{ctsys},wvecs{ctsys});
      ud(ctsys).bode.frequency=wvecs{ctsys};
      ud(ctsys).bode.magnitude=mag;
      ud(ctsys).bode.phase=ph;
   end
   set(H,'UserData',ud);
   
   if ~isequal(get(CallbackFig,'Tag'),'ResponseGUI'), 
      LocalUpdateGUI(H,ud)
   end
   rguifcn('plotfinish');
   
 case 'margin'
   %---In the case of margin...the input can not be multi system 
   %---No GUIs will be available
   
   frequency=varargin{1};
   magnitude=varargin{2};
   phase=varargin{3};
   plotstr=[];
   
   if strcmp(get(H,'Tag'),'ResponseGUI'), % Store the response in the figure's userdata
      ud(1).bode.frequency=varargin{1};
      ud(1).bode.magnitude=varargin{2};
      ud(1).bode.phase=varargin{3};
      ud(1).plottype=10; 
      set(H,'UserData',ud);
      
      rguifcn('plotfinish');
      
   else, % Just plot the response
      
      BackAx=get(H,'CurrentAxes');
      ltiplot('margin',[],BackAx,[{magnitude},{phase}],frequency,plotstr)
      
   end	% if/else strcmp('Tag'...)
   
case 'nyquist'
   % Get time vector input
   [wvecs]=LocalMakeFreqVec('nyquist',nsys,sys,ExtraArg{1});
   
   for ctsys=1:nsys,
      ud(ctsys).system=sys{ctsys};
      if isempty(sysname{ctsys})
         ud(ctsys).name=['tempsys',num2str(ctsys)];
      else
         ud(ctsys).name=sysname{ctsys};
      end
      ud(ctsys).plottype=4;
      if isempty(pstyle{ctsys}),
         ud(ctsys).plotstr=LocalSetPlotStr(ctsys);
      else,
         ud(ctsys).plotstr=pstyle{ctsys};
      end % if isempty
      [Re,Im]=nyquist(sys{ctsys},wvecs{ctsys});
      ud(ctsys).nyquist.frequency=wvecs{ctsys};
      ud(ctsys).nyquist.real=Re;
      ud(ctsys).nyquist.imaginary=Im;
   end
   set(H,'UserData',ud);
   
   if ~isequal(get(CallbackFig,'Tag'),'ResponseGUI'), 
      LocalUpdateGUI(H,ud)
   end
   rguifcn('plotfinish');
   
case 'nichols'
    % Get time vector input
   [wvecs]=LocalMakeFreqVec('nichols',nsys,sys,ExtraArg{1});
   
   for ctsys=1:nsys,
      ud(ctsys).system=sys{ctsys};
      if isempty(sysname{ctsys})
         ud(ctsys).name=['tempsys',num2str(ctsys)];
      else
         ud(ctsys).name=sysname{ctsys};
      end
      ud(ctsys).plottype=5;
      if isempty(pstyle{ctsys}),
         ud(ctsys).plotstr=LocalSetPlotStr(ctsys);
      else,
         ud(ctsys).plotstr=pstyle{ctsys};
      end % if isempty
      [mag,ph]=nichols(sys{ctsys},wvecs{ctsys});
      ud(ctsys).nichols.frequency=wvecs{ctsys};
      ud(ctsys).nichols.magnitude=mag;
      ud(ctsys).nichols.phase=ph;
   end
   set(H,'UserData',ud);
   
   if ~isequal(get(CallbackFig,'Tag'),'ResponseGUI'), 
      LocalUpdateGUI(H,ud)
   end
   rguifcn('plotfinish');
   
case 'sigma'
    % Get time vector input
    [wvecs]=LocalMakeFreqVec('sigma',nsys,sys,ExtraArg{1});
    type=[];
    if length(ExtraArg)>1,
       type=ExtraArg{2};
    end
    if isempty(type),
       type=0;
    end
    
   for ctsys=1:nsys,
      ud(ctsys).system=sys{ctsys};
      if isempty(sysname{ctsys})
         ud(ctsys).name=['tempsys',num2str(ctsys)];
      else
         ud(ctsys).name=sysname{ctsys};
      end
      ud(ctsys).plottype=6;
      if isempty(pstyle{ctsys}),
         ud(ctsys).plotstr=LocalSetPlotStr(ctsys);
      else,
         ud(ctsys).plotstr=pstyle{ctsys};
      end % if isempty
      [Sv,w]=sigma(sys{ctsys},wvecs{ctsys},type);
      ud(ctsys).sigma.frequency=wvecs{ctsys};
      ud(ctsys).sigma.values=Sv;
   end
   set(H,'UserData',ud);
   
   if ~isequal(get(CallbackFig,'Tag'),'ResponseGUI'), 
      LocalUpdateGUI(H,ud)
   end
   rguifcn('plotfinish');
   
case 'size'
   PropVal=lower(varargin{1});
   OrigUnit=get(H,'Unit');
   
   switch PropVal
      
   case 'tall'
      
      pos = get(H,'Position');
      
      %---Find out how much room the Viewer controls need
      set(findobj(H,'Tag','ZoomText'),'Unit','point');
      posnew = get(findobj(H,'Tag','ZoomText'),'Position');
      set(findobj(H,'Tag','ZoomText'),'Unit','norm');
      udzoom = get(findobj(H,'Tag','ZoomText'),'UserData');
      posdiff = udzoom(1) + (posnew(2) - udzoom(2));
            
      %---Determine if the Viewer controls go beyond the screen size
      OrigRootUnit = get(0,'unit');
      set(0,'unit','points');
      ScreenSize = get(0,'ScreenSize');
      set(0,'units',OrigRootUnit)
      DiffHeight = ScreenSize(4) - (pos(4) + posdiff) - 75;
      
      %---If the "tall" figure is too large for the screen, resize
      if DiffHeight<0,
         pos(4) = pos(4) + (0.605*DiffHeight);
         set(H,'position',pos);
         set(findobj(H,'Tag','ZoomText'),'Unit','point');
         posnew = get(findobj(H,'Tag','ZoomText'),'Position');
         set(findobj(H,'Tag','ZoomText'),'Unit','norm');
         udzoom = get(findobj(H,'Tag','ZoomText'),'UserData');
         posdiff = udzoom(1) + (posnew(2) - udzoom(2));
      end
      
      %---Get location of Viewer
      pos(2) = max([pos(2) - posdiff,20]);
      pos(4) = pos(4) + posdiff;
      
      %---Resize figure
      set(findobj(H,'Tag','BackgroundAxes'),'unit','point');
      set(findobj(H,'Tag','LTIdisplayAxes'),'unit','point');
      set(H,'position',pos);
      
      %---Relocate the axes that will remain
      BackPos=get(findobj(H,'Tag','BackgroundAxes'),'position');
      BackPos(2) = BackPos(2) + posdiff;
            
      set(findobj(H,'Tag','BackgroundAxes'),'Position',BackPos);
      TempAx=findobj(H,'Tag','LTIdisplayAxes');

      if ~isempty(TempAx),
         udDispAx=get(TempAx(1),'Userdata');
         DispAx=udDispAx.Siblings;
         [numrow,numcol] = size(DispAx);
         if (numrow~=0) & (numcol~=0);
            if DiffHeight>0,
               DiffHeightDispAx=DiffHeight/numrow;
            else
               DiffHeightDispAx=0;
            end
            for ctrow=1:numrow,
               for ctcol=1:numcol
                  DispPos=get(DispAx(ctrow,ctcol),'Position');
                  DispPos(2)=DispPos(2)+posdiff;
                  set(DispAx(ctrow,ctcol),'Position',DispPos);
               end % for ctcol
            end % for ctrow
         end
      end % if ~isempty(TempAx)
      set(findobj(H,'Tag','BackgroundAxes'),'unit','norm');
      set(findobj(H,'Tag','LTIdisplayAxes'),'unit','norm');
            
      %---Set the GUIs to visible-make sure they reflect any changes in the response plot
      set(findobj(H,'type','uicontrol'),'visible','on');
      set(findobj(H,'style','checkbox'),'visible','off');      
      set(findobj(H,'tag','PlotCheck'),'visible','on');      
      LocalDispOpts(get(findobj(H,'Tag','PlotPopup'),'Value'));
      set(findobj(H,'Tag','ZoomAxes'),'visible','on');
      set(findobj(H,'Tag','ZoomFcn'),'visible','on');
      set(get(findobj(H,'Tag','ZoomFcn'),'children'),'visible','on');
      
      %---only turn the selector on for MIMO objects
      ud=get(H,'UserData');
      if ~isempty(ud),
         [numin,numout]=size(ud(1).system);
         plottype=ud(1).plottype;
         if (numin~=1) | (numout~=1)
            switch plottype
            case {1 2 3 4 5}
               set(get(findobj(H,'Tag','selector'),'children'),'visible','on');
               set(findobj(H,'Tag','selector'),'visible','on');
            end
         end
      end
         
      %----Add the current workspace to the listbox
      rguifcn('systemlist'); % initialize the workspace list
      
   %--------turning UI's off
   case 'short'
      set(findobj(H,'Tag','BackgroundAxes'),'unit','point');
      set(findobj(H,'Tag','LTIdisplayAxes'),'unit','point');
      FigPos = get(H,'position');
      BackPos = get(findobj(H,'Tag','BackgroundAxes'),'position');
      
      %---Set the GUIs to invisible
      set(findobj(H,'type','uicontrol'),'visible','off');
      set(get(findobj(H,'Tag','selector'),'children'),'visible','off');
      set(findobj(H,'Tag','selector'),'visible','off');
      set(findobj(H,'Tag','ZoomFcn'),'visible','off');
      set(get(findobj(H,'Tag','ZoomFcn'),'children'),'visible','off');
      set(findobj(H,'Tag','ZoomAxes'),'visible','off');
      LocalDispOpts(0);
      
      %---Relocate the axes and controls that will remain
      set(findobj(H,'Tag','ZoomText'),'Unit','point');
      postemp = get(findobj(H,'Tag','ZoomText'),'Position');
      posdiff = postemp(2);
      set(findobj(H,'Tag','ZoomText'),'Unit','norm');
      FigPos(2)=FigPos(2)+posdiff;
      FigPos(4)=FigPos(4)-posdiff;
      set(H,'Position',FigPos);
      
      BackPos(2)=BackPos(2)-posdiff;
      set(findobj(H,'Tag','BackgroundAxes'),'Position',BackPos);
      DispAx=findobj(H,'Tag','LTIdisplayAxes');
      for ctdisp=1:length(DispAx),
         DispPos=get(DispAx(ctdisp),'Position');
         DispPos(2)=DispPos(2)-posdiff;
         set(DispAx(ctdisp),'Position',DispPos);
      end % for ctdisp
      
      set(findobj(H,'Tag','BackgroundAxes'),'unit','norm')
      set(findobj(H,'Tag','LTIdisplayAxes'),'unit','norm');
      set(findobj(H,'Tag','ZoomText'),'Unit','point');
      posnew = get(findobj(H,'Tag','ZoomText'),'Position');
      set(findobj(H,'Tag','ZoomText'),'Unit','norm', ...
         'UserData',[postemp(2),posnew(2)]);
      
      drawnow
   end % switch PropVal
end % switch Property

%%%%%%%%%%%%%%%%%
%% initstructs %%
%%%%%%%%%%%%%%%%%

%---To accomodate the new structs format

function s = initstructs(siz,f)
s = cell2struct(cell([length(f) siz]),f,1);

%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% LocalDispOpts  %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%
function LocalDispOpts(plottype);

H=gcf;

% If there are no LTIresponseLines, do not turn any options on
DispAx = findobj(H,'Tag','LTIresponseLines');
if isempty(DispAx),
   return
end

% Turn all options off, then turn correct ones on,
set(findobj(H,'Tag','TimePeakCheck'),'visible','off');
set(findobj(H,'Tag','SetCheck'),'visible','off');
set(findobj(H,'Tag','RiseCheck'),'visible','off');
set(findobj(H,'Tag','DcCheck'),'visible','off');
set(findobj(H,'Tag','MarginCheck'),'visible','off');
set(findobj(H,'Tag','FreqPeakCheck'),'visible','off');

switch plottype
   
case {1,2}   % Time Domain 
   set(findobj(H,'Tag','TimePeakCheck'),'visible','on','UserData',[]);
   set(findobj(H,'Tag','SetCheck'),'visible','on','UserData',[]);
   
   if plottype==1,
      set(findobj(H,'Tag','RiseCheck'),'visible','on','UserData',[]);
      set(findobj(H,'Tag','DcCheck'),'visible','on','UserData',[]);
   end
   
case {3,4,5} % Freq. Domain
   set(findobj(H,'Tag','MarginCheck'),'visible','on','UserData',[]);
   if plottype==3,
      set(findobj(H,'Tag','FreqPeakCheck'),'visible','on','UserData',[]);
   end
case 6  % sigma
   set(findobj(H,'Tag','FreqPeakCheck'),'visible','on','UserData',[]);
   
end % switch plottype

%%%%%%%%%%%%%%%%%%%%%
%% LocalSetPlotStr %%
%%%%%%%%%%%%%%%%%%%%%
function plotstr=LocalSetPlotStr(ctsys)

legendlist=['r';'b';'g';'c';'m';'y';'k'];
ll=size(legendlist,1);
cl=ctsys-(floor(ctsys/ll)*ll);
if ~cl, cl=1; end,
plotstr=deblank(legendlist(cl,:));	

%%%%%%%%%%%%%%%%%%%%%%
%%% LocalUpdateGUI %%%
%%%%%%%%%%%%%%%%%%%%%%
function LocalUpdateGUI(H,ud)

SystemBox=findobj(H,'Tag','SystemList');
PlotPopup=findobj(H,'Tag','PlotPopup');
ActiveBox=findobj(H,'Tag','SystemSelector');
systemname=cell(size(ud,1),1);
AllSystems=get(SystemBox,'String');
PlotInd=get(ActiveBox,'UserData');

if isempty(PlotInd),
   PlotInd = ones(size(ud))'; % PlotInd should be a row vector
end

s=0;
plottype=ud(1).plottype;

ListVal=[];
for ct=1:size(ud,1),
   [LL,CC,MM,msg] = colstyle(ud(ct).plotstr);
   legendstr='';
   
   if ~isempty(CC)
      Cabbrev=[{'r'};{'b'};{'g'};{'c'};{'m'};{'y'};{'k'}];
      indCmat=strmatch(CC,Cabbrev);
      Cmat=[{'red'};{'blue'};{'green'};{'cyan'};{'magenta'};{'yellow'};{'black'}];
      legendstr=Cmat{indCmat};
   end
   if ~isempty(LL),
      if strcmp(LL,'-'); LLlong='solid';
      elseif strcmp(LL,'--'); LLlong='dashed';
      elseif strcmp(LL,':'); LLlong='dotted';
      elseif strcmp(LL,'-.'); LLlong='dash-dot';end;
      
      if ~isempty(legendstr),
         legendstr=[legendstr,',',LLlong];
      else
         legendstr=LLlong;
      end
   end
   if ~isempty(MM),
      if ~isempty(legendstr),
         legendstr=[legendstr,',',MM];
      else
         legendstr=MM;
      end
   end    
   if PlotInd(ct)
      systemname(ct)={['+ ',ud(ct).name,' (',legendstr,')']};
   else
      systemname(ct)={[ud(ct).name,' (',legendstr,')']};
   end      
   indtemp=strmatch(ud(ct).name,AllSystems,'exact');
end % for ct

%---Disable plot configuration options for SISOsystems
[numin,numout]=size(ud(1).system);
if (numin==1) & (numout==1),
   set(findobj(H,'Tag','ArrayButton'),'enable','off');
   set(findobj(H,'Tag','SingleButton'),'enable','off');
   set(findobj(H,'Tag','InButton'),'enable','off');
   set(findobj(H,'Tag','OutButton'),'enable','off');  
else
   set(findobj(H,'Tag','ArrayButton'),'enable','on');
   set(findobj(H,'Tag','SingleButton'),'enable','on');
   set(findobj(H,'Tag','InButton'),'enable','on');
   set(findobj(H,'Tag','OutButton'),'enable','on');  
end

rguifcn('makepatches');

set(ActiveBox,'String',systemname,'UserData',PlotInd,'Value',[]);

if ~isempty(plottype),
   set(PlotPopup,'value',plottype,'UserData',plottype);
end

%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% LocalMakeTimeVec %%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%
function [tvecs]=LocalMakeTimeVec(plottype,nsys,sys,tcell,x0,Ts);
t=tcell{1};
tfinal=[];
if isempty(t) & isempty(tfinal),
   tvecs = trange(plottype,tfinal,x0,sys{1:nsys});
   
else
   if isequal(size(t),[1 1]),  
      % Input T is the final time
      tfinal = t;    t = [];  
   end
   if tfinal<=0,  error('TFINAL must be positive.'),  end
   
   tvecs = cell(1,nsys);   t0 = 0;
   if isempty(t), 
      if any(Ts==-1),
         if ~isequal(tfinal,round(tfinal)),
            error(...
               'Final time must be an integer (# of samples) when sample time is unspecified.');
         else
            tfinal=tfinal-1;
         end
      end
      % Generate appropriate time vector for each system
      tvecs = trange(plottype,tfinal,x0,sys{1:nsys});
      
   else
      % Time vector is supplied: override if not evenly spaced
      t0 = t(1);       % to handle case T(1) is not zero
      dt = t(2)-t(1);
      t = dt * (0:1:floor((1+10*eps)*t(end)/dt));
      if any(abs(Ts(Ts>0)-dt) > 1e-4*dt),
         error('Spacing of time samples T should match sample period of discrete systems.')
      end
      tvecs(1:nsys) = {t(:)};
   end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% LocalMakeFreqVec %%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%
function [wgrids]=LocalMakeFreqVec(plottype,nsys,sys,w);

npts = 50;   % min. number of points to be used
NoW = 1;     % 1 if no W vector supplied
if isempty(w),
   % No W input argument: generate freq. grids for each system
   wgrids = fgrid(plottype,[],[],npts,sys{1:nsys});
 
elseif isa(w,'cell'),
   % W = {WMIN , WMAX}
   w = w(:);
   if ndims(w)>2 | length(w)>2,
      error('W should be of the form {WMIN,WMAX} when a cell array.')
   end
   wmin = w{1};
   wmax = w{2};
   wmin = wmin(1);   wmax = wmax(1);
 
   if ~isa(wmin,'double') | ~isa(wmax,'double'),
      error('WMIN and WMAX must be scalars in syntax BODE(SYS,{WMIN,WMAX}).')
   elseif wmin<=0 | wmax<=wmin,
      error('WMIN and WMAX must satisfy 0<WMIN<WMAX.')
   end
   wgrids = fgrid(plottype,wmin,wmax,npts,sys{1:nsys});
 
elseif isa(w,'double') & ndims(w)==2 & min(size(w))==1,
   % Vector of frequency W supplied
   wgrids = cell(1,nsys);
   wgrids(1:nsys) = {w(:)};
   NoW = 0;
   
end
