function a = mtpath(o, pth )
%MTPATH a = MTPATH( obj, path_string )  Follow an Mtree path
% returns the set of nodes generated by following a path from obj

% Copyright 2006-2014 The MathWorks, Inc.

% this is a recursive implementation to handle + and *
    a = o;
    I = find(a.IX);
    [I,pth,flag,rest] = pathit( o, I, pth );
    % on return, if no + and *, flag is 0 and pth and rest are
    % empty
    % if flag is nonzero, then it should be less than 4
    % pth has the path component to which the */+ is applied
    % II reflects the path before that
    % rest has any further path elements that are applied later
    I = I(I~=0);   % throw out dead ends

    % for L, R, and X, a.m = length(I).  But this isn't true for P
    
    a.IX = false( 1, o.n );
    a.IX(I) = true;
    a.m = sum(a.IX);
    if flag
        if flag >= 4
            error(message('MATLAB:mtree:pathAndOr'));
        end
        if bitand(flag, 1)
            a = List( a );
        end
        if bitand(flag, 2)
            a = Tree( a );
        end
        % when flag is nonzero, we may still have path to do
        % recurse
        if ~isempty( pth )
            a = mtpath( a, pth );
        end
        if ~isempty( rest )
            a = mtpath( a, rest );
        end
    end
end
