If you are getting and keeping erections during masturbation, but not sex, you are probably having performance anxiety. But anyways, you should probably get another check up, full panel of blood work done, etc, it's always a good thing to keep yourself healthy
Hopefully it is just one of those "Oh man, what if I'm not >insert negative thought one has about self<" during sex moments. If it's anxiety, just calm down, and as hard as it maybe, don't worry about it, it will keep happening if you stress about it.
Hope you are healthy and wishing you well! Good Luck!