Sure, you can do anything you want. It might not be a good idea, but feel free.
Write an objective function that is, say, the cross-validation error rate for a particular parameter. If you have k as your parameter, and a cross-valudation partition c, then you might have
fun = @(k)kfoldLoss(fitcknn(X,y,'CVPartition',c,...
The only problem with this is that k is an integer variable, and most optimizers (including simulannealbnd) work only with continuous parameters. But you could use mixed-integer ga to optimize this.
MATLAB mathematical toolbox documentation