Send array from MATLAB to Arduino

84 views (last 30 days)
Afzal Ali
Afzal Ali on 8 Feb 2017
Commented: Walter Roberson on 4 Nov 2018
I want to send an array of three floating point numbers to Arduino from MATLAB. I'm trying to check if these values have been received by Arduino by sending these values back from the Arduino to MATLAB. It seems that Arduino only reads the first element in the array correctly.
My array, 'parameters' is:
measurement_interval = 5.0;
ec_ref_thickness = 2.0;
e_ref_thickness = 3.0;
parameters = [measurement_interval ec_ref_thickness e_ref_thickness];
I established communication with Arduino as:
arduino = serial('COM4');
I send it to Arduino using:
fprintf(arduino, '%d', parameters);
fprintf(arduino, '\n');
And in Arduino I have:
char parameters[3] = { '0','0','0'};
void setup()
while (Serial.available() == 0)
if (Serial.available() > 0)
for (int i=0; i < 3 ; i++)
parameters[i] =;
I send back from the Arduino over the serial port as:
void loop()
Serial.print(" ");
Serial.print(" ");
And read in MATLAB as:
output = fscanf(arduino);
'output' should be [5.0 2.0 1.0]. However, what I get is [5 ÿ ÿ]
So only the first element '5.0' is returned correctly.

Answers (3)

german rodriguez
german rodriguez on 31 Oct 2018
amigo pudiste solucionar el problema ???. ya que tengo el mismo problema. No logro enviar un vector desde matlab a arduino de ningun modo

Walter Roberson
Walter Roberson on 1 Nov 2018 returns -1 if no bytes are available.
When you check for serial available the way you do, you are testing for 1 or more bytes available, not for 3 or more.
That said, sending to the arduino over usb will not happen until a timeout or until the buffer is full, because the usb driver assumes that you might be sending more data that it should buffer all together for efficiency. So you should be sending groups of 4 bytes.
Hmmm, but you are flushing whatever else might be in the same usb packet instead of eating the newline

german rodriguez
german rodriguez on 1 Nov 2018
Edited: german rodriguez on 1 Nov 2018
Lo estoy enviando de esta manera MATLAB
dato=[5 11 33 23 14 65 33 11 10 6];
En arduino lo recibo
float lectura[10];
int N=10;
if (Serial.available()>5)
for(int r=0;r<N;r++)
  1 Comment
Walter Roberson
Walter Roberson on 4 Nov 2018
MATLAB's documentation does not define what happens if you have a serial() object, s, and you use
fprintf(s, NumericArray)
Probably it would be treated the same as
fprintf(s, char(uint8(NumericArray)) )
which in turn would be the same as
fprintf(s, '%s\n', char(uint8(NumericArray)) )
Notice the line terminator would be sent, so a minimum of 11 bytes would be sent for a 10 entry buffer.
Your code is not expecting anything after the N items, so you could have trouble with the unexpected newline.
You should probably code
fprintf(s, '%s', NumericArray)
or better yet,
fwrite(s, data, 'uint8')

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!