function [A,B,C,D,E,dx0,x0,u0,y0,Delays] = ioselectDF(sys,rowIndex,colIndex,varargin)
% Constructs data function for SYS(I,J).
% Note: Deleting inputs is interpreted as setting its value to zero. Use
%       fixInput for all other interpretations.

%   Copyright 2022 The MathWorks, Inc.
[A,B,C,D,E,dx0,x0,u0,y0,Delays] = sys.DataFunction(varargin{:});
[ny,nu] = iosize(sys);
nfd = nfdelay(sys);

% Absorb input offsets for discarded inputs
if ~isempty(u0)
   jDel = 1:nu; jDel(:,colIndex) = [];
   if ~isempty(jDel)
      if isempty(dx0)
         dx0 = -B(:,jDel)*u0(jDel,:);
      else
         dx0 = dx0-B(:,jDel)*u0(jDel,:);
      end
      if isempty(y0)
         y0 = -D(:,jDel)*u0(jDel,:);
      else
         y0 = y0-D(:,jDel)*u0(jDel,:);
      end
   end
end

% Update delays
if ~isempty(Delays)
   Delays.Input = Delays.Input(colIndex,:);
   Delays.Output = Delays.Output(rowIndex,:);
end

% Update matrices and offsets
if nfd>0
   rowIndex = [reshape(rowIndex,[1 numel(rowIndex)]) ny+1:ny+nfd];
   colIndex = [reshape(colIndex,[1 numel(colIndex)]) nu+1:nu+nfd];
end
B = B(:,colIndex);
C = C(rowIndex,:);
D = D(rowIndex,colIndex);
if ~isempty(u0)
   u0 = u0(colIndex,:);
end
if ~isempty(y0)
   y0 = y0(rowIndex,:);
end
