function varargout = rltool(varargin);
%RLTOOL opens the Root Locus Design GUI
%   RLTOOL opens the Root Locus Design GUI.  This GUI allows you to 
%   design a single-input/single-output (SISO) compensator using Root 
%   Locus techniques.  A plant model can be imported into the GUI by  
%   selecting the Import Model item from the File menu.
%
%   RLTOOL(SYS) opens a Root Locus Design GUI using the plant model in 
%   SYS.  SYS must be a SISO LTI model.  When opened with one input  
%   argument, the compensator is initialized as a unity gain.
%
%   RLTOOL(SYS,COMP) initializes the compensator with the SISO LTI 
%   model COMP.
%
%   When one or two input arguments are provided, the root locus of the 
%   closed-loop poles and their locations for the current compensator 
%   gain are drawn.  The closed-loop model has the following default 
%   structure: 
%
%             u --->O--->[ COMP ]--->[ SYS ]----+---> y
%                 - |                           |
%                   +---------------------------+
%
%   The following syntax allows you to override the default closed-loop 
%   structure.
%
%   RLTOOL(SYS,COMP,LocationFlag,FeedbackSign) opens a Root Locus Design 
%   GUI where the location of the compensator and the feedback sign are 
%   specified as follows:
%
%       LocationFlag = 1: Places the compensator in the forward loop
%       LocationFlag = 2: Places the compensator in the feedback loop
%       
%       FeedbackSign = -1: Negative feedback
%       FeedbackSign =  1: Positive feedback

%   Additional Comments:
%   
%   A LocationFlag of 0 turns the feedback structure toggle feature on. 
%   In this mode, the user may switch the location of the compensator during 
%   the design session. This is not consistant with how the GUI will function 
%   when it is called from CODA.
%
%   RLTOOL(ModelData,CompData,Parent) where Model/CompData are structured 
%   arrays and Parent is the handle of a CODA interface is used to open the
%   Root Locus Design GUI from CODA

%   Karen D. Gondoly
%   Copyright (c) 1986-98 by The MathWorks, Inc.
% $Revision: 1.10 $

ni=nargin;

ToggleFlag=1; % Turn off ability to flip through feedback structures

if ~ni,
   ParentFig=0;
   sys=zpk(1);
   sysname='sys';
   ModelData = struct('Name',sysname,'Structure',1,'FeedbackSign',-1,...
      'Plant',struct('Name','P','Object',sys),...
      'Sensor',struct('Name','H','Object',zpk(1)),...
      'Filter',struct('Name','F','Object',zpk(1)));
end

if ni>=1, % Some model has been entered
   if isstruct(varargin{1}), % Design Model from CODA
      ModelData = varargin{1};
      error(nargchk(1,3,ni)); % Must have three input arguments when called from CODA
      ParentFig = varargin{3};
      if ~ishandle(ParentFig),
         error('The third input argument must be a valid figure handle when called from CODA.')
      end
   else
      ParentFig = 0; % Opened from Command Line
      sysname=inputname(1);
      if isempty(sysname)
         sysname = 'sys';
      end
      sys=varargin{1};
      
      if (~isa(sys,'lti') & ~isa(sys,'double')) | isa(sys,'frd'),
         error('The model must be a TF, ZPK, or SS model, or a scalar.');
      elseif isa(sys,'double') & isempty(sys)
         sys=zpk(1);
      elseif isa(sys,'double') & ~isempty(sys),
         sys=zpk(sys);
      elseif hasdelay(sys),
         if sys.Ts,
            % Map delay times to poles at z=0 in discrete-time case
            sys = delay2z(sys);
         else
            error('Not supported for continuous-time delay systems.')
         end      
      end
      
      %---Make sure the model is SISO and has no gains of zero
      [z,p,k]=zpkdata(sys);
      PlantSizes = size(k);
      if prod(PlantSizes(1:2))~=1,
         error('The plant model must be single input, single output.')
      elseif prod(PlantSizes(3:end))~=1
         error('Cannot be used with LTI arrays.');
      elseif ~all(k(:)),
         error('Cannot be used with LTI models having zero gain.');
      end
      
      ModelData = struct('Name',sysname,'Structure',1,'FeedbackSign',-1,...
         'Plant',struct('Name',sysname,'Object',sys),...
         'Sensor',struct('Name','H','Object',zpk(1)),...
         'Filter',struct('Name','F','Object',zpk(1)));
   end % if/else isstruct
end % if ni>=1

if ni>=2,
   if isstruct(varargin{2}), % Compensator from CODA
      CompData = varargin{2};
   else
      comp=varargin{2};
      compname=inputname(2);
      if isempty(compname);
         compname='comp';
      end
      
      %---Make sure the compensator is SISO
      if ~isa(comp,'lti') & ~isa(comp,'double'),
         error('The compensator must be an LTI object or scalar');
      elseif isa(comp,'double') & isempty(comp)
         comp=1;
      elseif hasdelay(comp),
         if comp.Ts,
            % Map delay times to poles at z=0 in discrete-time case
            comp= delay2z(comp);
         else
            error('Not supported for continuous-time delay systems.')
         end      
      end
      
      comp = zpk(comp);
      CompSizes = size(comp);
      Ts=[comp.Ts,sys.Ts];            
      if prod(CompSizes(1:2))~=1,
         error('The compensator model must be single input, single output.')
      elseif prod(CompSizes(3:end))~=1
         error('Cannot be used with LTI arrays.');
      elseif all(Ts) & any(diff(Ts(Ts>0))),
         error('The plant and compensator must have the same sample time');
      elseif any(Ts==0) & any(Ts~=0)
         error('The plant and compensator must both be either continuous or discrete');
      end
      
      compgain=comp.k;
      comp.k=1;
      CompData = struct('Name',compname,'Object',comp,'Gain',compgain);
   end % if/else isstruct(varargin{2})
else
   CompData = struct('Name','comp','Object',zpk([],[],1,sys.Ts),'Gain',1);
end

if ni>=3 & ~isempty(varargin{3}) & ~ParentFig, % Only when called from Command Line
   if ~any(varargin{3}==[0 1 2]);
      error('The third input must select a feedback structure using an index of 1 or 2')
   else,
      ToggleFlag=varargin{3};
      if ToggleFlag,
         ModelData.Structure = ToggleFlag;
      end   
   end
end

if ni==4 & ~isempty(varargin{4}),
   if ~any(varargin{4}==[-1 1]),
      error('The fourth input argument must be a 1 or -1, indicating the feedback sign')
   else
      ModelData.FeedbackSign = varargin{4};
   end
