Matlab doesn't update when calling python API

8 views (last 30 days)
Hello,
I am trying to implement a python API in MATLAB to read position data from a motion capture system for feedback control. The python API works in terminal perfectly, but when I try to call it in MATLAB the position fails to update, which means to me there is something about how MATLAB calls python functions that is static (which is supported by the fact that when I update the python code, I need to restart MATLAB for the changes to take effect). Is there a workaround for this? Or some coder settings that I can tweak?
My code is supplied below:
The API call is simply
dataTest = py.mocapPosition.readSys;
And the python code is
import owlXYZ
import sys
SERVER = '192.168.1.230'
o = owlXYZ.Context()
# connect to server with timeout of 10000000 microseconds
o.open(SERVER, "timeout=100000")
# initialize session
o.initialize("streaming=1")
def readSys():
evt = o.nextEvent(100)
newlist = []
while evt.type_id != owlXYZ.Type.FRAME:
evt = o.nextEvent(100)
if "markers" in evt:
for m in evt.markers:
newlist.append(str(m))
print(m)
return newlist
elif evt.type_id == owlXYZ.Type.ERROR:
print(evt.name, evt.data)
elif evt.name == "done":
# done event is sent when master connection stops session
print("done")
# end main loop
def close():
# end session
o.done()
# close socket
o.close()
The header file owlXYZ supplies all the declarations necessary to import data from the server. All help is appreciated!
  1 Comment
Brian Harris
Brian Harris on 4 Apr 2022
The "print(...)" statements from python don't flush to the matlab command line until the python object is destroyed OR... interestingly, if you have a non-semicolined assignment in matlab. Try this and see if you get your print statements coming through:
dataTest = py.mocapPosition.readSys;
% Note, no semi-colon.
a = 1
Note: despite the lack of stdout flushing, the code is running, you just don't get the command line feedback.

Sign in to comment.

Answers (1)

Pranjal Kaura
Pranjal Kaura on 29 Sep 2021
Hey David,
You could go through the following documentations, to know more about reloading python interpreter from MATLAB. This will help you update the modified module files(python) in MATLAB.
A workaround could be to output the position matrix from python to MATLAB using the 'pyrunfile' command and then using it in your feedback control model.

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!