function D = evalBlocks(D,BlockNames,BlockValues)
% Replaces specified blocks by specified values. Each value
% is either a double matrix or an ltipack.ssdata object.

%   Copyright 1986-2011 The MathWorks, Inc.
Blocks = D.Blocks;
iLoc = ltipack.findNamesInList(getBlockName(Blocks),BlockNames);
iEval = find(iLoc>0);
if isempty(iEval)
   return
end
iKeep = find(iLoc==0);
IC = D.IC;

% Account for block transform and convert all block values 
% to ltipack.frddata
nEval = numel(iEval);
BV = cell(nEval,1);
for ct=1:nEval
   ix = iEval(ct);
   val = evalBlockTransform(Blocks(ix),BlockValues{iLoc(ix)});
   if isnumeric(val)
      BV{ct} = createGain(IC,val);
   else
      BV{ct} = frd(val,IC.Frequency);
   end
end

% Move evaluated blocks to end of block vector
[ny,nu] = iosize(D);
[wperm,zperm] = getRowColPerm(Blocks,[iKeep;iEval]);
IC = ioperm(IC,[1:ny,ny+zperm],[1:nu,nu+wperm]);

% Replace blocks by their (transformed) values
V = BV{1};
for ct=2:nEval
   V = append(V,BV{ct});
end
[nw,nz] = iosize(V);
[nyz,nuw] = iosize(IC);
D.IC = lft(IC,V,nuw-nw+1:nuw,nyz-nz+1:nyz,1:nz,1:nw);

% Update block vector
D.Blocks = Blocks(iKeep,:);