In RS232, it is not possible to determine whether pin 2 (transmit) and pin 3 (receive) have been shorted. Data goes out on pin 2, data gets received on pin 3, and the device cannot tell that this is the result of a short and not the result of that particular data happening to be received right then.
Therefore the closest you could get would be to display everything you get on the serial input, and not have the receive tied to transmit except when you want it to be. However you do need to be careful to tie the receive to ground when you do not have it shorted to transmit: if you leave it floating then because signal presence is determined by difference between receive and ground, a floating receive pin can have phantom input.
The easiest way to do all of this is to look for the existing code in the File Exchange.
And as explained in http://uk.mathworks.com/matlabcentral/answers/265385-gui-to-make-usb-to-serial-communication#answer_207589 if you are not using an actual RS232 port and are instead using a USB to RS232 adapter, there is no chance at all of detecting the short. (There are special-purpose RS232 ports that can determine whether a short exists using TDR, time domain reflectometry, but you would need special drivers to be able to invoke that mode if you had such a device, which you almost certainly do not have.)