function ViewerObj = setstyle(ViewerObj)
%SETSTYLE  Resolves undefined plot styles for response plots.
% 
%    ViewerObj = SETSTYLE(ViewerObj) resolves undefined (empty) plot styles 
%    by cycling through either colors, markers, or linestyles according to
%    the value of ViewerObj.SystemPlotVariable.  
%
%    For each system, the plot style is described by a structure with fields  
%    Color, LineStyle, Marker, and Legend.  The field Color contains an RGB 
%    triplet and Legend is the corresponding legend string.  

%    P. Gahinet and K. Gondoly, 5-99
%    Copyright 1986-2000 The MathWorks, Inc. 
%    $Revision: 1.4 $  $Date: 2000/06/14 20:03:16 $

Cvals = ViewerObj.ColorOrder; 
numC = size(Cvals,1);
Lvals = ViewerObj.LineStyleOrder(:);
numL = length(Lvals);
Mvals = ViewerObj.MarkerOrder(:);
numM = length(Mvals);

% Find unspecified plot styles
PlotStyles = ViewerObj.PlotStyles;
UnDefStyle = cellfun('isempty',{PlotStyles.Color});

% Put unused styles upfront to avoid duplicate styles
if any(UnDefStyle) & any(~UnDefStyle)
   switch ViewerObj.SystemPlotVariable
   case 'color'
      Cvals = LocalReorder(Cvals,cat(1,PlotStyles(~UnDefStyle).Color));
   case 'linestyle'
      Lvals = LocalReorder(Lvals,{PlotStyles(~UnDefStyle).LineStyle});
   case 'marker'
      Mvals = LocalReorder(Mvals,{PlotStyles(~UnDefStyle).Marker});
   end
end

% Resolve unspecified plot styles
UnDefStyle = find(UnDefStyle);
for i=1:length(UnDefStyle),
   UnDefRank = i-1;
   isys = UnDefStyle(i);
   switch ViewerObj.SystemPlotVariable,
   case 'color',
      [PlotStyles(isys).Color,PlotStyles(isys).Legend] = deal(Cvals{rem(UnDefRank,numC)+1,:});
      PlotStyles(isys).LineStyle = Lvals{1};
      PlotStyles(isys).Marker = Mvals{1};
    case 'marker'
      PlotStyles(isys).Color = Cvals{1,1};
      PlotStyles(isys).LineStyle = Lvals{1};
      MM = Mvals{rem(UnDefRank,numM)+1};
      PlotStyles(isys).Marker = MM;
      PlotStyles(isys).Legend = MM;
   case 'linestyle',
      PlotStyles(isys).Color = Cvals{1,1};
      LL = Lvals{rem(UnDefRank,numL)+1};
      PlotStyles(isys).Marker = Mvals{1};
      AllLines = {'solid';'dashed';'dash-dot';'dotted'};
      PlotStyles(isys).LineStyle = LL;
      PlotStyles(isys).Legend = AllLines{strcmpi(LL,{'-';'--';'-.';':'})};
   case 'none'
      PlotStyles(isys).Color = Cvals{1};
      PlotStyles(isys).LineStyle = Lvals{1};
      PlotStyles(isys).Marker = Mvals{1};
      PlotStyles(isys).Legend = 'N/A';
   end
end

% Update plot style structure
ViewerObj.PlotStyles = PlotStyles;


%----------------- Local functions --------------------

function List = LocalReorder(List,UsedStyles)

if ~isempty(UsedStyles)
   if isa(UsedStyles,'cell')
      % Linestyle or marker
      InUse = ismember(List,UsedStyles);
   else
      % Colors
      nc = size(List,1);
      InUse = logical(zeros(nc,1));
      for i=1:nc
         RGBColor = List{i,1};
         InUse(i) = any(max(abs(UsedStyles - RGBColor(ones(size(UsedStyles,1),1),:)),[],2) < 0.05);
      end
   end
   
   List = [List(~InUse,:) ; List(InUse,:)];
end

