function DF = upgradeDF(DF,LPVFLAG,Version)
% Upgrade built-in data functions for LTV/LPV interpolation.
% Needed to support load/save.

%   Copyright 2023 The MathWorks, Inc.
fS = functions(DF);
wksp = fS.workspace{1};
INFO = wksp.INFO;
if Version<29
   F = fieldnames(INFO);
   for ct=1:numel(F)
      INFO.(F{ct}) = localUpdate29(INFO.(F{ct}));
   end
   INFO.pdim = numel(fieldnames(INFO.Grid));
   INFO.DELAY = false;
   if contains(fS.function,'R1')
      C = cat(1,struct2cell(wksp.SD),struct2cell(wksp.SO));
      F = cat(1,fieldnames(wksp.SD),fieldnames(wksp.SO));
      S = cell2struct(C,F,1);
      if LPVFLAG
         DF = @(t,p) ltvpack.interpolateR(t,p,S,INFO);
      else
         DF = @(t) ltvpack.interpolateR(t,[],S,INFO);
      end
   else
      gI = wksp.gI;
      if LPVFLAG
         DF = @(t,p) ltvpack.interpolateG(t,p,gI,INFO);
      else
         DF = @(t) ltvpack.interpolateG(t,[],gI,INFO);
      end
   end
end

%-------------------------------------------------
function S = localUpdate29(S)
% Template renamed to Nominal
if isstruct(S) && isfield(S,'Template')
   S.Nominal = S.Template;
   S = rmfield(S,'Template');
end