Watch this Author's files
Nice work Daniel - clever to catch the error message. I was thinking this myself but did not have time to work it out.
I think, however that the code could be further improved by inserting these two lines after the first "end": fclose(s); delete(s);
This removes the unnecessary handle and does not confuse the future operations with the serial port.
Contact us at files@mathworks.com