www.gusucode.com > mbcview 工具箱matlab源码程序 > mbcview/@cgproject/ModelInputs.m

    function [pInputs,IsNew] = ModelInputs(CGP,m, doReplaceVars,doModelConnect)
%MODELINPUTS create or find inputs to model in CAGE project
%
% pInputs = ModelInputs(CGP,m, doReplaceVars,doModelConnect)
%    CGP             CAGE project
%    doReplaceVars   Replace existing variables in CAGE - updates range
%                    (default true)
%    doModelConnect  Connect models to inputs (default false)

%  Copyright 2007-2013 The MathWorks, Inc. and Ford Global Technologies, Inc.

if nargin<3
    doReplaceVars = true;
end
if nargin<4
    doModelConnect= false;
end
pData = getdd(CGP);
hData = pData.info;

n = nfactors(m);
inputNames = getsymbols(m);
if isa(m,'xregstatsmodel')
    % use signal name as an alias
    Inp = getInputs(model(m));
    Aliases =  {Inp.Name}';
else
    Aliases = cell(1,n);
end

R = getranges(m)';
if isSwitchModel(m)
    [localMin,localMax] = modelranges(m);
    OpPoints = getSwitchPoints(m);
    OpPoints = [(localMin(:,1)+localMax(:,1))/2 ; OpPoints(1,:)'];
end
pInputs = mbcpointer(1, n);
if doModelConnect
    % Generate a list of model names that we can use as an input instead of a
    % variable
    pModExprs = getmodels(CGP);
    mdlnames = pveceval(pModExprs,@getname);
else
    mdlnames = {};
end
IsNew = false(1,n);
for j = 1:n
    idx = find(strcmp(inputNames{j}, mdlnames));
    if isempty(idx) || ~doModelConnect
        % Ask the variable dictionary to create an item for us
        if isempty(Aliases{j}) || strcmp(inputNames{j},Aliases{j})
            [hData, newv, isInDD] = add(hData, inputNames{j});
        else
            % add alias as well
            [hData, newv, isInDD] = add(hData, inputNames{j},Aliases{j});
        end
        pInputs(j) = newv;
        IsNew(j) = ~isInDD;
        if ~isInDD
            newv.info = newv.setrange(R(j,:));
            if isSwitchModel(m) 
                sp = OpPoints(j);
            else
                sp = mean(R(j,:));
            end
            newv.info = newv.setnomvalue(sp);

        elseif doReplaceVars && ~newv.isconstant
            % Update the range of the input
            newv.info = newv.setrange(R(j,:));
        end
    else
        % connect to model
        pInputs(j) = pModExprs(idx);
    end
end
% update the data dictionary
pData.info= hData;