function D = evalBlocks(D,BlockNames,BlockValues)
% Replaces specified blocks by specified numeric values.

%   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);

% Account for block transform
nEval = numel(iEval);
BV = cell(nEval,1);
for ct=1:nEval
   ix = iEval(ct);
   BV{ct} = evalBlockTransform(Blocks(ix),BlockValues{iLoc(ix)});
end

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

% Replace blocks by transformed values
D.IC = lft(IC,blkdiag(BV{:}));
D.Blocks = Blocks(iKeep,:);
