Sending an array from MATLAB to Arduino, and storing each entry

Hi everyone,

What is the easiest way to send an array, say M = [1,2,3], from MATLAB to the Arduino, and store the entries as seperate values in the Arduino?

I know it's possible to use fprintf to send a single number from MATLAB to the Arduino, and then use Serial.read to read that single number from the serial. I can't however, make it work with arrays.

Thanks Mattias

I am not familiar with Matlab's COM resources, but I would develop a message packet and send it to the Arduino. For example, if you want to send the values 10, 20, and 30, create a packet like:

10,20,30#

where the sharp sign (#) becomes a sentinel that marks the end of the packet and the commas serve as delimiters for the numbers. You could then use:

#define BUFFERSIZE 20
// Some code...
  char incomingData[BUFFERSIZE ];
  int index;
// probably more code...
  if (Serial.available() > 0) {
    index = Serial.readBytesUntil('#', incomingData, BUFFERSIZE - 1);
    incomingData[index] = '\0';  // Now it's a string
// the rest of the code...

The readBytesUntil() method reads characters until it reads the '#' character or 19 characters, whichever comes first. This prevents overflowing the input buffer. Once the data is in incomingData[], you can use strtok() and atoi() to pull the string apart and then assign the data into the array.

What is the easiest way to send an array, say M = [1,2,3], from MATLAB to the Arduino, and store the entries as seperate values in the Arduino?

Some serial servo test code where command values are captured for different servos.

//zoomkat 11-22-12 simple delimited ',' string parse 
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added 
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h> 
Servo myservoa, myservob, myservoc, myservod;  // create servo object to control a servo 

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

  //myservoa.writeMicroseconds(1500); //set initial servo position if desired

  myservoa.attach(6);  //the pin for the servoa control
  myservob.attach(7);  //the pin for the servob control
  myservoc.attach(8);  //the pin for the servoc control
  myservod.attach(9);  //the pin for the servod control 
  Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded
}

void loop() {

  //expect single strings like 700a, or 1500c, or 2000d,
  //or like 30c, or 90a, or 180d,
  //or combined like 30c,180b,70a,120d,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.println(readString); //prints string to serial port out

        int n = readString.toInt();  //convert readString into a number

        // auto select appropriate value, copied from someone elses code.
        if(n >= 500)
        {
          Serial.print("writing Microseconds: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
          if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
          if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
          if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
        }
        else
        {   
          Serial.print("writing Angle: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.write(n);
          if(readString.indexOf('b') >0) myservob.write(n);
          if(readString.indexOf('c') >0) myservoc.write(n);
          if(readString.indexOf('d') >0) myservod.write(n);
        }
         readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

econjack

Your code works! Thanks!

I'm a bit concerned though, since the data I'm going to send to the arduino, will be a number in between 0-1023. Can a number like this be stored as a 'char'?

You are not sending numbers, as such.

You are sending text. You are sending the character '1', then the character '0', then the character '2' and then the character '3'.

Each of those is a 1-byte character.

You could also send a two-byte integer, as an actual binary number. That would also work, and would require the sending of less bytes.

For a range of reasons, in a lot of cases people prefer to send characters.

michinyon: You are sending text. You are sending the character '1', then the character '0', then the character '2' and then the character '3'.

So how would it look, if I was to send the array M = [400,500,600] from MATLAB, and store each entry in the Arduino? I'm guessing something like

For MATLAB:

arduino=serial('COM4','BaudRate',9600); 
M = [400,500,600];
fopen(arduino); 
 fprintf(arduino,'%s',char(M)); 
fclose(arduino);

Arduino:

char matlabdata[20];
int index;

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

void loop() {
  if(Serial.available()>0){
    index = Serial.readBytesUntil('x',matlabdata,20); // where 'x' is just a random character, because I'm not going to use it
    matlabdata[index] = '\0'; 
    a = matlabdata[0];
    b = matlabdata[1];
    c = matlabdata[2];
  }
}

So after this, I'm hoping a = 400, b = 500 and c = 600. I'm not sure however, if the code is only going to store the first character, so a = 4, b = 5 and c = 6. Anyways, I'm not sure how to test it, since I can't look at the serial in the Arduino software, because MATLAB is using it (i get an error message).