end
         
StdColor = get(0,'DefaultUicontrolBackgroundColor');
StdUnit='points';
PointsToPixels = 72/get(0,'ScreenPixelsPerInch');

a = figure('Color',[0.8 0.8 0.8], ...
   'IntegerHandle','off', ...
   'Interruptible','off', ...
   'BusyAction','cancel', ...
   'DeleteFcn','rlfcn(''closekids'');', ...
   'HandleVis','Callback', ...
	'MenuBar','none', ...
	'Name',prepender('Root Locus Design'), ...
	'NumberTitle','off', ...
   'Visible','off', ...
   'Position',[67 30 350 420], ...
   'ResizeFcn','rlfcn(''resize'');', ...
   'Tag','RootLocusDesignFig', ...
   'WindowButtonDownFcn','rlfcn(''windowbuttondown'');', ...
   'WindowButtonMotionFcn','rlfcn(''windowbuttonmotion'');', ...
   'WindowButtonUpFcn','rlfcn(''windowbuttonup'');');

if nargout,
   varargout{1}=a;
end

%---Setup the figure UserData
%----Parent indicates where the GUI was called from:
%               0: Command Line
%               FigureHandle = handle of CODA Master
%
%----Currently, for Feedback.Configuration: 1 = K in forward loop
%                                           2 = K in feedback loop
%                   Feedback.Sign: 1=positive; -1=negative

day = date;
UdRL = struct('Figure',...
   					struct('Parent',ParentFig, ...
   					'Children',[],...
   					'Pointer',struct('Style','arrow','Object',[]),...
   					'ButtonDown',struct('Point',[],'Type',[])), ...
   			  'Model',ModelData, ...
   			  'Compensator',CompData, ...
   			  'ClosedLoopModel',struct('Name','clsys','Object',[]), ...
  				  'History',[],'Handles',[]);

%---Set up Structure for the LTIdisplayAxes UserData
udAx = struct('Locus',[],'ClosedLoopPoles',[],...
   'FilterPoles',[], ...
   'Model',struct('Poles',[],'Zeros',[]), ...
   'Compensator',struct('Poles',[],'Zeros',[]), ...
   'Limit',struct('X',[-1 1],'Y',[-1 1]), ...
   'Preferences',struct('X',[],'Y',[],'Equal',0,'Square',0, ...
   'Model','b','Compensator','r','ClosedLoop','rs'), ...
   'Grid',struct('State',0,'Value',1), ...
   'Constraints',struct('Damping',struct('State',0,'Value',[]),...
   'NaturalFrequency',struct('State',0,'Value',[]),...
   'SettlingTime',struct('State',0,'Value',[])), ...
   'MoveData',struct('MoveType',[],'MoveHandles',[], ...
   'GainVector',[],'LocusSystem',struct('Z',[],'P',[],'K',1)));

UdRL.Handles.LTIdisplayAxes = axes('Parent',a, ...
	'Units','points', ...
	'CameraUpVector',[0 1 0], ...
	'CameraUpVectorMode','manual', ...
   'Color',[1 1 1], ...
   'DrawMode','fast', ...
   'Position',PointsToPixels*[25 102 308 239], ...
   'Tag','LTIdisplayAxes', ...
   'UserData',udAx, ...
   'XColor',[.7 .7 .7], ...
   'Xlim',[-1,1], ...
   'YColor',[0.7 0.7 0.7], ...
   'Ylim',[-1 1],...
	'ZColor',[0.7 0.7 0.7]);

%---Add title/labels so they are available for printing purposes
set(get(UdRL.Handles.LTIdisplayAxes,'xlabel'),...
   'String','Real Axis','visible','off');
set(get(UdRL.Handles.LTIdisplayAxes,'ylabel'),...
   'String','Imag Axes','visible','off');
set(get(UdRL.Handles.LTIdisplayAxes,'title'),...
   'String','Root Locus Design','visible','off');

%---Draw axes lines
UdRL.Handles.XaxisLine = line([-1 1],[0 0],'Color',[.7 .7 .7],'LineStyle',':', ...
   'Parent',UdRL.Handles.LTIdisplayAxes,'Tag','XaxisLine');
UdRL.Handles.YaxisLine = line([0 0],[-1 1],'Color',[.7 .7 .7],'LineStyle',':', ...
   'Parent',UdRL.Handles.LTIdisplayAxes,'Tag','YaxisLine');

%-Add All the ButtonGroups
[default,addpole,addzero,erase,setpref,getpref,squarepref,equalpref]=LocalMakeIcons;

%---Add Add/Erase/Default controls
UdRL.Handles.PZAxes=uicontrol('parent',a, ...
   'Style','frame',...
   'Backgroundcolor',StdColor, ...
   'tag','PZAxes', ...
   'unit',StdUnit, ...
   'HitTest','off',...
   'Position',PointsToPixels*[25 343 105 26]);   

UdRL.Handles.DefaultLocusFcn= uicontrol(a,...
   'Style','toggleButton',...
   'Unit',StdUnit',...
   'Tag','DefaultButton',...
   'HitTest','off',...
   'Value',1,...
   'Callback','rlfcn(''default'');',...
   'ToolTipStr','Drag poles/zeros',...
   'Position',PointsToPixels*[28 344.7 25 24],...
   'Cdata',default);

UdRL.Handles.AddPoleButton= uicontrol(a,...
   'Style','toggleButton',...
   'Unit',StdUnit',...
   'Tag','AddPoleButton',...
   'Callback','rlfcn(''togglecallback'');',...
   'ToolTipStr','Add poles',...
   'Position',PointsToPixels*[53 344.7 25 24],...
   'Cdata',addpole);
UdRL.Handles.AddZeroButton= uicontrol(a,...
   'Style','toggleButton',...
   'Unit',StdUnit',...
   'Tag','AddZeroButton',...
   'Callback','rlfcn(''togglecallback'');',...
   'ToolTipStr','Add zeros',...
   'Position',PointsToPixels*[78 344.7 25 24],...
   'Cdata',addzero);
UdRL.Handles.EraseButton= uicontrol(a,...
   'Style','toggleButton',...
   'Unit',StdUnit',...
   'Tag','EraseButton',...
   'Callback','rlfcn(''togglecallback'');',...
   'ToolTipStr','Erase poles/zeros',...
   'Position',PointsToPixels*[103 344.7 25 24],...
   'Cdata',erase);

