function display(sys)
%DISPLAY   Pretty-print for LTI models.
%
%   DISPLAY(SYS) is invoked by typing SYS followed
%   by a carriage return.  DISPLAY produces a custom
%   display for each type of LTI model SYS.
%
%   See also LTIMODELS.

%   Author(s): S. Almy
%   Copyright 1986-2013 The MathWorks, Inc.

CWS = matlab.desktop.commandwindow.size;      % max number of char. per line
LineMax = round(.8*CWS(1));
Inames = sys.InputName;
Onames = sys.OutputName;
StaticFlag = isstatic(sys);
Data = sys.Data_;
FreqUnits = sys.FrequencyUnit;
TimeUnits = sys.TimeUnit;
% Conversion factor to display frequency vector in current units 
cf = funitconv('rad/TimeUnit',FreqUnits,TimeUnits);
% Tweak display string for frequency units
FreqUnits = strrep(FreqUnits,'TimeUnit',TimeUnits(1:end-1));
FreqUnits = strrep(FreqUnits,'rad/second','rad/s');  % optimize default

% Get system name
VarName = inputname(1);
if isempty(VarName)
   VarName = 'ans';
end

% Get number of models in array
[Ny,Nu] = iosize(sys);
ArraySizes = size(Data);
nsys = numel(Data);
if nsys>1
   % Construct sequence of indexing coordinates
   indices = zeros(nsys,length(ArraySizes));
   for k=1:length(ArraySizes)
      range = 1:ArraySizes(k);
      base = repmat(range,[prod(ArraySizes(1:k-1)) 1]);
      indices(:,k) = repmat(base(:),[nsys/numel(base) 1]);
   end
end

% Handle various cases
if Ny==0 || Nu==0 || isempty(sys)
   fprintf('\n%s =\n\n  %s\n\n',VarName,...
      getString(message('Control:ltiobject:DispFRD6')))   
elseif nsys==1
   % Single model
   fprintf('\n%s =\n',VarName)
   dispsys(Data,Inames,Onames,LineMax,'  ',TimeUnits,FreqUnits,cf)
   % Metadata
   dispGroup(sys);
   % System name and sample time
   dispTs(sys,StaticFlag);
   % Last line
   if StaticFlag
      MsgID = 'Control:ltiobject:DispGain';
   elseif Data.Ts==0
      MsgID = 'Control:ltiobject:DispFRD7';
   else
      MsgID = 'Control:ltiobject:DispFRD8';
   end
   fprintf('%s\n',getString(message(MsgID)))
else
   % FRD array
   G = sys.SamplingGrid;
   for k=1:nsys
      ltipack.dispHeader(VarName,indices(k,:),G)
      dispsys(Data(k),Inames,Onames,LineMax,'  ',TimeUnits,FreqUnits,cf)
   end
   % Metadata
   dispGroup(sys);
   % System name and sample time
   dispTs(sys,StaticFlag);
   % Last line
   ArrayDims = sprintf('%dx',ArraySizes);
   if StaticFlag
      MsgID = 'Control:ltiobject:DispGainArray';
   elseif Data(1).Ts==0
      MsgID = 'Control:ltiobject:DispFRD9';
   else
      MsgID = 'Control:ltiobject:DispFRD10';
   end
   fprintf('%s\n',getString(message(MsgID,ArrayDims(1:end-1))))
end

try
   showModelProperties(sys)
end
