MATLAB: Matlab Sending Arrays to Arduino with For loop

arduinoarrayserial communicationsinewave

Hello Matlab Users,
Right now I'm trying to send an array out of matlab to arduino. Below is my code. When i run this code, nothing seems to happen. Is this b/c of the for loop, or is there a different reason? Thanks for all the help.
t = 0:1:40;
A =500;
f =500;
fs = 8000;
x = A*sin(2 * pi * f / fs * t) + A;
plot(t, x);
arduinocom = serial('COM4', 'BaudRate', 9600);
fopen(arduinocom);
for i = 1:length(x)
a = x(i);
fprintf(arduinocom, '%d', a);
end
fclose(arduinocom);

Best Answer

You are not putting in any delimiters between the numbers, so [12 34 567] would be sent as '1234567'
For efficiency you should consider not using a loop and instead using
fprintf(arduinocom, '%d ', x) %entire variable
However if the vector to be sent is large that could overflow the serial output buffer. It is possible to increase the size of the buffer: https://www.mathworks.com/help/matlab/matlab_external/outputbuffersize.html
Related Question