function varargout = rguihelper(task,varargin)
%RGUIHELPER  Centralized helper function for Response GUIs.

%   P. Gahinet and Karen Gondoly
%   Copyright 1986-2000 The MathWorks, Inc. 
%   $Revision: 1.2 $ %Date$

switch lower(task)
case 'labelsystem'
   % Derives the system identifier shown when clicking on a line or 
   % response characteristic marker 
   % SystemStr = rguihelper('labelsystem',...
   %                   SystemName,Input,Output,ArrayLoc,IOSize,ArraySize,ResponseType)
   [SystemName,Input,Output,ArrayLoc,IOSize,ArraySize,ResponseType] = deal(varargin{1:7});
   NumModels = prod(ArraySize);
   if length(ArraySize)==2 & ArraySize(2)==1,
      ArrayLoc = ArrayLoc(1);
   end
   ArrayLocStr = sprintf(',%d',ArrayLoc); 
   
   switch ResponseType
   case {'sigma','pzmap','rlocus'}
      % Ignore I/O dimensions 
      if NumModels>1,
         SystemStr = sprintf('System: %s(:,:%s)',SystemName,ArrayLocStr);
      else
         SystemStr = sprintf('System: %s',SystemName);
      end
   case {'initial','lsim'}
      % Input dimension is irrelevant
      if NumModels>1, 
         SystemStr = sprintf('System: %s(%d,:%s)',SystemName,Output,ArrayLocStr);
      elseif IOSize(1)>1,
         % multi-output
         SystemStr = sprintf('System: %s(%d,:)',SystemName,Output);
      else
         SystemStr = sprintf('System: %s',SystemName);
      end
   otherwise
      % General case
      if prod(IOSize)>1, % MIMO
         if NumModels>1, % Array
            SystemStr = sprintf('System: %s(%d,%d%s)',SystemName,...
               Output,Input,ArrayLocStr);
         else
            SystemStr = sprintf('System: %s(%d,%d)',SystemName,Output,Input);
         end
      else
         if NumModels>1, % SISO Array
            SystemStr = sprintf('System: %s(:,:%s)',SystemName,ArrayLocStr);
         else % SISO, non-array
            SystemStr = sprintf('System: %s',SystemName);
         end
      end
   end
   
   varargout = {SystemStr};
   
case 'ind2sub'
   % Gets array subscripts given the absolute position in the LTI array
   % Subs = rguihelper('ind2sub',ArraySize,Index)
   [ArraySize,Index] = deal(varargin{1:2});
   
   % Use IND2SUB to compute vector of subscripts
   Subs = cell(1,length(ArraySize));
   [Subs{:}] = ind2sub(ArraySize,Index);
   
   varargout = {[Subs{:}]};
   
end
