Run a Python Script to Stream Data

10 views (last 30 days)
Dominic Tanzillo
Dominic Tanzillo on 26 Mar 2020
Commented: Mohammad Sami on 26 May 2020
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?

Answers (1)

Mohammad Sami
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
Dominic Tanzillo
Dominic Tanzillo on 26 May 2020
Thank you for this reply. I attempted to find a different solution but I am running into a wall. For sending and returning the socket between MATLAB and Python, would you recommend JSON files or some other solution?
Mohammad Sami
Mohammad Sami on 26 May 2020
What part are you having problems with. What have you tried so far ?

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!