function set(ViewerObj,varargin)
%SET  Set properties of LTI Viewer.
%
%   SET(VIEWEROBJ,'Property',VALUE)  sets the property of VIEWEROBJ specified
%   by the string 'Property' to the value VALUE.
%
%   SET(VIEWEROBJ,'Property1',Value1,'Property2',Value2,...) sets multiple 
%   Response Object property values with a single statement.
%
%   See also GET.

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

ni = nargin;
if ~isa(ViewerObj,'viewgui')
   % Call built-in SET. Handles calls like set(gcf,'user',ss)
   builtin('set',ViewerObj,varargin{:});
   return
elseif rem(ni-1,2)~=0
   error('Property/value pairs must come in even number.')
end

% Now left with SET(ViewerObj,'Prop1',Value1, ...)
name = inputname(1);
if isempty(name)
   error('First argument to SET must be a named variable.')
end

AllProps = fieldnames(ViewerObj);

for i=1:2:ni-1
   % Set each PV pair in turn
   % Find match for property name in property list
   try
      [Property,imatch] = pnmatch(varargin{i},AllProps);
   catch
      error(lasterr)
   end
   Value = varargin{i+1};
   if ischar(Value)
      Value = lower(Value);
   end

   switch lower(Property)
   case 'backgroundaxes'
      ViewerObj.BackgroundAxes = Value;

   case 'colororder'
      ViewerObj.ColorOrder = Value;

   case 'configurationwindow'
      Value=lower(Value);
      if isempty(strmatch(Value,{'on';'off'})) | length(strmatch(Value,{'on';'off'}))>1
         error(sprintf('Bad value for response property: %s.',Property))
      else
         ViewerObj.ConfigurationWindow = Value;
         configax(ViewerObj,Value);
      end

   case 'initialcondition'
      ViewerObj.InitialConditions= Value;

   case 'inputsignal'
      ViewerObj.InputSignal= Value;

   case 'linestyleorder'
      ViewerObj.LineStyleOrder = Value;

   case 'linestylepreferences'
      set(ViewerObj.Handle,'Pointer','watch');
      ViewerObj.LineStylePreferences = Value;
      PlotPrefFig = get(ViewerObj.FigureMenu.EditMenu.LineStyles,'UserData');
      switch lower(Value)
      case 'off'
         if ishandle(PlotPrefFig) & ~isequal(PlotPrefFig,0)
            close(PlotPrefFig);
         end
         set(ViewerObj.FigureMenu.EditMenu.LineStyles,'UserData',[]);
      case 'on'
         if ishandle(PlotPrefFig) & ~isequal(PlotPrefFig,0)
            if strcmpi(get(PlotPrefFig,'Visible'),'off')
               centerfig(PlotPrefFig,ViewerObj.Handle);
            end
            figure(PlotPrefFig);
         else
            PlotPrefFig = rguipopts('initialize',ViewerObj.Handle);
            set(ViewerObj.FigureMenu.EditMenu.LineStyles,'UserData',PlotPrefFig);
         end
      end
      set(ViewerObj.Handle,'Pointer','arrow');

   case 'markerorder'
      ViewerObj.MarkerOrder = Value;

   case 'plotstyles'
      ViewerObj.PlotStyles = Value;

   case 'activeplottypes'
      %---Changing the ActivePlotTypes will not recompute anything
      % 1) Reconfigure plots
      [ViewerObj,inew] = arrangeview(ViewerObj,Value);
      % 2) Populate new plots (compute desired response)
      ViewerObj = plotswitch(ViewerObj,[],inew);

   case 'systems'
      ViewerObj.Systems = Value;
      % REVISIT: hack to make new systems available to functions like CALCOPT/margins
      % which read system data off the figure UserData (caused improper update of 
      % stability margins)
      builtin('set',ViewerObj.Handle,'UserData',ViewerObj);
      ViewerObj = plotswitch(ViewerObj);

   case 'systemnames'
      % Change system names
      OldNames = ViewerObj.SystemNames;
      NewNames = Value(:);
      if length(OldNames)~=length(NewNames)
         error(sprintf('Bad value for response property %s.',Property))
      else
         ViewerObj.SystemNames = NewNames;
      end
      % Update each plot area
      uic = ViewerObj.UIContextMenu;
      for ctR=1:length(uic)
          RespObj = get(uic(ctR),'UserData');
          mFlag = ismember(OldNames,get(RespObj,'SystemNames'));
          set(RespObj,'SystemNames',NewNames(mFlag,:))
          builtin('set',uic(ctR),'UserData',RespObj)
      end

   case 'uicontextmenu'
      %---Need proper error handling
      ViewerObj.UIContextMenu = Value;

   otherwise
      error(sprintf('Attempt to modify read-only response property: %s.',Property))

   end
end

% Store updated ViewerObj in figure's UserData
builtin('set',ViewerObj.Handle,'UserData',ViewerObj);

% Finally, assign sys in caller's workspace
if ~isempty(name)
   assignin('caller',name,ViewerObj)
end


