Run a Python Script to Stream Data
10 views (last 30 days)
Show older comments
Tl;dr: Is there a way to establish a Socket in Python and then constantly pass the socketed data to Matlab?
Currently, I am reading data from a sensor in a python script. In short, the Python script reads from a UDP Port (using sockets) and then decodes the data (the data is passed in UTF-8 format and I don't believe Matlab plays nicely with UTF). I am having trouble now getting the data into Matlab. Specifically, I need to be able to run a Matlab function which then initiates the Python script and reads the real-time data from Python (my assignment is to read the data in Matlab so I'm open to other methods but this is the way I have found the most progreess). For now, I just need to plot the data but I will need to manipulate it later. For anyone who is curious, the data stream method is called an OSC Stream.
I have found a method by calling a Python script repeatedly from within Matlab but this current method has me reinitializing the socket every time I want to connect. This does not work as the sampling frequency is roughly 100 Hz and constantly reestablishing a socket causes issues passing the data.
That is all to say, how can I best read data from a Python live stream or otherwise get the data into a Matlab workspace?
0 Comments
Answers (1)
Mohammad Sami
on 26 Mar 2020
You should split your code into two functions.
You can create a python function that instantiates the socket and return the socket to matlab.
This way the socket will be kept in memory.
Then create another python function that accepts the socket variable and reads and returns the data.
Finally when your code is done you can close the socket and clear it.
2 Comments
See Also
Categories
Find more on Call Python from MATLAB in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!