필터 지우기
필터 지우기

pass inputs from python to matlab and return output back to python

조회 수: 11 (최근 30일)
em
em 2015년 2월 10일
답변: Bo Li 2015년 2월 10일
I have a python flask restful API to accept web requests in JSON format.
{
"imageurl":"http://cf067b.medialib.glogster.com/media/ea/ea717410f781940999f24dadaeb0b547dc9aa1289dc17b98ee4d134cec74e73c/t-shirt-in-modal-stretch-png.png"
"itemlist": [item1, item2, item3]
}
I want to pass the contents of imageurl and itemlist as inputs to a Matlab function. I used subprocess to pass parameters to Matlab function. Here is how far I got:
@app.route('/testMatlab',methods = ['POST'])
def testMatlab():
url=request.json['imageurl']
taglist=request.json.get('taglist',[])
solve_it(url,taglist)
return jsonify(out)
def solve_it(arg1,arg2):
# Modify this code to run your matlab code
proc=subprocess.call(["matlab -nosplash -nodisplay -wait -r \"myFunction(\'%s\',\'%s\')\"" % (arg1, arg2)],shell=True, stdin=subprocess.PIPE, stout=subprocess.PIPE);
# Read result from file
out, err = proc.communicate()
print(out)
Right now myFunction.m simply returns taglist itself.
How can I return the output of myFunction as a string in JSON. Suppose the results of myFunction is out, I want to pass this results in JSON back to web request, e.g.
return jsonify(out)

답변 (1개)

Bo Li
Bo Li 2015년 2월 10일
In R2014b, the MATLAB Engine for Python provides a Python package for Python to call MATLAB functions:
Using Python Engine, you still need to convert the result into JSON types.

카테고리

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