我正在通过python执行matlab计算。为此,我使用以下命令:
retcode=subprocess.call["matlab","-nosplash","nodesktop","-wait","-r","run('matlabscript.m')","quit;"])
通过在python中运行此命令行,打开了一个matlab会话,我可以开始执行前面提到的脚本'matlabscript.m'
。有没有办法将此脚本的执行输出到python中? retcode,除非是一个数字,否则不包含任何内容。我试图使用subprocess.check_output,但我无法得到任何东西。基本上,当我执行前面提到的sript时,将创建并指定casename。
答案 0 :(得分:1)
我建议你使用Popen
。我没有matlab,所以我无法测试你的确切命令,但试试这个:
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()
if error:
print "error:", error
print "output:", output
# if you need this:
retcode = proc.returncode
如果产生了大量的输出,那么这可能会崩溃,只有你能做出判断。
请注意,subprocess.communicate()
也用于stdin
。
答案 1 :(得分:0)
您可以使用等待结果的subprocess.check_output
:
subprocess.check_output(["matlab","-nosplash","nodesktop","-wait","-r","run('matlabscript.m')","quit;"])
答案 2 :(得分:0)
如果您使用的是Matlab> = R2014b,则只需使用Mathworks提供的Matlab Engine for Python即可。
>>> 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]]
作为替代方案,切换到GNU Octave并简单地使用Oct2Py。