Matlab feature selection I am trying to learn relevant features in a 300*299 training matrix by taking a random row from it as my test data and applying sequentialfs on it. I have used the following code:

&gt;&gt; Md1=fitcdiscr(xtrain,ytrain);
&gt;&gt; func = @(xtrain, ytrain, xtest, ytest) sum(ytest ~= predict(Md1,xtest));
&gt;&gt; learnt = sequentialfs(func,xtrain,ytrain)


xtrain and ytrain are 299*299 and 299*1 respectively. Predict will give me the predicted label for xtest(which is some random row from original xtrain).

However, when I run my code, I get the following error:

Error using crossval&gt;evalFun (line 480)
The function '@(xtrain,ytrain,xtest,ytest)sum(ytest~=predict(Md1,xtest))' generated the following error:
X must have 299 columns.

Error in crossval&gt;getFuncVal (line 497)
funResult = evalFun(funorStr,arg(:));

Error in crossval (line 343)
    funResult = getFuncVal(1, nData, cvp, data, funorStr, []);

Error in sequentialfs&gt;callfun (line 485)
    funResult = crossval(fun,x,other_data{:},...

Error in sequentialfs (line 353)
                crit(k) = callfun(fun,x,other_data,cv,mcreps,ParOptions);

Error in new (line 13)
    learnt = sequentialfs(func,xtrain,ytrain)


Where did I go wrong?
func = @(xtrain, ytrain, xtest, ytest) sum(ytest ~= predict(Md1,xtest));


The function "func" is called by sequentialfs, which does not pass "Md1" to the "predict" function. Therefore, Md1 is unknown to that "cost" function. You should include "Md1=fitcdiscr(xtrain,ytrain);" within your "func" function.  

Beside, "sequentialfs" does a 10-fold CV. Why are you trying to pass only 299 samples instead of all 300?