set(UdRL.Handles.DefaultLocusFcn,'UserData',...
   [UdRL.Handles.AddPoleButton,UdRL.Handles.AddZeroButton,UdRL.Handles.EraseButton]);
set(UdRL.Handles.AddPoleButton,'UserData',...
   [UdRL.Handles.DefaultLocusFcn,UdRL.Handles.AddZeroButton,UdRL.Handles.EraseButton]);
set(UdRL.Handles.AddZeroButton,'UserData',...
   [UdRL.Handles.AddPoleButton,UdRL.Handles.DefaultLocusFcn,UdRL.Handles.EraseButton]);
set(UdRL.Handles.EraseButton,'UserData',...
   [UdRL.Handles.AddPoleButton,UdRL.Handles.DefaultLocusFcn,UdRL.Handles.AddZeroButton]);

%---Add Zoom buttons
[XYicon,Xicon,Yicon,FVicon]=zoomicons;
UdRL.Handles.ZoomAxes=uicontrol('parent',a, ...
   'style','frame',...
   'backgroundcolor',StdColor, ...
   'tag','ZoomAxes', ...
   'unit',StdUnit, ...
   'Position',PointsToPixels*[192 60 143 25]);   
UdRL.Handles.ZoomText = uicontrol(a,...
   'Style','text',...
   'backgroundcolor',StdColor, ...
   'Unit',StdUnit',...
   'FontSize',8,...
   'String','Zoom:',...
   'horiz','left',...
   'Position',PointsToPixels*[198 63 40 15]);
UdRL.Handles.XYzoomButton = uicontrol(a,...
   'Style','toggleButton',...
   'Unit',StdUnit',...
   'Tag','XYzoomButton',...
   'Callback','rlfcn(''zoomcallback'');',...
   'ToolTipStr','Zoom in X-Y',...
   'Position',PointsToPixels*[241 61 23 23],...
   'Cdata',XYicon);
UdRL.Handles.XzoomButton = uicontrol(a,...
   'Style','toggleButton',...
   'Unit',StdUnit',...
   'Tag','XzoomButton',...
   'Callback','rlfcn(''zoomcallback'');',...
   'ToolTipStr','Zoom in-X',...
   'Position',PointsToPixels*[264 61 23 23],...
   'Cdata',Xicon);
UdRL.Handles.YzoomButton = uicontrol(a,...
   'Style','toggleButton',...
   'Unit',StdUnit',...
   'Tag','YzoomButton',...
   'Callback','rlfcn(''zoomcallback'');',...
   'ToolTipStr','Zoom in-Y',...
   'Position',PointsToPixels*[287 61 23 23],...
   'Cdata',Yicon);
UdRL.Handles.FullViewButton = uicontrol(a,...
   'Style','toggleButton',...
   'Unit',StdUnit',...
   'Tag','FullViewButton',...
   'Callback','rlfcn(''unzoom'');',...
   'ToolTipStr','Full view',...
   'Position',PointsToPixels*[310 61 23 23],...
   'Cdata',FVicon);

set(UdRL.Handles.XYzoomButton,'UserData',...
   [UdRL.Handles.XzoomButton,UdRL.Handles.YzoomButton,UdRL.Handles.FullViewButton]);
set(UdRL.Handles.XzoomButton,'UserData',...
   [UdRL.Handles.XYzoomButton,UdRL.Handles.YzoomButton,UdRL.Handles.FullViewButton]);
set(UdRL.Handles.YzoomButton,'UserData',...
   [UdRL.Handles.XzoomButton,UdRL.Handles.XYzoomButton,UdRL.Handles.FullViewButton]);
set(UdRL.Handles.FullViewButton,'UserData',...
   [UdRL.Handles.XzoomButton,UdRL.Handles.YzoomButton,UdRL.Handles.XYzoomButton]);

%---Add Axes settings controls
UdRL.Handles.PrefAxes=uicontrol('parent',a, ...
   'style','frame',...
   'backgroundcolor',StdColor, ...
   'Enable', 'inactive', ...
   'tag','PrefAxes', ...
   'unit',StdUnit, ...
   'Position',PointsToPixels*[14 60 175 25]);   
UdRL.Handles.AxesText=uicontrol('parent',a, ...
   'style','text',...
   'backgroundcolor',StdColor, ...
   'buttondownfcn','rlfcn(''setaxes'');',...
   'Enable', 'inactive', ...
   'unit',StdUnit,...
   'FontSize',8,...
   'String','Axes settings:',...
   'horiz','left',...
   'Position',PointsToPixels*[18 63 125 15]);
UdRL.Handles.AxesSet = uicontrol(a,...
   'Style','toggleButton',...
   'Unit',StdUnit',...
   'Tag','AxesSetButton',...
   'ToolTipStr','Store axes limits',...
   'Callback','rlfcn(''savepref'');',...  
   'Position',PointsToPixels*[95 61 23 23],...
   'Cdata',setpref);
UdRL.Handles.AxesRetrieve = uicontrol(a,...
   'Style','toggleButton',...
   'Tag','AxesRetrieveButton',...
   'Unit',StdUnit',...
   'Callback','rlfcn(''restorepref'');',...  
   'ToolTipStr','Retrieve axes limits',...
   'Position',PointsToPixels*[118 61 23 23],...
   'Cdata',getpref);
UdRL.Handles.AxesSquare = uicontrol(a,...
   'Style','toggleButton',...
   'Unit',StdUnit',...
   'Tag','AxesSquareButton',...
   'ToolTipStr','Make axes square',...
   'Callback','rlfcn(''axissquare'');',...  
   'Position',PointsToPixels*[141 61 23 23],...
   'Cdata',squarepref);
UdRL.Handles.AxesEqual = uicontrol(a,...
   'Style','toggleButton',...
   'Tag','AxesEqualButton',...
   'Unit',StdUnit',...
   'Callback','rlfcn(''axisequal'');',...  
   'ToolTipStr','Use equal axes aspect ratios',...
   'Position',PointsToPixels*[164 61 23 23],...
   'Cdata',equalpref);

%---Add gain controls

UdRL.Handles.GainFrame = uicontrol('Parent',a, ...
	'Units','points', ...
	'BackgroundColor',StdColor, ...
	'Position',PointsToPixels*[140 343 122 24], ...
	'Style','frame');
UdRL.Handles.GainText = uicontrol('Parent',a, ...
	'Units','points', ...
	'BackgroundColor',StdColor, ...
	'Position',PointsToPixels*[141 346 37 16], ...
	'String','Gain:', ...
	'Style','text');
