Problem with MATLAB to Arduino MEGA

Hi, I want to interface MATLAB with Arduino MEGA. For the serial reading operation from Arduino MEGA to MATLAB i havn't any problem.

But I don't succeed for the writing operation from MATLAB to Arduino MEGA, this is my code:

Arduino Code:

byte incomingByte = 0;

void setup() { Serial.begin(9600); }

void loop() {

if (Serial.available() > 0) { incomingByte =; Serial.print("I received: "); Serial.println(incomingByte, DEC);

} }

MATLAB code:

function serialMTOA()

s = serial('COM5', 'BaudRate', 9600); set(s,'DataBits',8); set(s,'StopBits',1); set(s,'Parity','none');


fwrite(s, 5,'uint8','async');

stopasync(s); fclose(s);


Are there some errors? When I send the Matlab function I see that Arduino receives (TX led blinks) but when I try to see the Arduino Serial Monitor there's nothing.

Others informations: I use the usb alimentation, and only the usb as serial (no others comunication ports).


is it possible to write to the arduino with one program and to read from it with another program?

what happens when u make the arduino turning on pin 13 if it receives an 'A' and off if it receives an 'E'...?


I get round this by getting matlab to check the connection status first:

Comtest = arduino.TransferStatus; % check port status before trying to write if Comtest(1) == 'i'; fwrite(arduino, ServoValue, 'uint8','async') else Comtest; pause(0.08) end

Hope this helps

'i' here as in idle