It's an issue of expectation of privacy. If you are occupying a stall on your own with the door closed and no one can observe you unless they make special effort, then you have an expectation of privacy and you can masturbate quite legally.
If the stall has no door, then you have no expectation of privacy. In this case it is regarded as a public place and indecent acts are illegal. Even if you are only performing a bowel movement , to be absolutely safe you should probably hide your penis with your shirt or by some other means. To masturbate openly in such a stall such that others passing by can clearly see your hand stroking your penis up and down is almost certainly illegal. I am not a lawyer.