UdRL.Handles.GainEdit = uicontrol('Parent',a, ...
	'Units','points', ...
   'BackgroundColor',[1 1 1], ...
   'Horiz','left', ...
	'Position',PointsToPixels*[179 346 80 20], ...
   'Style','edit', ...
   'String','1', ...
   'TooltipString','Compensator gain',...
   'UserData','1', ...
   'Callback','rlfcn(''ChangeGain'');', ...
   'Tag','GainEdit');

%---Add grid controls
UdRL.Handles.GridFrame = uicontrol('Parent',a, ...
	'Units','points', ...
	'BackgroundColor',StdColor, ...
	'Position',PointsToPixels*[270 343 62 23], ...
	'Style','frame');
UdRL.Handles.GridBox = uicontrol('Parent',a, ...
	'BackgroundColor',StdColor, ...
	'Units','points', ...
   'Horiz','left', ...
	'Position',PointsToPixels*[273 345 56 19], ...
   'Style','checkbox', ...
   'TooltipString','Add/Delete grid',...
   'Value',0, ...
   'String','Grid', ...
   'Callback','rlfcn(''showgrid'');', ...
   'Tag','GridBox');

UdRL.Handles.ConfigurationAxes = axes('Parent',a, ...
	'Units','points', ...
	'Box','on', ...
	'CameraUpVector',[0 1 0], ...
	'Color',[1 1 1], ...
	'Position',PointsToPixels*[208 370 124 46], ...
	'Tag','ConfigurationAxes', ...
	'XColor',[0 0 0], ...
	'XTickMode','manual', ...
	'YColor',[0 0 0], ...
	'YTickMode','manual', ...
   'ZColor',[0 0 0]);

if ~ToggleFlag % feedback structure can be toggled
   set(UdRL.Handles.ConfigurationAxes,'ButtonDown','rlfcn(''config'');')
   UdRL.Handles.ChangeConfig = uicontrol('Parent',a, ...
      'Units','points', ...
      'Position',PointsToPixels*[311 370 20 15], ...
      'String','-->', ...
      'UserData',1, ...
      'Callback','rlfcn(''ChangeConfig'');', ...
      'Tag','ConfigButton');
end

UdRL.Handles.ChangeSign = uicontrol('Parent',a, ...
	'Units','points', ...
	'Position',PointsToPixels*[208 370 20 15], ...
   'String','+/-', ...
   'TooltipString','Toggle feedback sign',...
   'Callback','rlfcn(''ChangeSign'');', ...
   'Tag','SignButton');

%---Set up default configuration number 1
Handles = loopstruct('drawconfig',UdRL.Handles.ConfigurationAxes,ModelData.Structure);
UdRL.Handles.Configuration = Handles;

%---Set ButtonDownFcn's of the configuration
set([Handles.PlantPatch,Handles.SensorPatch,Handles.FilterPatch,...
   Handles.PlantText,Handles.SensorText,Handles.FilterText],...
   'ButtonDownFcn','rlfcn(''showplant'');');
set([Handles.CompPatch,Handles.CompText],'ButtonDownFcn','rlfcn(''editcomp'');');

if ModelData.FeedbackSign>0,
   SignText='+';
else
   SignText='-';
end

UdRL.Handles.Configuration.SignText = text(.24, .4,SignText,'Parent',UdRL.Handles.ConfigurationAxes, ...
   'Tag','SignText','UserData',ModelData.FeedbackSign,'FontWeight','bold');

set(UdRL.Handles.ConfigurationAxes,'Ylim',[0 1],'Xlim',[.0 .9]);
   
%---Add Current Compensator Text
UdRL.Handles.CompensatorFrame = uicontrol('Parent',a, ...
   'ButtonDownFcn','rlfcn(''editcomp'');', ...
   'Units','points', ...
   'Enable','inactive',...
	'Position',PointsToPixels*[24 370 176 46], ...
	'Style','frame');
UdRL.Handles.CompensatorText = uicontrol('Parent',a, ...
	'Units','points', ...
   'FontSize',8, ...
   'Enable','inactive',...
   'ButtonDownFcn','rlfcn(''editcomp'');', ...
	'Position',PointsToPixels*[25 401 173 12], ...
	'String','Current Compensator', ...
	'Style','text');
UdRL.Handles.GcText = uicontrol('Parent',a, ...
   'Enable','inactive',...
   'Units','points', ...
   'ButtonDownFcn','rlfcn(''editcomp'');', ...
   'Position',PointsToPixels*[30 377 23 14], ...
	'String','K =', ...
	'Style','text');
UdRL.Handles.DenText = uicontrol('Parent',a, ...
   'Enable','inactive',...
   'FontSize',8, ...
	'Units','points', ...
   'ButtonDownFcn','rlfcn(''editcomp'');', ...
	'Position',PointsToPixels*[52 371 145 12], ...
   'String','1', ...
   'Max',1, ...
   'Min',1, ...
	'Style','text', ...
	'Tag','DenText');
UdRL.Handles.FractionText = uicontrol('Parent',a, ...
   'Enable','inactive',...
   'FontSize',8, ...
	'Units','points', ...
   'ButtonDownFcn','rlfcn(''editcomp'');', ...
   'Position',PointsToPixels*[52 382 145 9], ...
   'String','-----------------------------------', ...
	'Style','text', ...
	'Tag','FractionText');
UdRL.Handles.NumText = uicontrol('Parent',a, ...
   'Enable','inactive',...
   'FontSize',8, ...
	'Units','points', ...
   'ButtonDownFcn','rlfcn(''editcomp'');', ...
	'Position',PointsToPixels*[52 387 145 12], ...
	'String','Gain', ...
   'Style','text', ...
   'Max',1, ...
   'Min',1, ...
	'Tag','NumText');

%---Add Response Buttons
UdRL.Handles.ResponseFrame = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'Style','frame', ...
   'Position',PointsToPixels*[14 34 320 24]);
UdRL.Handles.StepButton = uicontrol('Parent',a, ...
	'Units',StdUnit, ...
	'BackgroundColor',StdColor, ...
   'Callback','rlfcn(''view'');', ...
   'FontSize',8, ...
   'Interruptible','off',...
	'Position',PointsToPixels*[18 37 56 19], ...
   'String','Step', ...
   'TooltipString','Closed-loop step response',...
	'Style','checkbox', ...
   'Tag','StepButton');
