www.gusucode.com > mbcdata 工具箱 matlab 源码程序 > mbcdata/@cgoptimconstraint/pInsertIndicesIntoTemplate.m

    function str = pInsertIndicesIntoTemplate(obj, templateStr, inputNames, inputValues)
%PINSERTINDICESINTOTEMPLATE Inserts indices into a template string
%
%   STR = PINSERTINDICESINTOTEMPLATE(OBJ, TEMPLATESTR, INPUTNAMES,
%   INPUTVALUES) inserts indices into the specified template string,
%   replacing any occurrences of the token, "CONSUMMARYINPUTx", where x is
%   an integer denoting the x-th input of OBJ. 
%
%   STR is a 1-by-NSTR cell array of strings, where NSTR is the maximum
%   length of any of the variables in INPUTVALUES. In the i-th row of STR,
%   the token is replaced by INPUTNAMES{x}(i) if the inputValues{x} is a
%   vector or INPUTNAMES{x} if it is a scalar.
%
%   This method is intended for use by subclasses of this object only. It
%   is currently used by CGPOINTCONSTRAINT/GETSUMMARY and
%   CGRANGECONSTRAINT/GETSUMMARY.
%
%   See also CGPOINTCONSTRAINT/GETSUMMARY, CGRANGECONSTRAINT/GETSUMMARY

%   Copyright 2007 The MathWorks, Inc.

% Determine length of all the variables
Sc = cellfun('size', inputValues, 2);
nCon = max(Sc);
isScalarVar = (Sc == 1);
if ~all(isScalarVar | Sc == nCon)
    error(message('mbc:cgpointconstraint:InvalidArgument'));
end

% Replace CONSUMMARYINPUTj with j-th input name followed by (%d) if it is a
% vector variable
for j = 1:length(inputNames)
    repToken = sprintf('CONSUMMARYINPUT%d', j);
    if isScalarVar(j)
        repStr = inputNames{j};
    else
        repStr = [inputNames{j}, '(%d)'];
    end
    templateStr = strrep(templateStr, repToken, repStr);
end

% Find how many %d's there are to replace
nRep = length(strfind(templateStr, '%d'));

% Perform the replacement
str = cell(1, nCon);
for i = 1:nCon
    str{i} = sprintf(templateStr, i*ones(1, nRep));
end