I think the biggest thing that helps me when I'm feeling like I need defend myself, etc is that whatever I say/do is not going to improve this person's opinion or behavior. They either (1) already created a negative impression of me that may or may not be justified, and me walking away, arguing, or responding with a smart comment is just going to make them feel like they were right all along, or (2) are just being negative because of their own issues, and it has nothing to do with my behavior. All arguing or walking away is going to do is escalate the situation and make you look worse.
"Okay, thanks for the feedback" with a neutral face/voice is a great response - you acknowledge that you hear what they're saying, without necessarily accepting their interpretation as accurate or fair. You have been professional and "receptive to feedback," which makes you look good. And if it's just that they're a grumpy person or having a bad day, you've de-escalated the situation and ended the conversation. And then afterwards you can change the subject or say "excuse me, I'm going to go do X" if you feel like you need to leave the situation.
And then obviously, also consider that they might be being rude for a reason or just giving you legitimate feedback in a not-so-kind tone. Especially as a med student, most people in the hospital will know a lot more than you, and it's usually valuable to hear their input and consider that there might be a grain of truth in what they're saying even if the delivery was less than ideal. And also the hospital is a stressful place, so remember that even if someone seems angry while talking to you, they may not be angry about you.