UdRL.Handles.ImpulseButton = uicontrol('Parent',a, ...
	'Units',StdUnit, ...
	'BackgroundColor',StdColor, ...
   'Callback','rlfcn(''view'');', ...
   'FontSize',8, ...
   'Interruptible','off',...
   'Position',PointsToPixels*[82 37 56 19], ...
	'String','Impulse', ...
	'Style','checkbox', ...
   'TooltipString','Closed-loop impulse response',...
   'Tag','ImpulseButton');
UdRL.Handles.BodeButton = uicontrol('Parent',a, ...
	'Units',StdUnit, ...
	'BackgroundColor',StdColor, ...
   'Callback','rlfcn(''view'');', ...
   'FontSize',8, ...
   'Interruptible','off',...
	'Position',PointsToPixels*[146 37 56 19], ...
	'String','Bode', ...
	'Style','checkbox', ...
   'TooltipString','Open-loop Bode diagram',...
   'Tag','BodeButton');
UdRL.Handles.NyquistButton = uicontrol('Parent',a, ...
	'Units',StdUnit, ...
	'BackgroundColor',StdColor, ...
   'Callback','rlfcn(''view'');', ...
   'FontSize',8, ...
   'Interruptible','off',...
	'Position',PointsToPixels*[210 37 56 19], ...
	'String','Nyquist', ...
   'TooltipString','Open-loop Nyquist diagram',...
	'Style','checkbox', ...
   'Tag','NyquistButton');
UdRL.Handles.NicholsButton = uicontrol('Parent',a, ...
	'Units',StdUnit, ...
	'BackgroundColor',StdColor, ...
   'Callback','rlfcn(''view'');', ...
   'Interruptible','off',...
	'Position',PointsToPixels*[274 37 56 19], ...
   'String','Nichols', ...
   'TooltipString','Open-loop Nichols chart',...
   'FontSize',8, ...
	'Style','checkbox', ...
   'Tag','NicholsButton');

%---Add Status Text
UdRL.Handles.StatusFrame = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'Style','frame', ...
   'Position',PointsToPixels*[14 3 321 29]);

UdRL.Handles.StatusText = uicontrol('Parent',a,...
   'Units',StdUnit, ...
   'FontSize',8, ...
   'Style','text', ...
   'Horiz','left', ...
   'TooltipString','Status Bar',...
   'Position',PointsToPixels*[18 5 311 25],...
   'Tag','StatusText');

UdRL.Handles.Menus.File.Top= uimenu('Parent',a, ...
   'Label','File');

if ~ParentFig,
   UdRL.Handles.Menus.File.ImportModel = uimenu('Parent',...
      UdRL.Handles.Menus.File.Top,...
      'Label','Import Model...',...
      'Accelerator','I',...
      'Callback','rlfcn(''importmodel'',gcbf);',...
      'Tag','ImportModel');
   UdRL.Handles.Menus.File.ImportComp = uimenu('Parent',...
      UdRL.Handles.Menus.File.Top,...
      'Label','Import Compensator...',...
      'Callback','rlfcn(''importcomp'',gcbf);',...
      'Tag','ImportComp');
   
   UdRL.Handles.Menus.File.Export = uimenu('Parent',UdRL.Handles.Menus.File.Top, ...
      'Label','Export...', ...
      'Accelerator','E', ...
      'Callback','rlfcn(''export'',gcbf)', ...
      'Tag','ExportMenu');
   
else,
   UdRL.Handles.Menus.File.SendComp = uimenu('Parent',UdRL.Handles.Menus.File.Top, ...
      'Accelerator','S', ...
      'Label','Send Compensator to CODA', ...
      'Tag','SendMenu', ...
      'Callback','rlfcn(''sendcomp'');');
end

UdRL.Handles.Menus.File.Draw = uimenu('Parent',UdRL.Handles.Menus.File.Top, ...
   'Label','Draw Simulink Diagram...', ...
   'Separator','on', ...
   'Callback','rlfcn(''drawdiagram'')', ...
   'Tag','DrawMenu');
UdRL.Handles.Menus.File.History = uimenu('Parent',UdRL.Handles.Menus.File.Top, ...
   'Label','Display History...',...
   'Callback','rlfcn(''history'');', ...
   'Tag','HistoryMenu');
UdRL.Handles.Menus.File.Print = uimenu('Parent',UdRL.Handles.Menus.File.Top, ...
   'Label','Print Locus...',...
   'Callback','rlfcn(''print'');', ...
   'Separator','on', ...
   'Accelerator','P', ...
   'Tag','PrintMenu');
UdRL.Handles.Menus.File.Figure = uimenu('Parent',UdRL.Handles.Menus.File.Top, ...
   'Label','Send Locus to Figure...',...
   'Callback','rlfcn(''print'');', ...
   'Tag','FigureMenu');
UdRL.Handles.Menus.File.Close = uimenu('Parent',UdRL.Handles.Menus.File.Top, ...
   'Callback','rlfcn(''close'',gcbf);', ...
   'Separator','on', ...
   'Label','Exit Design', ...
   'Accelerator','X', ...
   'Tag','CloseMenu');

UdRL.Handles.Menus.Options.Top= uimenu('Parent',a, ...
	'Label','Tools');

if ~ToggleFlag, % If feedback structure can be toggled
   UdRL.Handles.Menus.Options.ConfigureLoop = uimenu('Parent',UdRL.Handles.Menus.Options.Top, ...
      'Label','Change Feedback Structure...', ...
      'Enable','on', ...
      'Accelerator','L', ...
      'Callback','rlfcn(''config'');', ...
      'Tag','ConfigMenu');
end

UdRL.Handles.Menus.Options.EditComp = uimenu('Parent',UdRL.Handles.Menus.Options.Top, ...
   'Label','Edit Compensator...', ...
   'CallBack','rlfcn(''editcomp'');', ...
   'Tag','EditComp');

UdRL.Handles.Menus.Options.Show = uimenu('Parent',UdRL.Handles.Menus.Options.Top, ...
   'Label','List Model Poles/Zeros...', ...
   'Enable','off',...
   'Separator','on', ...
   'CallBack','rlfcn(''showplant'');', ...
   'Tag','ShowMenu');      
UdRL.Handles.Menus.Options.ShowClosedLoop = uimenu('Parent',UdRL.Handles.Menus.Options.Top, ...
   'Label','List Closed-loop Poles...',...
   'Enable','on',...
   'Tag','ClPolesMenu',...
   'Callback','rlfcn(''showclpoles'');');
