www.gusucode.com > mbc 工具箱 matlab 源码程序 > mbc/@mctree/preorder.m
function List= preorder(T,varargin) %PREORDER preorder traversal of tree. % % List= preorder(T,varargin); % List= preorder(T,'func',varargin) % evaluates function 'func' and returns output in a cell array. % % Normally this function is called from a pointer. % p.preorder or p.preorder('func',args); % Copyright 2000-2008 The MathWorks, Inc. and Ford Global Technologies, Inc. if ~isnull(address(T)) if nargin>1 && ischar(varargin{1}) varargin{1} = str2func(varargin{1}); end List = i_Preorder(info(T),varargin); if length(List)==1 List = List{1}; end else error(message('mbc:mctree:InvalidPointerReference3')); end %------------------------------------------------------------------------ function List= i_Preorder(T,Args) % collect outputs in a cell array if isempty(Args) % Just return what is in the node List = {T}; elseif length(Args)==1 % First element of varargin argument is function name List = {Args{1}(T)}; else % First element of varargin argument is function name % Other elements are additional inputs List = {Args{1}(T,Args{2:end})}; end ch = T.Children; if ~isempty(ch) && ~any(isnull(ch)) clist = cell(length(ch),1); carray = infoarray(ch); for i=1:length(ch) % preorder each of the children % preorder requires the dynamic copy of carray. The call to info is % required to get the up-to-date version from the heap. clist{i}= i_Preorder(info(carray{i}),Args); end List= cat(1,List,clist{:}); end