Can masturbation be allowed in case of a medical condition that requires it?

If someone has some sort of medical issue that requires masturbation (And I don’t mean IVF, or anything of that sort) would they be able to masturbate for that purpose? I have never heard of any case where this would be necessary, but if it in fact was, would it be a mortal/venial sin for that person to do so? Also, if it was necessary for a fertility test would it be alright to do so?

The following links should help answer your questions. If you have any further questions, please contact Catholic Answers directly.

Recommended Reading:

Does the Church allow this for Prostatitis?
Is this particular medical test moral?

DISCLAIMER: The views and opinions expressed in these forums do not necessarily reflect those of Catholic Answers. For official apologetics resources please visit www.catholic.com.