Run a Python Script to Stream Data
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Call Python from MATLAB 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!