www.gusucode.com > mbcdesign 工具箱 matlab 源码程序 > mbcdesign/@coninputfactor/coninputfactor.m

    function cif = coninputfactor(varargin)
%CONINPUTFACTOR Input factor object for Constraints
%
%  CIF = CONINPUTFACTOR(VARARGIN)
%
%  CIF = CONINPUTFACTOR(NF)
%  CIF = CONINPUTFACTOR(NAMES)
%  CIF = CONINPUTFACTOR(NAMES, SYMBOLS)
%  CIF = CONINPUTFACTOR(NAMES, SYMBOLS, UNITS)
%  CIF = CONINPUTFACTOR(NAMES, SYMBOLS, UNITS, RANGE)
%  CIF = CONINPUTFACTOR(NAMES, SYMBOLS, UNITS, MIN, MAX)
%
%  Symbols and units for factors may empty, but all factors must have a
%  name. If NAMES is empty, then the default names {'X1', ...'Xd'} will be
%  used.
%
%  CIF = CONINPUTFACTOR(MODEL) uses the information in the given model to
%  construct an appropriate coninputfactor object.
%  CIF = CONINPUTFACTOR(MBCINPUTFACTOR) uses the information in the given inputs object to
%  construct an appropriate coninputfactor object.
%
%  See also CONBASE.

%  Copyright 2004-2008 The MathWorks, Inc. 


if nargin >= 1 && isstruct( varargin{1} ),
    cif = varargin{1};
elseif nargin==1 && (isa(varargin{1}, 'xregmodel') || isa(varargin{1}, 'mbcinputfactor'))
    cif = i_createfrommodel(varargin{1});
    return
else
    cif = i_parseInputs( varargin{:} );
end

cif = class( cif, 'coninputfactor' );


%-------------------------------------------------------------------------|
function cif = i_createfrommodel(m)
xi = xinfo( m );
Bnds = getcode( m );

% Make sure that we have names for all input factors
i = cellfun( 'isempty', xi.Names );
[xi.Names{i}] = deal( xi.Symbols{i} );

% Convert the units from JUNITs to CHARs
for i = 1:length( xi.Units ),
    xi.Units{i} = char( xi.Units{i} );
end

% Call this constructor with the information
cif = coninputfactor(xi.Names, xi.Symbols, xi.Units, Bnds(:,1), Bnds(:,2));


%-------------------------------------------------------------------------|
function  cif = i_parseInputs( varargin )

% The first argument is either the number of input factors or the names of
% those factors
if nargin < 1,
    nf = 1;
    names = {'X1'};
elseif ischar( varargin{1} ),
    names = varargin(1); % = {varargin{1}}, i.e., we keep as a cell array
    nf = 1;
elseif iscellstr( varargin{1} ),
    names = varargin{1}(:).'; % row array
    nf = length( names );
elseif isnumeric( varargin{1} ) && numel( varargin{1} ) == 1,
    nf = varargin{1};
    if nf < 1 || floor( nf ) ~= nf || ~isreal( nf ),
        error(message('mbc:coninputfactor:InvalidArguments2'));
    end
    names = cell( 1, nf );
else
    error(message('mbc:coninputfactor:InvalidArguments3'));
end

% Replace empty names with the default name
for i = 1:nf,
    if isempty( names{i} ),
        names{i} = sprintf( 'X%d', i );
    end
end

% The second argument is the factor symbols
if nargin < 2 || isempty( varargin{2} ),
    [symbols{1:nf}] = deal( '' );
elseif iscellstr( varargin{2} ) && numel( varargin{2} ) == nf,
    symbols = varargin{2}(:).'; % row array
elseif ischar( varargin{2} ) && nf == 1,
    symbols = varargin(2);  % = {varargin{2}}, i.e., we keep as a cell array
else
    error(message('mbc:coninputfactor:InvalidArguments4'));
end        

% The third argument is the factor units
if nargin < 3 || isempty( varargin{3} ),
    [units{1:nf}] = deal( '' );
elseif iscellstr( varargin{3} ) && numel( varargin{3} ) == nf,
    units = varargin{3}(:).'; % row array
elseif ischar( varargin{3} ) && nf == 1,
    units = varargin(3);  % = {varargin{3}}, i.e., we keep as a cell array
else
    error(message('mbc:coninputfactor:InvalidArguments5'));
end        

% Fourth & fifth arguments: ranges
if nargin == 4,
    % range
    if isnumeric( varargin{4} ) && all( size( varargin{4} ) == [2, nf] ),
        min = varargin{4}(1,:);
        max = varargin{4}(2,:);
    else
        error(message('mbc:coninputfactor:InvalidArguments6'));
    end
elseif nargin >= 5
    % min, max
    if isnumeric( varargin{4} ) && numel( varargin{4} ) == nf && ...
            isnumeric( varargin{5} ) && numel( varargin{5} ) == nf,
        min = varargin{4}(:).';
        max = varargin{5}(:).';
    else
        error(message('mbc:coninputfactor:InvalidArguments7'));
    end
else
    min = zeros(   1, nf );
    max = 100*ones( 1, nf );
end

cif = struct( ...
    'Max', max, ...
    'Min', min, ...
    'Name', {names}, ...
    'Symbol', {symbols}, ...
    'Unit', {units}, ...
    'Version', 1 );

%-------------------------------------------------------------------------|
% EOF
%-------------------------------------------------------------------------|