www.gusucode.com > mbcmodels 工具箱 matlab 源码程序 > mbcmodels/@xregcubic/term_char2order.m
function terms_out = term_char2order( obj, terms_in ) %TERM_CHAR2ORDER Convert the format of polynomial term specification. % % ORDER = MODEL.TERM_CHAR2ORDER( CHARS ) % % Polynomial terms can be specified in three ways: % * CHAR, for example 'x^2*y^3*z' % * ORDER, giving the order (power index) of each variable [2 3 1] % * MONOMIAL, a list of variables to multiply [1 1 2 2 2 3] % % Where there are several terms specified, these should be a cell array % of chars {'x','x*y^2','z^3'}, a matrix of orders [1 0 0;1 2 0;0 0 3] % or a cell array of monomials {[1],[1 2 2],[3 3 3]}. Note the special % case of a constant, char = '1', order=[0 0 0], monomial=[]. % Copyright 2006-2011 The MathWorks, Inc. if ischar( terms_in ), terms_in = {terms_in}; elseif ~iscell( terms_in ), error(message('mbc:xregcubic:term_char2order:InvalidValue')); end varlabels = get(obj,'symbol'); terms_out = zeros( numel(terms_in), numel(varlabels) ); for ii=1:numel(terms_in), if ischar( terms_in{ii} ), terms_out(ii,:) = i_string2order( terms_in{ii}, varlabels ); else error(message('mbc:xregcubic:term_char2order:InvalidValue')); end end %-------------------------------------------------------------------------% function term = i_string2order( string, varnames ) term = zeros( 1, numel(varnames) ); % First separate each token f = find( string == '*' ); token = cell(numel(f)+1,1); if numel(f)==0, token{1} = string; else token{1} = string(1:f(1)-1); for ii=2:numel(f), token{ii} = string(f(ii-1)+1:f(ii)-1); end token{numel(f)+1} = string(f(end)+1:end); end for ii=1:numel(token), f = find(token{ii}=='^'); var = ''; pwr = 1; if numel(f)==0, var = token{ii}; else var = token{ii}(1:f(1)-1); pwr = str2num(token{ii}(f(1)+1:end)); if numel(pwr)~=1, error('mbc:xregcubic:term_char2order:InvalidValue','Incorrect power specified: %s.',token{ii}(f(1)+1:end) ) end end if var == '1', % Leave as all zeros break; else n = find(ismember(varnames,var)); if numel(n)==1, term(n) = pwr; else error(message('mbc:xregcubic:term_char2order:InvalidValue1', var)); end end end