Why am I always getting 48 with serial communication? So I want to send some data from Matlab to Arduino. To verify that I am getting the correct data I am sending the data back to Matlab (since I get errors if I open the serial monitor, if there's an easier way to print debug messages let me know).

The problem is that I always get 48 back.

My matlab code:

arduino = serial('COM3','BaudRate',115200);
fopen(arduino);
fprintf(arduino,'%f',1);
fclose(arduino);

fopen(arduino);
disp(fscanf(arduino, '%f'))
fclose(arduino);


and my arduino code:

In the setup:

Serial.begin(115200);


In the loop:

delay(100);
myFunction();


In myFunction:

while(!Serial.available()&gt;0)){}
int data = Serial.read();
Serial.println(data,DEC);


It seems like the 48 is an ascii code for something but it doesn't ever change when I change the 1 is the 3rd line of the matlab code to anything else.
Why am I always getting 48 with serial communication?


Your Arduino sketch (snippet) is reading a byte and writing the value, not the character. The value 48 in ASCII is the character '0'. 

The below loop() function will echo all the characters received. 

void loop()
{
  while (Serial.available()) {
     char c = Serial.read();
     Serial.println(c);
  }
}


This could be a good starting point when debugging what is actually written from MATLAB.