gpt4 book ai didi

python - 如何在Python中获取matlab脚本的输出

转载 作者:太空宇宙 更新时间:2023-11-03 17:38:22 26 4
gpt4 key购买 nike

我正在通过 python 执行 matlab 计算。为此,我使用以下命令:

retcode=subprocess.call["matlab","-nosplash","nodesktop","-wait","-r","run('matlabscript.m')","quit;"])

通过在 python 中运行此命令行,将打开一个 matlab session ,我可以开始执行上述脚本 'matlabscript.m'。有没有办法将这个脚本的执行输出获取到python中? retcode,不包含任何内容,除非是单个数字。我尝试使用 subprocess.check_output 代替,但我什么也得不到。基本上,当我执行上述脚本时,将创建并指定一个案例名称。

最佳答案

我建议您使用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

关于python - 如何在Python中获取matlab脚本的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30895005/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com