Found this pointless piece of code in the initialization:
while k ~= unique(label)
idx = randsample(n,k);
m = X(:,idx);
[~,label] = max(bsxfun(@minus,m'*X,sum(m.^2,1)'/2),,1);
Unless I am missing something, I'm assuming you were trying to make sure at least one point is assigned to each cluster? Well, this just checks if at least 1 point is assigned to the kth cluster. E.g. try:
if k ~= unique([5;5;5;5])
it will say that label assignment is OK.
Furthermore since you draw the centers from the points themselves, there will always be at least 1 point in each cluster, making even the intended code pointless.
You may want to use another strategy to ensure centers are chosen that take more than a single point for instance.
Another common initialization strategy is to partition the points randomly into k clusters.
I ran across your post since I was looking how to solve the same problem for another file. I found out it was because the compiler could not handle the new C99 comments of the form "//", changing them to the original C comment style of "/*" and "*/" fixed my problem. I found this info (with more details) here: