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

    function optim = renameObjective(optim, oldLabel, newLabel)
%RENAMEOBJECTIVE Rename an objective
%
%   OPTIM = RENAMEOBJECTIVE(OPTIM, OLDLABEL, NEWLABEL) renames the
%   specified objective. 
%
%   See also CGOPTIM/RENAMECONSTRAINT

%   Copyright 2007 The MathWorks, Inc.

% Return if oldLabel and newLabel are identical
if strcmp(oldLabel, newLabel)
    return
end

% Find existing objective
ind = getObjectiveIndex(optim.OptimSetup, oldLabel);
if ~ind
   error(message('mbc:cgoptim:InvalidArgument12', oldLabel));
end 

if ~canRename(optim.OptimSetup)
   error(message('mbc:cgoptim:InvalidArgument13'));
end

if ~isvarname(newLabel)
    error(message('mbc:cgoptim:InvalidArgument14'));
end

newUniqueLabel = generateLabel(optim.OptimSetup, newLabel, 'allowroot');
if ~strcmp(newUniqueLabel, newLabel)
   error(message('mbc:cgoptim:InvalidArgument15'));
end

% Set the new label as the name in the objective object
objective = optim.Objectives{ind};
objective = setName(objective, newLabel);
optim.Objectives{ind} = objective;

% Set the new label in the setup
optim.OptimSetup = renameObjective(optim.OptimSetup, oldLabel, newLabel);