UdRL.Handles.Menus.Options.Convert = uimenu('Parent',UdRL.Handles.Menus.Options.Top, ...
   'Label','Convert Model/Compensator...', ...
   'Separator','on',...
   'Enable','off',...
   'CallBack','rlfcn(''discretize'');', ...
   'Tag','DiscretizeMenu');
UdRL.Handles.Menus.Options.Clear = uimenu('Parent',UdRL.Handles.Menus.Options.Top, ...
   'Label','Clear Model', ...
   'Enable','off',...
   'CallBack','rlfcn(''clearmodel'');', ...
   'Tag','ClearMenu');      
UdRL.Handles.Menus.Options.ClearComp = uimenu('Parent',UdRL.Handles.Menus.Options.Top, ...
   'Label','Clear Compensator', ...
   'Enable','off',...
   'CallBack','rlfcn(''clearcomp'');', ...
   'Tag','ClearCompMenu');      
UdRL.Handles.Menus.Options.Constraints = uimenu('Parent',UdRL.Handles.Menus.Options.Top, ...
   'Label','Add Grid/Boundary...', ...
   'Separator','on', ...
   'Accelerator','G', ...
   'Callback','rlfcn(''grid'');', ...
   'Tag','GridMenu');
UdRL.Handles.Menus.Options.Preferences = uimenu('Parent',UdRL.Handles.Menus.Options.Top, ...
   'Label','Set Axes Preferences...', ...
   'Enable','on', ...
   'Callback','rlfcn(''setaxes'');', ...
   'Tag','PrefMenu');

UdRL.Handles.Menus.Window.Top = uimenu(a, 'Label', 'Window', ...
   'Callback', winmenu('callback'), 'Tag', 'winmenu');
winmenu(a);  % Initialize the submenu
     
UdRL.Handles.Menus.Help.Top = uimenu('Parent',a, ...
   'Label','Help');
UdRL.Handles.Menus.Help.Main = uimenu('Parent',UdRL.Handles.Menus.Help.Top, ...
   'Label','Main Help', ...
   'Callback','rlhelp(''main'');',...
   'Accelerator','H');
if ~ToggleFlag,
   UdRL.Handles.Menus.Help.ConfigureLoop = uimenu('Parent',UdRL.Handles.Menus.Help.Top, ...
      'Label','Change Feedback Structure...', ...
      'Separator','on', ...
      'Callback','rlhelp(''configure'');');
   SeparatorState='off';
else
   SeparatorState='on';
end
UdRL.Handles.Menus.Help.EditComp = uimenu('Parent',UdRL.Handles.Menus.Help.Top, ...
   'Label','Edit Compensator...', ...
   'Separator',SeparatorState,...
   'CallBack','rlhelp(''editcomp'');');
UdRL.Handles.Menus.Help.Convert = uimenu('Parent',UdRL.Handles.Menus.Help.Top, ...
   'Label','Convert Model...', ...
   'CallBack','rlhelp(''discretize'');');
UdRL.Handles.Menus.Help.Constraints = uimenu('Parent',UdRL.Handles.Menus.Help.Top, ...
   'Label','Add Grid/Boundary...', ...
   'Separator','on', ...
   'Callback','rlhelp(''grid'');');
UdRL.Handles.Menus.Help.Preferences = uimenu('Parent',UdRL.Handles.Menus.Help.Top, ...
   'Label','Set Axis Preferences...', ...
   'Callback','rlhelp(''axes'');');

%---Add the UIcontrol handles and History to the UserData
UdRL.History = {[day,': Initialize RL Design for model: ',sysname]};
set(a,'UserData',UdRL)

if ni,
   rlfcn('finishimport',a,UdRL);     
   rlfcn('unzoom',a)
end

set(UdRL.Handles.StatusText,'String','Ready')
set(a,'visible','on')
refresh(a)

%%%%%%%%%%%%%%%%%%%%%%
%%% LocalMakeIcons %%%
%%%%%%%%%%%%%%%%%%%%%%
function [default,addpole,addzero,erase,setpref,getpref,squarepref,equalpref] = ...
   LocalMakeIcons;

cm = [0 0 0;
     0.502 0 0;
         0    0.5020         0;
    0.5020    0.5020         0;
         0         0    0.5020;
    0.5020         0    0.5020;
         0    0.5020    0.5020;
    0.7529    0.7529    0.7529;
    0.5020    0.5020    0.5020;
    1.0000         0         0;
         0    1.0000         0;
    1.0000    1.0000         0;
         0         0    1.0000;
    1.0000         0    1.0000;
         0    1.0000    1.0000;
    1.0000    1.0000    1.0000;
    0.4000    0.4000    0.4000;
    0.4267    0.4267    0.4267;
    0.4533    0.4533    0.4533;
    0.4800    0.4800    0.4800;
    0.5067    0.5067    0.5067;
    0.5333    0.5333    0.5333;
    0.5600    0.5600    0.5600;
    0.7529    0.7529    0.7529;
    0.6133    0.6133    0.6133;
    0.6400    0.6400    0.6400;
    0.6667    0.6667    0.6667;
    0.6933    0.6933    0.6933;
    0.7200    0.7200    0.7200;
    0.7467    0.7467    0.7467;
    0.7733    0.7733    0.7733;
    0.8000    0.8000    0.8000;
    1         1         1;
    0.85      0.85       0.85];
 
 d =  [...
    32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 25
    32 24 24 24 24 1 24 24 24 24 24 24 24 24 24 24 24 24 24 17
    32 24 24 24 24 1 1 24 24 24 24 24 24 24 24 24 24 24 24 17
    32 24 24 24 24 1 33 1 24 24 24 24 24 24 24 24 24 24 24 17
    32 24 24 24 24 1 33 33 1 24 24 24 24 24 24 24 24 24 24 17
    32 24 24 24 24 1 33 33 33 1 24 24 24 24 24 24 24 24 24 17
    32 24 24 24 24 1 33 33 33 33 1 24 24 24 24 24 24 24 24 17
    32 24 24 24 24 1 33 33 33 33 33 1 24 24 24 24 24 24 24 17
    32 24 24 24 24 1 33 33 33 33 33 33 1 24 24 24 24 24 24 17
    32 24 24 24 24 1 33 33 33 33 33 33 33 1 24 24 24 24 24 17
    32 24 24 24 24 1 33 33 33 33 33 33 33 33 1 24 24 24 24 17
    32 24 24 24 24 1 33 33 33 33 33 1 1 1 1 24 24 24 24 17
    32 24 24 24 24 1 33 33 1 33 33 1 24 24 24 24 24 24 24 17
    32 24 24 24 24 1 33 1 24 1 33 33 1 24 24 24 24 24 24 17
    32 24 24 24 24 1 1 24 24 1 33 33 1 24 24 24 24 24 24 17
    32 24 24 24 24 24 24 24 24 24 1 33 33 1 24 24 24 24 24 17
    32 24 24 24 24 24 24 24 24 24 1 33 33 1 24 24 24 24 24 17
    32 24 24 24 24 24 24 24 24 24 24 1 1 1 24 24 24 24 24 17
    32 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 17
    25 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17];
    
 default=LocalInd2RGB(d,cm); 
    
 p=[...
    32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 25
    32 24 10 24 24 24 10 24 24 24 24 24 24 24 24 24 24 24 24 17
    32 24 24 10 24 10 24 24 24 24 24 24 24 24 24 24 24 24 24 17
    32 24 24 24 10 24 24 24 24 24 24 24 24 24 24 24 24 24 24 17
    32 24 24 10 24 10 24 24 1 24 24 24 24 24 24 24 24 24 24 17
    32 24 10 24 24 24 10 24 1 1 24 24 24 24 24 24 24 24 24 17
    32 24 24 24 24 24 24 24 1 33 1 24 24 24 24 24 24 24 24 17
    32 24 24 24 24 24 24 24 1 33 33 1 24 24 24 24 24 24 24 17
    32 24 24 24 24 24 24 24 1 33 33 33 1 24 24 24 24 24 24 17
    32 24 24 24 24 24 24 24 1 33 33 33 33 1 24 24 24 24 24 17
    32 24 24 24 24 24 24 24 1 33 33 33 33 33 1 24 24 24 24 17
    32 24 24 24 24 24 24 24 1 33 33 33 33 33 33 1 24 24 24 17
    32 24 24 24 24 24 24 24 1 33 33 33 33 1 1 1 24 24 24 17
    32 24 24 24 24 24 24 24 1 33 1 33 33 1 24 24 24 24 24 17
    32 24 24 24 24 24 24 24 1 1 24 1 33 1 24 24 24 24 24 17
    32 24 24 24 24 24 24 24 1 24 24 24 1 33 1 24 24 24 24 17
    32 24 24 24 24 24 24 24 24 24 24 24 1 33 1 24 24 24 24 17
    32 24 24 24 24 24 24 24 24 24 24 24 24 1 33 1 24 24 24 17
    32 24 24 24 24 24 24 24 24 24 24 24 24 1 1 1 24 24 24 17
    25 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17];
      
