problem: serial communication matlab to arduino uno

i tried to send data form mat-lab to arduino board through Bluetooth (HC-05) but the data received is wrong this is a sample program mat-lab code

b = Bluetooth('HC-05',1);
fopen(b);
x= 0;
for n = 1:50
   fprintf(b,'%s',double(x));
    disp(x);
    x= x+1;
    
end
fclose(b);

and this the arduino code

int ledPin=13;
double matlabData;

void setup() 
{
  pinMode(ledPin,OUTPUT);
  Serial.begin(9600);
}

void loop() 
{
   
   if(Serial.available()>0) // if there is data to read
   {
    matlabData=Serial.read(); // read data
    Serial.println(matlabData);
    if(matlabData==1)
      digitalWrite(ledPin,HIGH); // turn light on
    else if(matlabData==2)
      digitalWrite(ledPin,LOW); // turn light off
  }
}

and this is the result which as you see is wrong |500x267

How would I know from that picture that the data is wrong?

Maybe you need to use one of the examples in serial input basics to receive your data?

…R

the data should be received is 0 incremented every time by 1
so what i expected is
0
1
2
3
4
5
6

until it reaches 49 which is not actually done
and it clear from mat-lab code what should be received
thanks

superzeeka: and it clear from mat-lab code what should be received

It is much quicker for you to tell me what the problem is rather than for me to study Matlab code that I am not familiar with.

Have you tried the examples I gave you?

...R

Robin2: It is much quicker for you to tell me what the problem is rather than for me to study Matlab code that I am not familiar with.

Have you tried the examples I gave you?

...R

I read the topic, the problem is not with serial communication in general but with serial communication between arduino and matlab the problem is i cannot send to arduino(from matlab) any negative values or fractions

hi, i'm facing a problem with arduino matlab serial communication i can send positive integers to arduino from matlab and i can receive negative,fraction,positive, integers from arduino to matlab but the problem is when i try to send negative or fraction value from matlab to arduino it gives me wrong result this is the matlabcode

b = Bluetooth('HC-05',1);
fopen(b);
n=10;
    fprintf(b,double(n));
    q=fscanf(b,'%e',1);
        

fclose(b);

if i change n to any negative or fraction values it will give me wrong result

and this is the arduino code

  int b=0;
  double s=0;

    
    void setup()
    {
    Serial.begin(9600);
    }
     
    void loop()
    {
      b=5;
         if (Serial.available() >0) {
           s=Serial.read();
           b= b + s;
           Serial.println(b);
         
}  
    }

thank you

fopen(b);
n=10;
    fprintf(b,double(n));

Open the serial port, resetting the Arduino. Before it is ready, send it some data. Hmmm...

The third argument to fscanf() is what? NOT a constant.

You are sending a string, and reading the string one character at a time. You add 5 to the character (why?) and send the nonsense back. I fail to see the value in doing that.

the problem is i cannot send to arduino(from matlab) any negative values or fractions

You certainly can. You MUST read them properly, though. You are sending the value as a string, and expecting to read the value as the value you sent.

That's a bit like sending the value 1000 as One thousand, and expecting to read "One thousand" as a single character, and expecting the value of that character to be 1000. Not in your wildest dreams.

Go read Robin's stuff again. Pay attention this time.

superzeeka: the problem is i cannot send to arduino(from matlab) any negative values or fractions

The code I gave you a link to will read negative values or floating point values (or both). TRY IT.

If by fraction you mean "1/2" or "2/7" you will need to receive them as characters and write some code to convert them to numbers.

...R