This can happen if your matrix is close to symmetric positive semi-definite (meaning the smallest eigenvalue is around machine epsilon compared to the largest eigenvalue). The Cholesky function does not support symmetric positive semi-definite input.
If this doesn't happen every time you run the code, it must mean that the matrices you're passing to Cholesky are not exactly equal between runs - one of the other operations used must give slightly different results between runs. The function chol guarantees that if you call it twice with the exact same input within the same MATLAB session, it will return the exact same output (unless you're changing some very specific MATLAB settings in between).
How are you generating your covariance matrix? If it's by forming C = M'*M, you could instead compute the QR decomposition of M: M = Q*R, M'*M = R'*Q'*Q*R = R'*R (using that Q'*Q is the identity matrix for the QR decomposition). This is numerically more robust; note that if C is close to positive semi-definite, the last row(s) of R will likely be close to machine epsilon relative to the other entries of R. You may need to be careful about using R depending on the next step of your algorithm.