MATLAB: Real time data acquisition from AVR in serial communication

real time com port communication

i am gathering data from Razor IMU with ADXL345. when i connect the micro controller, it is in communication port and in putty i have to select the baudrate of 57600. then a message comes as followed:
=~=~=~=~=~=~=~=~=~=~=~= PuTTY log 2013.01.04 04:24:02 =~=~=~=~=~=~=~=~=~=~=~=
9DOF IMU Firmware v22
==========================
[1]Accelerometer: ADXL345
[2]Magnetometer: HMC5883
[3]Gyroscope: ITG-3200
[4]Raw Output
[5]Change Baud Rate: 57600bps
[Ctrl+z]Toggle Autorun
[?]Help
in this case i have to press "1" and the data of accelerometer comes as followed:
x= 5, y= 41, z= 230
x= 5, y= 41, z= 231
x= 5, y= 41, z= 230
x= 5, y= 39, z= 230
x= 6, y= 42, z= 231
x= 7, y= 41, z= 230
x= 7, y= 41, z= 230
x= 4, y= 41, z= 231
x= 6, y= 41, z= 232
x= 6, y= 41, z= 231
x= 5, y= 41, z= 229
x= 6, y= 41, z= 231
x= 6, y= 41, z= 229
x= 6, y= 40, z= 229
x= 5, y= 42, z= 230
x= 6, y= 43, z= 230
……………..
i wrote the code in matlab as followed but it isn't working…
s1 = serial('COM4'); %define serial port
s1.BaudRate=57600
fopen(s1)
display (s1)
all i want to do to have a variable to assign the array and after that i want to manipulate as i require in real time.

Best Answer

You need to fread() or fgetl() to read the data.
Although it would be possible to write your code in such a way that you define a variable and the variable "automatically" keeps getting updated as more data comes in, that is usually not a good design. It is usually better to establish a bytes received callback that reads a line and extracts the information and calls a function to process the new information.
There are also some quite reasonable program structures that involve updating a buffer with the new information and then returning, and having the program check to see if new information has arrived.
But having the information variables themselves silently update through the callback is usually a bad idea, as the variables could end up being updated right in the middle of being accessed for calculation purposes.