How can I store sensor data from serial port?
25 views (last 30 days)
Situation is, that I have MATLAB connected to an Arduino. The Arduino is sending with perhaps 500 Hz data to MATLAB. Different data, so for example temperature, position, current etc. ...
This data is a String and begins with an identifier, that MATLAB is able to know, which message is for which variable in MATLAB. Now, the task is to store this variables in a vector. Problem is, that I can’t know how long this vector will be. So please renounce to explain, how to create a vector with 1000 entries and then a new one ....
I know the function animatedline(), this function can plot sensor data and store this data in a vector. In fact, I need exact this, but without plotting. Why?
The main task of the program is to receive the messages from the Arduino, store them and calculate a response, e.g. to switch on a fan, if the temperature goes to high and send this response message back to the µC (Of course, you will not need for this example MATLAB. It was just an example.)
The second task is, that MATLAB plot also the data, e.g. the temperature that was measured. But MATLAB is much to slow, for do this in one in instance. MATLAB needs a lot time to plot something. Idea is, that I use two instances. One for receiving, storing and responding on the messages and the second one to plot the received messages.
Means, that it could be necessary to store the data in an external file, that the second instance is able to read the data the first received. But this task may not need too much time, that slow down the performance of the first instance, because the idea for using a second instance is, that the first instance is very quick and can response in real time to 500 Hz messages.
Have somebody an idea to:
1) Store the data from the serial port in:
1.1) a vector or table
1.2) in an external file, without losing a lot time
2) Other way to hand over the data from one instance to an other
Siddharth Bhutiya on 19 Nov 2018
To store the data into a vector you can start with an empty vector and then keep adding values to it.
data = ;
data = [data; new_data];
However, if your end goal is to send the data to another MATLAB instance that will do the plotting for you, then you can setup a TCPIP client/server pair.
Your data generating session would act as a client and write the data to it. The other session will act as the client that will recieve the data and plot when sufficient data is available.
You can use the BytesAvailable trigger to plot the data when a fixed amount of bytes are available. Below is a link that demonstrates how to do it.