如何在python中获取matlab脚本的输出 [英] How to get the output of a matlab script in python
问题描述
我正在通过python执行matlab计算.为此,我使用以下命令:
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('matlabscript.m')","quit;"])
通过在python中运行此命令行,将打开一个matlab会话,我可以开始执行上述脚本'matlabscript.m'
.有没有办法将此脚本的执行结果输出到python中? retcode,除非包含单个数字,否则不包含任何内容.我尝试使用subprocess.check_output代替,但是我什么也没得到.基本上,当我执行上述代码时,将创建并指定案例名称.
by running this command line in python, a matlab session opens and I can start to execute the aforementioned script 'matlabscript.m'
. Is there a way to get the output of the execution of this script into python? retcode, does not contain anything unless a single number. I have tried to use subprocess.check_output instead, but I cannot get anything. Basically, when I execute the aforementioned sript a casename is going to be created and specified.
推荐答案
我建议您使用Popen
.我没有matlab,因此无法测试您的确切命令,但是请尝试以下操作:
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()
if error:
print "error:", error
print "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.
请注意,subprocess.communicate()
也用于stdin
.
这篇关于如何在python中获取matlab脚本的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!