You should follow the following steps:
- With your training data, create a PCA model
- With the PCA of your training data, train your classifier
- Apply the first PCA model to your new data
- With the PCA of your new data, test the classifier
Here are some code snippets for steps 1 and 3 (2 and 4 depend on your classifier):
%Step 1.Generate a PCA data model
[W, Y] = pca(data, 'VariableWeights', 'variance', 'Centered', true);
%# Getting the correct W, mean and weights of data (for future data)
W = diag(std(data))\W;
[~, mu, we] = zscore(data);
we(we==0) = 1;
%Step 3.Apply the previous data model to a new vector
%# New coordinates as principal components
x = newDataVector;
x = bsxfun(@minus,x, mu);
x = bsxfun(@rdivide, x, we);
newDataVector_PCA = x*W;