Arduino uno and matlab sending data in real time

Hello,
I need to send data from matlab to arduino and then back. I'm using arduino uno and matlab R2015. I tried send data numbers one by one and it works. I receive first number back, because I don't have there loop it's only "test program" .But mine professor want to send numbers in matrix. So I must do some changes. Here is example of matrix(servalue= [0 1 2 3 4];). And when I want to send a matrix, matlab showed me this: (Warning: Unsuccessful read: A timeout occurred before the Terminator was reached..).
I tried almost everything, I changed terminator to CR to make it faster response, different Baudrate etc. But still same problem. I saw somewhere, that function Serial.read(); in arduino, read data one by one. So can be problem there?
Or do you have some advice please?
Sorry for my English.

Matlab one by one"works":

s=serial('COM7','BAUD', 9600);
fopen(s);
for m=1:10
servalue= input('Enter the value 1 to turn ON LED & 0 to turn OFF LED :');
fprintf(s,servalue);
end
i = fscanf(s, '%s');
disp(i);

Matlab matrix:

s=serial('COM7');
set(s, 'Baudrate', 9600);
set(s, 'Parity', 'none');
set(s, 'Databits', 8);
set(s, 'Stopbit', 1);
set(s, 'Terminator', 'CR');


fopen(s);

servalue= [0 1 2 3 4];
fprintf(s,servalue);
i = fscanf(s, '%s');
disp(i);
%display the properties of serial port object in MATLAB Window
disp(get(s,{'Type','Name','Port','BaudRate','Parity','DataBits','StopBits','Terminator'}));
disp('Character sent to Serial Port is:');
disp(servalue);

pause(3);
 
return;

fclose(s);
delete(s);
clear s;

Arduino code:

const int ledpin=13;
int recValue;

void setup()
{
Serial.begin(9600);
pinMode(13, OUTPUT);
delay(2500);
}

void loop()
{
  if(Serial.available()>0)
  {
    recValue=Serial.read();

    if (recValue == 1) // If use will send value 100 from MATLAB then LED will turn ON
     {
       digitalWrite(ledpin, HIGH);
       }
        if(recValue == 0) // If use will send value 101 from MATLAB then LED will turn OFF
         {
         digitalWrite(ledpin, LOW);
         
         }
     
     Serial.print("I received: ");
     Serial.println(recValue, DEC);
     delay(200);
      }
    }

Serial.read does read one byte at a time.

You need to store each byte in an array and then when you have all the data, parse it as a string by adding a final '/0' in the char array.

Basic serial "echo" test code that captures what is sent to the arduino from the serial monitor into a String variable, then the String is sent back to the serial monitor.

//zoomkat 6-29-14 Simple serial echo test
//type or paste text in serial monitor and send

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("Simple serial echo test"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the String readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured String 
    readString="";
  } 
}

Thank you for your response. I tried that, but for me doesn't work. I tried to change delay but it doesn't help. Matlab shows same warning. I tried in Matlab all the function for reading from serial port(fscanf, fread, fgets). But with the same results. I had in arduino in serial monitor set both NL & CR and 38400 baud rate, but when i changed it on 9600, it doesn't make any difference.

I can't figure whether you are having a problem receiving data on your Arduino or on Matlab.

If this is an Arduino problem have a look at the 2nd and 3rd examples in Serial Input Basics. They are simple reliable ways to receive data.

Am I correct to assume that Matlab is sending the data as ascii characters - i.e. human-readable rather than in binary format. ?

It is perfectly possible for an Arduino to receive binary data but you need to treat it a little differently.

...R

I actuallly had a similar a problem and found a partial solution to them using the following arduino and matlab codes respectively. I am using bluetooth however and the concept is the same with COM port.

int dist[20]={};
int ang[20]={};

int i;

boolean newData=false;
boolean flag=false;
boolean a_dist=false;
boolean a_ang=false;
int ledr=7;
int ledg=4;
int len;

void setup() {

 Serial.begin(9600);
 pinMode(ledr,OUTPUT);
 pinMode(ledg,OUTPUT);
}

void loop() {


 //check if there is data to be read
  if (Serial.available()>0)
  {
    //check if the length has been received
    if (flag==false)
    {
      len=Serial.read();
      flag=true;
    }

    else
    {   
      //fill up array distances
      if (a_dist==false)
      {
        dist[i]=Serial.read();

        i++;
        if (i==len)
        {
          a_dist=true;
          i=0;
        }
      }
      
      else if (a_ang==false)
      {
        //fill up angles array
        ang[i]=Serial.read();
        

        
        i++;
        if (i==len)
        {
          a_ang=true;
          i=0;
        }
      }
    }
    if ((flag==true)&&(a_dist==true)&&(a_ang==true))
    {
      newData=true;
    }
  }
 

  if (newData==true){
    Serial.println(len);
    //print out distance array 
    for (int x=0;x<len;x++)
    {
      Serial.println(dist[x]);
    }
    //print out angles array
        for (int x=0;x<len;x++)
    {
      Serial.println(ang[x]);
    }

    newData=false;
    flag=false;
    a_dist=false;
    a_ang=false;
    
  }

}
b = Bluetooth('HC-05',1);

fopen(b);
d=[20,10,20,10];
a=[90,-90,-90,-90];

len=length(d);

for i=1:len
    if sign(a(i))==-1
        a(i)=360+a(i);
    end
end
       
fwrite(b,len);
fwrite(b,d);
fwrite(b,a);



r_len=fscanf(b,'%d');

for i=1:1:len
    r_dist(i)=fscanf(b,'%d');
end

for i=1:1:len
    r_ang(i)=fscanf(b,'%d');
end
fclose(b);