There are two issues in taking care of patients of the opposite sex. One is touch and the other is seeing them undressed.
The touch that is clearly forbidden by halacha is affectionate touch. Touching for a physical exam is not affectionate. While in general day to day activity we don't permit "non-affectionate" touch for no purpose as a further safeguard, in case of need such as medicine, this would be permitted in the context of study and practice.
With repeated exposure to undressed bodies, the element of excitement lessens. Furthermore, women are felt to be less sexually excited by seeing men undressed than men are when they see women. While looking at undressed men for purposes of enjoyment is frowned upon, seeing what one needs to see to care for them is permitted for students (who quickly become an important part of the care of patients in teaching institutions) as well as trained physicians.