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

    function [OK, msg, out] = cgLoadMBCModel(filename, out)
%CGLOADMBCMODEL Load exported MBC models
%
%  [OK, msg, out] = CGLOADMBCMODEL(filename, out) loads models that have
%  been exported from the MBC Model Browser.  The models will be contained
%  in out.models.

%  Copyright 2000-2008 The MathWorks, Inc. and Ford Global Technologies, Inc.


OK = 0;
msg = '';

warnstate = warning('off','all');
try
    s = load(filename , '-mat');
    warning(warnstate);
catch ME
    warning(warnstate);
    errmsg = ME.getReport('basic', 'hyperlinks', 'off');
    msg = sprintf('Cannot read file.  It is missing or contains invalid data. \n%s',errmsg);
    return
end


fields = fieldnames(s);
if length(fields) > 1
    OK = 0;
    msg = ['Too many variables found in the EXM file ' filename '.'];
    return
end

mod = s.(fields{1});

if isa(mod , 'xregexportmodel')
    % replace the model name with the field name when we have only
    % one model.  This is ugly, but see G168686
    mod = setname(mod,fields{1});
    try
        OK = checkmodel(mod);
        if ~OK
            msg = 'Unable to load MBC model.';
        end
    catch ME
        OK = 0;
        msg = ME.message;
    end
    if OK
        out.models = {mod};
        OK = 1;
        msg = '';
    end
elseif isa(mod , 'cell')
    modelList = [];
    count = 0;
    for i = 1:length(mod)
        % pull off the export models
        if isa(mod{i} , 'xregexportmodel')
            try
                loadOK = checkmodel(mod{i});
                if ~loadOK
                    msg = 'Unable to load MBC model.';
                end
            catch ME
                loadOK = 0;
                msg = ME.message;
            end
            if loadOK
                count = count + 1;
                modelList = [modelList mod(i)];
            end
        end
    end

    if count == 0
        OK = 0;
        msg = ['No valid MBC models found in the .EXM file. ',msg];
    else
        out.models = modelList;
        OK = 1;
        msg = '';
    end
else
    OK = 0;
    msg = 'No MBC models found in the .EXM file.';
end