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

    function optim = setinitialvaluedatafromoutput(optim, Output, pInp, OutNames, Runs, Sol, ChangeVars)
%SETINITIALVALUESDATAFROMOUTPUT Set initial values from an output.
%
%   OBJ = SETINITIALVALUESDATAFROMOUTPUT(OBJ, OUTPUT, FACS, OUTNAMES, RUNS,
%   SOL, CHANGEVARS) imports data from an optimization output object,
%   OUTPUT.  FACS must be a vector of pointers to the variables that
%   initial values should be set for.  OUTNAMES must be a cell array the
%   same size as FACS that indicates which output columns should be
%   imported into each corresponding entry in FACS.  If RUNS is specified,
%   only a subset of the output runs will be imported.  If SOL is specified
%   then the specified solution will be imported.  If SOL is set to -1 then
%   the best solution will be used if it exists.  The default setting for
%   SOL is to use the first solution. CHANGEVARS is a boolean flag that
%   specifies whether the number of variables in the optimization will be
%   altered when the output is imported. Note that this option is ignored
%   if all the variables in the OUTPUT are not scalar.
%
%   The number of RUNS imported must match the number of runs in the
%   optimization object.

%   Copyright 2006-2011 The MathWorks, Inc.


if nargin<7
   ChangeVars = false; 
end

if nargin<6
    Sol = 1;
end

if nargin<5
    Runs = 1:getNumRuns(Output);
end

if ~ChangeVars && length(Runs)~=getNumRuns(optim)
    error(message('mbc:cgoptim:InvalidArgument32'));
end

% Get all of the data for the specified solution from the output
if Sol==-1
    [AllData, AllNames] = getSelectedSolution(Output, ...
        'OutputFormat', 'cell', ...
        'OutputContents', {'FreeVars', 'FixedVars', 'Objectives', 'Constraints'});
else
    [AllData, AllNames] = getSolution(Output, Sol, ...
        'OutputFormat', 'cell', ...
        'OutputContents', {'FreeVars', 'FixedVars', 'Objectives', 'Constraints'});
end

% Find data for specified output names
[Found, AllDataIdx] = ismember(OutNames, AllNames);
if ~all(Found)
    error(message('mbc:cgoptim:InvalidArgument33'));
end

RunsToImport = 1:getNumRuns(optim);
for n = 1:length(AllDataIdx)
    varData = AllData{AllDataIdx(n)}(Runs, :);
    if ChangeVars && size(varData, 2) == 1
        varData = varData';
    end
    optim = setinitialvaluedata(optim, pInp(n), RunsToImport, varData);
end