Run a Python Script to Stream Data

조회 수: 4 (최근 30일)
Dominic Tanzillo
Dominic Tanzillo 2020년 3월 26일
댓글: Mohammad Sami 2020년 5월 26일
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?

답변 (1개)

Mohammad Sami
Mohammad Sami 2020년 3월 26일
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
Dominic Tanzillo
Dominic Tanzillo 2020년 5월 26일
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 2020년 5월 26일
What part are you having problems with. What have you tried so far ?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by