In which situations is cvx_optval nan

Why sometimes the cvx_optval is equal to nan, but other variables have values?

Please post a complete reproducible problem, including all solver and CVX output, and disp of relevant variables. I am not aware of any situation in which cvx_optval is NaN, but variables have non-NaN values (although I am not saying it definitely can’t happen). Are you using CVX 2.2?