www.gusucode.com > mbc 工具箱 matlab 源码程序 > mbc/@mctree/children.m

    function varargout=children(T,varargin)
%CHILDREN List of tree node's children or apply a function to children.
%
% ch= children(T,varargin);
% ch= children(T,index)
% out= children(T,@func,varargin)
% out= children(T,index,@func,varargin)
%   evaluates function @func and returns output in a cell array. The assumptions  
%   in children(T,func) is that the number of children don't change and that 
%   it is correct to evaluate the children from left to right.
%   Note this function is different from pveceval(children(T),func) as
%   pveceval assumes that all elements of children(T) are static during
%   evaluation of the loop. 
% 
% Normally this function is called from a pointer.
%   p.children or p.children(@func,args);

%  Copyright 2000-2006 The MathWorks, Inc. and Ford Global Technologies, Inc.

ch= T.Children;

if nargin>1
   npos= 1;
   if ~isempty(T) && (isnumeric(varargin{npos}) || islogical(varargin{npos}))
      % index the array of children pointers
      ind= varargin{1};
      ch= ch(ind);
      npos= npos+1;
   end
   
   if npos<=length(varargin) 
       % function call
       if ~isempty(ch);
           % evaluate 'func' on children pointers
           func= varargin{npos};
           if ischar(func)
               % turn function into function handle
               func= str2func(func);
           end
           nChildren= length(ch);
           out =cell(nargout,nChildren);
           % input arguments for function
           InputArgs= varargin(npos+1:end);
           for i=1:nChildren
               % get children dynamically
               C= ch(i).info;
               if nargout
                   [out{:,i}]= func(C,InputArgs{:});
               else
                   func(C,InputArgs{:});
               end
           end
           varargout= cell(1,nargout);
           for i=1:nargout
               % separate outputs 
               varargout{i}= out(i,:);
           end
       else
           % no children
           varargout = repmat({{}},1,nargout);
       end
   else
       % indexed pointer array
       varargout{1}= ch;
   end
else
    % all children
    varargout{1}= ch;
end