www.gusucode.com > mbcmodels 工具箱 matlab 源码程序 > mbcmodels/@xregoptmgr/IsAlternative.m

    function OK = IsAlternative(om1,om2)
%ISALTERNATIVE
%
% OK = IsAlternative(om1,om2);

%  Copyright 2006-2008 The MathWorks, Inc. 


% use name for algorithm if algorithm is not set
CompareNames = false;
if isempty(om1.algorithm)
    om1.algorithm = om1.name;
    CompareNames = true;
end
if isempty(om2.algorithm)
    om2.algorithm = om2.name;
    CompareNames = true;
end

% same algorithm
SameAlgorithm = iCompareAlgorithms(om1.algorithm,om2.algorithm);

% same context
SameContext = iCompareContext(om1,om2);

if (SameAlgorithm && SameContext)
    OK = true;
else
    % check whether this algorithm is an alternative
    if isempty(om1.Alternatives) && ~isempty(om2.Alternatives)
        om1.Alternatives = om2.Alternatives;
    end
    alg2 = iAlgName(om2.algorithm);
    OK = false;
    i= 0;
    while ~OK && i<length(om1.Alternatives)
        i = i+1;
        OK = iCompareAlgorithms(alg2,om1.Alternatives{i});
    end
end
if ~OK && isempty(om1.name) && isempty(om2.name) ...
        && isempty(om1.Alternatives) && isempty(om2.Alternatives)
    % allow assignment if both names are empty and 
    OK = true;
end
if ~OK && CompareNames
    % have to construct alternatives
    Alts = getAlternatives(om1);
    for i=1:length(Alts)
        OK = OK || strcmp(om2.name,Alts{i}.name);
    end
    
end


%--------------------------------------------------------------------------
function OK = iCompareAlgorithms(alg1,alg2)

OK = strcmp(iAlgName(alg1),iAlgName(alg2));

%--------------------------------------------------------------------------
function OK = iCompareContext(om1,om2)
% context is same if either context is empty or context matches
OK = isempty(om1.Context) || isempty(om2.Context) || isequal(om1.Context,om2.Context);

%--------------------------------------------------------------------------
function alg = iAlgName(alg)
if iscell(alg)
    % context implementation 
    alg= alg{2};
end
if isa(alg,'function_handle')
    alg = func2str(alg); 
end