function [A,B,C,D,E,dx,x,u,y,Delay] = interpolateG(t,p,gI,INFO)
% Interpolate A,B,C,D,E,dx,x,u,y varying data using griddedInterpolant or
% scatteredInterpolant.
% NOTE: Avoid renaming or changing this function (breaks load/save).

%   Copyright 2022 The MathWorks, Inc.

% Construct x value
tidx = INFO.tidx;
p = p(:);
if isempty(tidx)
   x = p;
else
   x = [p(1:tidx-1); t; p(tidx:end)];
end
if numel(x)~=INFO.pdim
   error(message('Control:ltiobject:ssInterpolant16',INFO.pdim-numel(tidx)))
end

% Clip each parameter to its range in the grid ('clip' extrapolation)
if strcmp(INFO.Extrapolation,'clip') && ~isempty(INFO.GridVectors)
   gvecs = INFO.GridVectors;
   for ct=1:numel(x)
      range = gvecs{ct};
      if x(ct)<range(1)
         x(ct) = range(1);
      elseif x(ct)>range(end)
         x(ct) = range(end);
      end
   end
end

% Evaluate interpolant
% Note: V is of size [1 ... 1 numel(V)] for griddedInterpolant and 
% [1 numel(V)] for scatteredInterpolant.
V = gI(x.');

% Update varying part of A,B,C,D,...
k = 0;
% A
nv = INFO.A.nv;
if nv==0
   A = INFO.A.Nominal;
elseif INFO.A.nf==0
   A = reshape(V(k+1:k+nv),size(INFO.A.Nominal));
else
   A = INFO.A.Nominal;
   A(INFO.A.iv) = V(k+1:k+nv);
end
k = k+nv;
% B
nv = INFO.B.nv;
if nv==0
   B = INFO.B.Nominal;
elseif INFO.B.nf==0
   B = reshape(V(k+1:k+nv),size(INFO.B.Nominal));
else
   B = INFO.B.Nominal;
   B(INFO.B.iv) = V(k+1:k+nv);
end
k = k+nv;
% C
nv = INFO.C.nv;
if nv==0
   C = INFO.C.Nominal;
elseif INFO.C.nf==0
   C = reshape(V(k+1:k+nv),size(INFO.C.Nominal));
else
   C = INFO.C.Nominal;
   C(INFO.C.iv) = V(k+1:k+nv);
end
k = k+nv;
% D
nv = INFO.D.nv;
if nv==0
   D = INFO.D.Nominal;
elseif INFO.D.nf==0
   D = reshape(V(k+1:k+nv),size(INFO.D.Nominal));
else
   D = INFO.D.Nominal;
   D(INFO.D.iv) = V(k+1:k+nv);
end
k = k+nv;
% E
nv = INFO.E.nv;
if nv==0
   E = INFO.E.Nominal;
elseif INFO.E.nf==0
   E = reshape(V(k+1:k+nv),size(INFO.E.Nominal));
else
   E = INFO.E.Nominal;
   E(INFO.E.iv) = V(k+1:k+nv);
end
k = k+nv;
% dx
nv = INFO.dx.nv;
if nv==0
   dx = INFO.dx.Nominal;
elseif INFO.dx.nf==0
   dx = reshape(V(k+1:k+nv),[nv 1]);
else
   dx = INFO.dx.Nominal;
   dx(INFO.dx.iv,:) = V(k+1:k+nv);
end
k = k+nv;
% x
nv = INFO.x.nv;
if nv==0
   x = INFO.x.Nominal;
elseif INFO.x.nf==0
   x = reshape(V(k+1:k+nv),[nv 1]);
else
   x = INFO.x.Nominal;
   x(INFO.x.iv,:) = V(k+1:k+nv);
end
k = k+nv;
% u
nv = INFO.u.nv;
if nv==0
   u = INFO.u.Nominal;
elseif INFO.u.nf==0
   u = reshape(V(k+1:k+nv),[nv 1]);
else
   u = INFO.u.Nominal;
   u(INFO.u.iv,:) = V(k+1:k+nv);
end
k = k+nv;
% y
nv = INFO.y.nv;
if nv==0
   y = INFO.y.Nominal;
elseif INFO.y.nf==0
   y = reshape(V(k+1:k+nv),[nv 1]);
else
   y = INFO.y.Nominal;
   y(INFO.y.iv,:) = V(k+1:k+nv);
end
k = k+nv;

% Delays
if INFO.DELAY
   if INFO.CT
      V(k+1:end) = max(0,V(k+1:end));
   else
      V(k+1:end) = max(0,round(V(k+1:end)));
   end
   % Input
   nv = INFO.uDelay.nv;
   if nv==0
      uDelay = INFO.uDelay.Nominal;
   elseif INFO.uDelay.nf==0
      uDelay = reshape(V(k+1:k+nv),[nv 1]);
   else
      uDelay = INFO.uDelay.Nominal;
      uDelay(INFO.uDelay.iv,:) = V(k+1:k+nv);
   end
   k = k+nv;
   % Output
   nv = INFO.yDelay.nv;
   if nv==0
      yDelay = INFO.yDelay.Nominal;
   elseif INFO.yDelay.nf==0
      yDelay = reshape(V(k+1:k+nv),[nv 1]);
   else
      yDelay = INFO.yDelay.Nominal;
      yDelay(INFO.yDelay.iv,:) = V(k+1:k+nv);
   end
   k = k+nv;
   % Internal
   if INFO.xDelay.nv==0
      xDelay = INFO.xDelay.Nominal;
   elseif INFO.xDelay.nf==0
      xDelay = reshape(V(k+1:k+nv),[nv 1]);
   else
      xDelay = INFO.xDelay.Nominal;
      xDelay(INFO.xDelay.iv,:) = V(k+1:k+nv);
   end
   Delay = struct('Input',uDelay,'Output',yDelay,'Internal',xDelay);
else
   Delay = [];
end

