www.gusucode.com > mbctools 工具箱 matlab 源码程序 > mbctools/@mdevtestplan/exportMultiModels.m

    function exportMultiModels(TP,CreateItems)
%exportMultiModels
%
% exportMultiModels(TP)

%  Copyright 2008-2016 The MathWorks, Inc.


MP = project(TP);
% get multimodels from model browser
GlobalMap = multimodel(MP.info,address(TP));
if length(GlobalMap)~=numChildren(TP)
    % didn't manage to make multimodels for some responses
    if isempty(GlobalMap)
        Missing = children(TP,@varname);
    else
        Missing = setdiff(children(TP,@varname),cellfun(@getname,GlobalMap,'UniformOutput',false));
    end
    if length(Missing)>1
        Missing = sprintf('%s, ',Missing{:});
        msg = sprintf('No models have been exported. There are no point-by-point models for %s.',Missing(1:end-2));
    else
        msg = sprintf('No model has been exported. There is no point-by-point model for %s.',Missing{1});
    end
        
    xregerror('Point-by-Point Models Export',msg);
    mv_busy('delete')
    return
end

OpPoints = getSwitchPoints(GlobalMap{1});

if get(cgbrowser,'GUIExists') && ~isempty(CreateItems)
    CGP = get(cgbrowser,'rootNode');
    
    % export models
    pCage = iExportModels(CGP,GlobalMap,OpPoints);

    SelectedNode = pCage(1);
    ext = mbcMakeValidName(name(TP));
    
    if CreateItems.TradeOff
        pTradeOff  = address(cgtradeoffnode(sprintf('Tradeoff_%s',ext)));
        % Change above, so unique names are given to models
        addnodestoproject(CGP.info, pTradeOff);

        % build tradeoff directly
        s.GlobalMap = GlobalMap;
        s.OpPoints = OpPoints;
        s.pModels = pCage;

        % Load file using multimodel dialog
        guiImportMMFile(pTradeOff.info, s);
        % models are modified when added to tradeoff 
        %   see xregmodswitch/SnapToGrid
    else
        pTradeOff = [];
    end
    
    if CreateItems.Dataset
        % export operating points to datasets
        SelectedNode  = iMakeDataSet(sprintf('OperatingPoints_%s',ext),CGP,pCage(1).getdata);
    end
        
    if CreateItems.Optim
        % make optim for multimodel
        pModel = pCage(CreateItems.Objective).getdata;
        SelectedNode  = iCreateOptim(pModel,CGP,CreateItems.ObjectiveType);
    end
    
    if ~isempty(pTradeOff)
        % select tradeoff if it was made
        SelectedNode = pTradeOff;
    end

    cgSelectNode(SelectedNode)
else
    % If CAGE is not open the old behaviour (save to file and import
    % multimodel tradeoff in CAGE) is used
    
    % save file
    [fname,pathname]=uiputfile(mbcGetPath('mbcmodel', 'Models', '*.mat'),'Export to Point-by-Point Model Tradeoff');
    if ischar(fname)
        mv_busy('Saving point-by-point models...');
        [pth, f,e]= fileparts(fullfile(pathname,fname));
        if isempty(e)
            e= '.mat';
        end
        save(fullfile(pth,[f,e]),'GlobalMap','OpPoints')

    end
end
mv_busy('delete')

function pOpNode = iMakeDataSet(Name,CGP,pModel)
% make dataset with midpoints of local variables and operating points

op = cgoppoint;
% connect inputs
pOp= xregpointer( op );
% make model node
pOpNode = cgnode(pOp.info,pOp,pOp,0);
% Change above, so unique names are given to models
addnodestoproject(CGP.info, pOpNode);

% add inputs
pInp= getinports(pModel.info);
values= pveceval(pInp,@getvalue);
op = addfactor(op, pInp, values);
% set data   
mdl = pModel.get('model');

op = set(op,'Data',InitialValues(mdl));
op = setname(op,Name);
% update heap
pOp.info = op;           



function pCage = iExportModels(CGP,GlobalMap,OpPoints)

pCage = mbcpointer(1,length(GlobalMap));
% variables for model inputs - don't use models
pInputs = ModelInputs(CGP.info,GlobalMap{1}, true,false);
mdl = GlobalMap{1};
data = InitialValues(mdl);
for i=1:length(pInputs);
    % set nominal values for operating points to be the first operating
    % point
    pInputs(i).info = pInputs(i).setnomvalue(data(1,i));
end

for i=1:length(GlobalMap)
    % make sure all models have the same inputs
    pCage(i) =  ImportMBCModel(CGP.info,GlobalMap{i},getname(GlobalMap{i}),false,true,pInputs);
end


function cgSelectNode(nd)
%CGSELECTNODE select node in CAGE Browser
%
% cgSelectNode(nd)


if isa(nd,'mbcfoundation.pointerinterface')
    nd = nd.Node.pGetPointerInstance;
end

% select first model
h = cgbrowser;
if matchtype(nd.typeobject,h.CurrentType)
    % Refresh tree/list
    h.doDrawList;
    h.doDrawTree;
end

h.gotonode(nd,xregpointer);
% bring to foreground
figure(h.Figure);

function pOpt = iCreateOptim(pModel,CGP,Type)

pOpt = pModel.createOptim(CGP,Type);
p = pOpt.getdata;
p.info = initializeSwitchData(p.info);