number of rules from a classification tree? I have generated a classification tree for a dataset using classregtree method in matlab. Tree gives me rules explained by if and elseifs. For a tree, i want to calculate number of rule generated from that tree without traversing manually from root to leaf node.. i need an algo or a method to get the number of rules generated.
 A: Here is a decision tree that Matlab learned from the Fisher Iris data set 
One rule from this tree is "Classify an iris as a setosa if its petal length is less than 2.45 (cm?)", which corresponds to the left-most branch of tree. Another, longer rule is "anything with a petal length greater than or equal to 2.45, but with a petal width of less than 1.75, and a petal length greater than 4.95, is a  virginica". 
Each rule is thus a complete traversal of tree, starting from the awkwardly-named "root" node at the top of the tree and ending at one of the "leaf" nodes, denoted here with circles, that indicate the example's class. Therefore, finding the total number of rules is equivalent to finding the total number of paths through the tree, which in turn is equivalent to finding the total number of leaf nodes.
There are many ways to do this. The tree structure lets you do this with a really simple and elegant recursive algorithm:
function countLeaves(subtree)
    if !(subTree.hasLeftChild() and subTree.hasRightChild())
      return 1;

    total = 0;
    if subTree.hasLeftChild()
      total = total + countLeaves(subTree.leftChild);
    if subTree.hasRightChild()
      total = total + countLeaves(subTree.rightChild);
    return total;

Any decent book on data structures and algorithms (or graph theory) will have more details. 
For Matlab specifically, the classregtree class has a method called isbranch. Calling tree.isbranch returns a vector containing an entry for each node with ones for nodes that are branches and zero for leaf nodes. Therefore, all you need to do is sum(~tree.isbranch()). If all you have is the textual representation of tree (which gets printed out whenever you disp() or display() it), I suppose you could count the number of lines that start with a number followed by "class = ", but that seems indirect.
Incidentally, classregtree is deprecated and you might want to move onto ClassificationTree or RegressionTree. Fortunately, these classes have a similar member function IsBranchNode.
A: For a T from the classregtree function
[YFIT,NODE] = eval(T,X)

the second output NODE is the tree node that the rules choose for X. Perhaps that's what you want. YFIT is the classification from that node.
For T from the newer fitctree function (which I recommend if you have a current release of MATLAB), the idea is the same, but it's the third output:
[YFIT,POSTERIOR,NODE]=predict(T,X)

