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

2 Ansichten (letzte 30 Tage)
em
em am 10 Feb. 2015
Beantwortet: Bo Li am 10 Feb. 2015
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)

Antworten (1)

Bo Li
Bo Li am 10 Feb. 2015
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.

Community Treasure Hunt

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

Start Hunting!

Translated by