Matlab doesn't update when calling python API
8 次查看(过去 30 天)
显示 更早的评论
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 个评论
Brian Harris
2022-4-4
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.
回答(1 个)
Pranjal Kaura
2021-9-29
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Call MATLAB from Python 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!