필터 지우기
필터 지우기

Matlab doesn't update when calling python API

조회 수: 9 (최근 30일)
David Haggerty
David Haggerty 2021년 5월 17일
댓글: Brian Harris 2022년 4월 4일
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
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
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.

카테고리

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

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by