pass inputs from python to matlab and return output back to python
9 次查看(过去 30 天)
显示 更早的评论
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)
0 个评论
回答(1 个)
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.
0 个评论
另请参阅
类别
在 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!