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

    function [conLabels,idx] = getConstraintNames(obj, type)
%GETCONSTRAINTNAMES Get constraint labels
%
%   CONLABELS = GETCONSTRAINTNAMES(OBJ, TYPE) returns optimization
%     constraint labels of a given TYPE. TYPE is either 'linear',
%     'nonlinear', 'sum','equality', 'inequality' or 'all'.
%   [conLabels,idx] = getConstraintNames(obj, type)
%      returns the logical indices to the requested constraint types
%

%   Copyright 2005-2010 The MathWorks, Inc.

% Get all objectives if type not specified
if nargin < 2
    type = 'all';
end
    
% Get the constraint types
NCON = length(obj.Constraints.Items);
idxlinear = cellfun(@isLinear, obj.Constraints.Items);
iseq = strcmp('==',cellfun(@getComparisonOperator, obj.Constraints.Items,'UniformOutput',false));

% Determine the constraint names to retrieve
switch type
    case 'sum'
        if NCON
            idx = cellfun(@isa, obj.Constraints.Items, repmat({'cgsumconstraint'}, 1, NCON));
        else
            idx = [];
        end
    case 'linear'
        idx = idxlinear;
    case 'nonlinear'
        idx = ~idxlinear;
    case 'inequality'
        idx = ~idxlinear & ~iseq;
    case 'equality'
        idx = ~idxlinear & iseq;
    otherwise
        idx = true(1, length(obj.Constraints.Items));
end 

% Retrieve the constraints
if any(idx)
    options = getOptimOptions(obj,'full');
    allcon = getConstraints(options);
    conLabels = {allcon(idx).label};
else
    conLabels = cell(1, 0);
end