Are you using for loops for putting data into test and train? Usually,
that might cause some problems - one solution is to use direct
indexing
test_data = data(test_idx, :);
train_data = data(train_idx, :);
Not sure what else might be causing problems. You can of course
generate the indices manually (using either rand() or even simpler by
just dividing the dataset into k equal parts).
Hope this helps,
Vinay