function sys = fcat(varargin)
% FCAT  Concatenates FRD models along the frequency dimension. 
% 
%    SYS = FCAT(SYS1,SYS2,...) takes two or more FRD models and merges their 
%    frequency responses into a single FRD model SYS. The frequency vectors 
%    of SYS1, SYS2,... should not intersect and are merged together. The 
%    resulting frequency vector is sorted by increasing frequency. 
% 
%    See also FSELECT, FRDMODEL/INTERP, FRD. 

%   Copyright 1986-2022 The MathWorks, Inc.
ni = nargin;

% Check that all models are FRD
if ~all(cellfun(@(sys) isa(sys,'FRDModel'),varargin))
   error(message('Control:combination:fcat1'))
elseif ni<2
   sys = varargin{1};  return
end

try
   if ltipack.hasMatchingType('stack',varargin{:})
      sys = varargin{1};
      for j=2:ni
         sysj = varargin{j};
         if ~isequal(sys.IOSize_,sysj.IOSize_)
            error(message('Control:combination:fcat2'))
         end
         % Combine data
         fsys = fcat_(sys,sysj);
         % Combine metadata
         fsys = plusInput(fsys,sys,sysj);
         fsys = plusOutput(fsys,sys,sysj);
         sys = fsys;
      end
      sys.Name_ = '';  sys.Notes_ = strings(0,1);  sys.UserData = [];
   else
      % Harmonize types and try again
      [varargin{1:ni}] = ltipack.matchType('stack',varargin{:});
      sys = fcat(varargin{:});
   end
catch E
   throw(E)
end
