function sys = frd(varargin)
% Construct GENFRD/UFRD from GENMAT/UMAT

%   Copyright 2009-2013 The MathWorks, Inc.
ni = nargin;

% Dissect input list
PVStart = min([ni+1 find(cellfun(@(x) (ischar(x) || isStringScalar(x)),varargin),1)]);
DataInputs = PVStart-1;
if ~any(DataInputs==[2 3])
   ctrlMsgUtils.error('Control:general:InvalidSyntaxForCommand','frd','frd')
end

% Process numerical data
try
   freq = FRDModel.checkFrequencyVector(varargin{2});
   resp = varargin{1};
   if ~isa(resp,'StaticModel')
      % Note: RESP is required to have the correct size when a GENMAT
      resp = FRDModel.checkResponseData(resp,numel(freq));
   end
   % Sample time
   if DataInputs==3
      % Discrete FRD
      Ts = ltipack.utValidateTs(varargin{3});
   else
      Ts = 0;
   end
      
   % Turn blocks into UMAT/GENMAT
   resp = ltipack.matchType('',resp);
   
   % Construct GENFRD or UFRD model
   sys = frdModel_(resp,freq,Ts);
   
   % Process additional settings and validate system
   if PVStart<=ni
      sys = set(sys,varargin{PVStart:ni});
   end
catch ME
   throw(ME)
end

