arduino and matlab: serial port

Hello, I am trying to hook up matlab to my arduino. I am trying to write a number to the serial port in the arduino IDE and retrieving the same number in the matlab IDE. My arduino code is:

void setup() {

// initialize serial communication at 9600 bits per second:

Serial.begin(9600);

}

void loop() {

if (Serial.available()){

Serial.println(3);

delay(100); // delay in between reads for stability

}

}

And my matlab code looks like:

%s = serial(‘COM9’);

s = serial ( ‘COM9’ , ‘BaudRate’ , 9600, ‘terminator’ , ‘CR’ );

%set(s,‘BaudRate’,4800);

fopen(s);

fprintf(s,‘*IDN?’)

fread(s)

disp(s)

out = fscanf(s);

fclose(s)

delete(s)

clear s

and the matlab output looks like:

10

51

13

10

51

13

10

51

13

How can I change this so only “3” comes out?

Thanks!

Do you know why you are getting 51, 13, 10 ?

When you do a “print” the Arduino code takes the number and encodes it as ASCII. That’s what goes out over the serial link. In addition the .println() tells the receiving end to do a new line which is usually done by transmitting the ASCII control characters for carriage return and newline. Care to guess what the ASCII representations are for a decimal 3, CR and NL ?

http://www.asciitable.com/

You can either have Matlab decode the ASCII and output only the pertinent characters or try a Serial.write() instead. See if that works like I think (hope) it does.

http://arduino.cc/en/Serial/Write

ps - (click on to open and enlarge)