In particular, 2nd example for crossval here http://www.mathworks.com/help/toolbox/stats/crossval.html shows what you need to do. You would need to replace the function handle classf in that example with a function which has two lines of code in it: 1) Train a TreeBagger on Xtrain and Ytrain, and 2) Predict labels for Xtest using the trained TreeBagger.