我执行通过Python中的MATLAB计算。 为此,我用下面的命令:
retcode=subprocess.call["matlab","-nosplash","nodesktop","-wait","-r","run('matlabscript.m')","quit;"])
通过运行在python这个命令行中,一个MATLAB会话打开,我就可以开始执行上述脚本'matlabscript.m'
。 有没有办法让这个脚本放到蟒蛇执行的输出? RETCODE,不包含任何东西,除非一个号码。 我曾尝试使用subprocess.check_output代替,但我不能得到任何东西。 基本上,当我执行上述sript一个casename将被创建并指定。
我建议你使用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
。
你可以使用subprocess.check_output
将等待结果:
subprocess.check_output(["matlab","-nosplash","nodesktop","-wait","-r","run('matlabscript.m')","quit;"])
如果你用Matlab> = R2014b,你可以简单地使用Matlab引擎为Python 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]]
作为替代方案,切换到GNU八度,使用简单Oct2Py 。