addpole=LocalInd2RGB(p,cm);
      
 z=[...
    32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 25
    32 24 24 10 10 10 24 24 24 24 24 24 24 24 24 24 24 24 24 17
    32 24 10 24 24 24 10 24 24 24 24 24 24 24 24 24 24 24 24 17
    32 24 10 24 24 24 10 24 24 24 24 24 24 24 24 24 24 24 24 17
    32 24 10 24 24 24 10 24 1 24 24 24 24 24 24 24 24 24 24 17
    32 24 24 10 10 10 24 24 1 1 24 24 24 24 24 24 24 24 24 17
    32 24 24 24 24 24 24 24 1 33 1 24 24 24 24 24 24 24 24 17
    32 24 24 24 24 24 24 24 1 33 33 1 24 24 24 24 24 24 24 17
    32 24 24 24 24 24 24 24 1 33 33 33 1 24 24 24 24 24 24 17
    32 24 24 24 24 24 24 24 1 33 33 33 33 1 24 24 24 24 24 17
    32 24 24 24 24 24 24 24 1 33 33 33 33 33 1 24 24 24 24 17
    32 24 24 24 24 24 24 24 1 33 33 33 33 33 33 1 24 24 24 17
    32 24 24 24 24 24 24 24 1 33 33 33 33 1 1 1 24 24 24 17
    32 24 24 24 24 24 24 24 1 33 1 33 33 1 24 24 24 24 24 17
    32 24 24 24 24 24 24 24 1 1 24 1 33 1 24 24 24 24 24 17
    32 24 24 24 24 24 24 24 1 24 24 24 1 33 1 24 24 24 24 17
    32 24 24 24 24 24 24 24 24 24 24 24 1 33 1 24 24 24 24 17
    32 24 24 24 24 24 24 24 24 24 24 24 24 1 33 1 24 24 24 17
    32 24 24 24 24 24 24 24 24 24 24 24 24 1 1 1 24 24 24 17
    25 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17];
         
addzero=LocalInd2RGB(z,cm);

e= [...
    32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 25
    32 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 17
    32 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 17
    32 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 17
    32 24 1 1 1 1 1 1 1 24 24 24 24 24 24 24 24 24 24 17
    32 24 1 1 33 33 33 33 33 1 24 24 24 24 24 24 24 24 24 17
    32 24 1 33 1 33 33 33 33 33 1 24 24 24 24 24 24 24 24 17
    32 24 1 33 33 1 33 33 33 33 33 1 24 24 24 24 24 24 24 17
    32 24 24 1 33 33 1 33 33 33 33 33 1 24 24 24 24 24 24 17
    32 24 24 24 1 33 33 1 33 33 33 33 33 1 24 24 24 24 24 17
    32 24 24 24 24 1 33 33 1 33 33 33 33 33 1 24 24 24 24 17
    32 24 24 24 24 24 1 33 33 1 33 33 33 33 33 1 24 24 24 17
    32 24 24 24 24 24 24 1 33 33 1 33 33 33 33 33 1 24 24 17
    32 24 24 24 24 24 24 24 1 33 33 1 1 1 1 1 1 1 24 17
    32 24 24 24 24 24 24 24 24 1 33 1 33 33 33 33 33 1 24 17
    32 24 24 24 24 24 24 24 24 24 1 1 1 1 1 1 1 1 24 17
    32 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 17
    32 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 17
    32 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 17
    25 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17];
