How To Get The Output Of A Matlab Script In Python
I am performing a matlab calculation through python. For this purpose, I use the following command: retcode=subprocess.call['matlab','-nosplash','nodesktop','-wait','-r','run('matl
Solution 1:
I suggest you use Popen
. I don't have matlab, so I can't test your exact command, but try this:
import subprocess
cmd = ["matlab", "-nosplash", "no desktop", "-wait", "r", "run('matlabscript.m')","quit;"]
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(output, error) = proc.communicate()
iferror:
print"error:", errorprint"output:", output
# if you need this:
retcode = proc.returncode
If a huge amount of output is produced then this could potentially crash, its a judgement only you can make.
Note that subprocess.communicate()
is also used for stdin
.
Solution 2:
You could use subprocess.check_output
which will wait for the result:
subprocess.check_output(["matlab","-nosplash","nodesktop","-wait","-r","run('matlabscript.m')","quit;"])
Solution 3:
If you're using Matlab >= R2014b, you can simply use the Matlab Engine for Python provided by Mathworks.
>>>import matlab.engine>>>worker = matlab.engine.start_matlab()>>>a = worker.eval('rand(10,10)')>>>print(a)
[[0.8147236863931789,0.15761308167754828,0.6557406991565868,0.7060460880196088,0.43874435965639824,0.27602507699857837,0.7512670593056529,0.8407172559836625,0.35165950706299676,0.07585428956306361],[0.9057919370756192,0.9705927817606157,0.035711678574189554,0.031832846377420676,0.3815584570930084,0.6797026768536748,0.2550951154592691,0.25428217897153105,0.8308286278962909,0.05395011866660715],[0.12698681629350606,0.9571669482429456,0.8491293058687771,0.27692298496088996,0.7655167881490024,0.6550980039738407,0.5059570516651424,0.8142848260688164,0.5852640911527243,0.5307975530089727],[0.9133758561390194,0.4853756487228412,0.9339932477575505,0.04617139063115394,0.7951999011370632,0.16261173519463057,0.699076722656686,0.2435249687249893,0.5497236082911395,0.7791672301020112],[0.6323592462254095,0.8002804688888001,0.6787351548577735,0.09713178123584754,0.1868726045543786,0.11899768155837664,0.8909032525357985,0.9292636231872278,0.91719366382981,0.934010684229183],[0.09754040499940952,0.14188633862721534,0.7577401305783334,0.8234578283272926,0.48976439578823106,0.49836405198214295,0.9592914252054443,0.34998376598480874,0.28583901882037355,0.12990620847373013],[0.2784982188670484,0.421761282626275,0.7431324681249162,0.694828622975817,0.4455862007108995,0.9597439585160811,0.5472155299638031,0.19659525043120818,0.7572002291107213,0.5688236608721927],[0.5468815192049838,0.9157355251890671,0.39222701953416816,0.31709948006086053,0.6463130101112646,0.3403857266661332,0.13862444282867914,0.25108385797603106,0.7537290942784953,0.4693906410582058],[0.9575068354342976,0.7922073295595544,0.6554778901775566,0.9502220488383549,0.7093648308580726,0.5852677509797773,0.14929400555905747,0.6160446761466392,0.38044584697535666,0.011902069501241397],[0.9648885351992765,0.959492426392903,0.17118668781156177,0.03444608050290876,0.7546866819823609,0.22381193949113698,0.25750825412373646,0.47328884890272926,0.5678216407252211,0.3371226443988815]]
As an alternative, switch to GNU Octave and simple use Oct2Py.
Post a Comment for "How To Get The Output Of A Matlab Script In Python"