I would suspect that the reason you always get 'failed' is because your idx is not scalar, in which case you need to loop of each element of your idx array before testing them with your if.
If that's not the issue then give attach your m code and a mat file with the necessary input to run it.