erase=LocalInd2RGB(e,cm);
   
   cp=[...
    32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 25
    32 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 17
    32 24 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 24 17
    32 24 21 24 24 24 24 18 18 18 18 18 18 18 24 24 24 21 24 17
    32 24 21 24 24 24 18 24 24 24 24 24 24 24 18 24 24 21 24 17
    32 24 21 21 21 24 18 24 24 24 24 24 24 24 18 24 24 21 24 17
    32 24 21 24 24 24 18 24 24 24 24 24 24 24 18 24 24 21 24 17
    32 24 21 21 21 24 18 24 24 24 24 24 18 18 18 18 18 21 24 17
    32 24 21 24 24 24 18 24 24 24 24 24 24 18 18 18 24 21 24 17
    32 24 21 21 21 24 18 24 24 24 24 24 24 24 18 24 24 21 24 17
    32 24 21 24 24 24 24 24 24 17 17 17 17 17 17 17 17 17 17 17
    32 24 21 21 21 24 24 24 24 17 32 32 32 32 32 32 32 32 17 17
    32 24 21 24 24 24 24 24 24 17 32 17 17 17 17 17 17 32 17 17
    32 24 21 21 21 24 24 24 24 17 32 17 32 32 32 32 17 32 17 17
    32 24 21 24 24 24 24 24 24 17 32 17 17 17 17 17 17 32 17 17
    32 24 21 24 21 24 21 24 21 17 32 32 32 32 32 32 32 32 17 17
    32 24 21 24 21 24 21 24 21 17 32 32 32 32 32 32 32 32 17 17
    32 24 21 21 21 21 21 21 21 17 32 32 32 32 32 32 32 32 17 17
    32 24 24 24 24 24 24 24 24 17 17 17 17 17 17 17 17 17 17 17
    25 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17];
 
 setpref=LocalInd2RGB(cp,cm); 
 
  cr=[...
    32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 25
    32 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 17
    32 24 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 24 17
    32 24 21 24 24 24 24 18 18 18 18 18 18 18 24 24 24 21 24 17
    32 24 21 24 24 24 18 24 24 24 24 24 24 24 18 24 24 21 24 17
    32 24 21 21 21 24 18 24 24 24 24 24 24 24 18 24 24 21 24 17
    32 24 21 24 24 24 18 24 24 24 24 24 24 24 18 24 24 21 24 17
    32 24 21 21 18 18 18 18 18 24 24 24 24 24 18 24 24 21 24 17
    32 24 21 24 24 18 18 18 24 24 24 24 24 24 18 24 24 21 24 17
    32 24 21 21 21 24 18 24 24 24 24 24 24 24 18 24 24 21 24 17
    32 24 21 24 24 24 24 24 24 17 17 17 17 17 17 17 17 17 17 17
    32 24 21 21 21 24 24 24 24 17 32 32 32 32 32 32 32 32 17 17
    32 24 21 24 24 24 24 24 24 17 32 17 17 17 17 17 17 32 17 17
    32 24 21 21 21 24 24 24 24 17 32 17 32 32 32 32 17 32 17 17
    32 24 21 24 24 24 24 24 24 17 32 17 17 17 17 17 17 32 17 17
    32 24 21 24 21 24 21 24 21 17 32 32 32 32 32 32 32 32 17 17
    32 24 21 24 21 24 21 24 21 17 32 32 32 32 32 32 32 32 17 17
    32 24 21 21 21 21 21 21 21 17 32 32 32 32 32 32 32 32 17 17
    32 24 24 24 24 24 24 24 24 17 17 17 17 17 17 17 17 17 17 17
    25 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17];
 
 getpref=LocalInd2RGB(cr,cm); 
 
 sq=[...
    32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 25
    32 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 17
    32 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 17
    32 24 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 24 17
    32 24 10 24 24 24 24 24 24 24 24 24 24 24 24 24 24 10 24 17
    32 24 10 24 24 24 24 24 24 24 24 24 24 24 24 24 24 10 24 17
    32 24 10 24 24 24 24 24 24 24 24 24 24 24 24 24 24 10 24 17
    32 24 10 24 24 24 24 18 18 18 18 18 18 24 24 24 24 10 24 17
    32 24 10 24 24 24 24 18 24 24 24 24 18 24 24 24 24 10 24 17
    32 24 10 24 24 24 24 18 24 24 24 24 18 24 24 24 24 10 24 17
    32 24 10 24 24 24 24 18 24 24 24 24 18 24 24 24 24 10 24 17
    32 24 10 24 24 24 24 18 24 24 24 24 18 24 24 24 24 10 24 17
    32 24 10 24 24 24 24 18 18 18 18 18 18 24 24 24 24 10 24 17
    32 24 10 24 24 24 24 24 24 24 24 24 24 24 24 24 24 10 24 17
    32 24 10 24 24 24 24 24 24 24 24 24 24 24 24 24 24 10 24 17
    32 24 10 24 24 24 24 24 24 24 24 24 24 24 24 24 24 10 24 17
    32 24 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 24 17
    32 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 17
    32 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 17
    25 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17];
 
squarepref=LocalInd2RGB(sq,cm); 

 eq=[...
   32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 25
   32 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 17
   32 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 17
   32 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 17
   32 24 24 24 24 24 24 24 24 24 24 24 24 24 24 1 24 24 24 17
   32 24 24 24 24 24 24 24 24 24 24 24 24 24 1 24 24 24 24 17
   32 24 24 18 18 18 18 18 24 24 24 24 24 1 24 24 24 24 24 17
   32 24 24 24 24 24 24 24 24 24 24 24 1 24 24 24 24 24 24 17
   32 24 24 24 24 24 24 24 24 24 24 1 24 24 24 24 24 24 24 17
   32 24 24 18 18 18 18 18 24 24 1 24 24 24 24 24 24 24 24 17
   32 24 24 24 24 24 24 24 24 1 24 24 24 24 24 24 10 24 24 17
   32 24 24 24 24 24 24 24 1 24 24 24 10 10 10 10 10 24 24 17
   32 24 24 24 24 24 24 1 24 24 24 24 24 24 10 24 24 24 24 17
   32 24 24 24 24 24 1 24 24 24 24 24 24 10 24 24 24 24 24 17
   32 24 24 24 24 1 24 24 24 24 24 24 10 10 10 10 10 24 24 17
   32 24 24 24 24 24 24 24 24 24 24 10 24 24 24 24 24 24 24 17
   32 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 17
   32 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 17
   32 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 17
   25 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17];
 
 equalpref=LocalInd2RGB(eq,cm); 

%%%%%%%%%%%%%%%%%%%%
%%% LocalInd2RGB %%%
%%%%%%%%%%%%%%%%%%%%
function rout = LocalInd2RGB(a,cm);

% Extract r,g,b components
r = zeros(size(a)); r(:) = cm(a,1);
g = zeros(size(a)); g(:) = cm(a,2);
b = zeros(size(a)); b(:) = cm(a,3);

rout = zeros([size(r),3]);
rout(:,:,1) = r;
rout(:,:,2) = g;
rout(:,:,3) = b;

