MATLAB - Arduino Uno Serial Communication Issue

You should really check out: Serial Input Basics - updated - #3 by Robin2

Also, you are not testing to see that you have received a valid pin # so your code will change any pin.

Check this out: relays - Wokwi ESP32, STM32, Arduino Simulator