如何从python捕获matlab函数的输出 [英] How to catch output of a matlab function from python
问题描述
我运行下面的python代码来评估数组的平均值:
I'm running the python code below which evaluates mean of an array:
def matlab_func1(array):
p = os.popen('matlab -nodesktop -nosplash -r "mean('+str(array)+');exit"')
while 1:
line = p.readline()
if not line:
break
print line
matlab_func1([1,2,3])
从下面的matlab脚本,可以看出输出返回到y。我想从python捕获这个输出。
From the matlab script below, it can be seen output return to y. I want to catch this output from python.
function y = mean(x,dim)
...
...
end
解决方案必须适用于其他matlab函数。 'mean'函数只是一个例子。
The solution must be applicable to the other matlab function. The 'mean' function is just an example.
推荐答案
使用 fprintf
将所需的文本写入 stderr
。只需在开头添加一个额外的参数 2
。
Use fprintf
to write needed text into stderr
. Just add an extra argument 2
in the beginning.
import subprocess
import os
def matlab_func1(array):
p = subprocess.Popen(['/home/user/Matlab/bin/matlab', '-nodesktop', '-nosplash', '-r "m = mean(' + str(array) + ');fprintf(2, \'%d\\n\',m);exit" >/dev/null'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while 1:
try:
out, err = p.communicate()
except ValueError:
break
print 'hello' + err
matlab_func1('[1,2,3]')
需要注意的几件事:
- 将Python命令更改为
subprocess.Popen
,允许stderr
管道。 - 在Matlab命令中,使用
fprintf
将所需信息写入stderr
。这可以分离代码输出从Matlab的标题行。 - 回到Python,使用
Popen.communicate()
捕获stderr
输出。 - 异常
ValueError
处理Matlab的退出事件(p
已关闭) 。
- Changed the Python command to
subprocess.Popen
, which allowsstderr
piping. - In Matlab command, use
fprintf
to write wanted info intostderr
. This can separate code ouput from Matlab's header lines. - Back into Python, use
Popen.communicate()
to catchstderr
output. - The exception
ValueError
handles exit event of Matlab (p
is closed).
编辑:
表示Matlab函数是
say a Matlab function is
function [y, z] = foo(x)
y = x+1;
z = x*20;
end
关键是使用 fprintf
抛出输出,而做所有其他事情,你总是做正常在Matlab。
The point is use fprintf
to throw the output, while doing all other things as you always do normally in Matlab.
p = subprocess.Popen(['/home/user/Matlab/bin/matlab', '-nodesktop', '-nosplash', '-r "[y, z] = foo(' + str(array) + ');for ii=1:length(y) fprintf(2, \'%d %d\\n\',y(ii),z(ii)); end; exit" >/dev/null'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
方法2-独立脚本
首先创建一个新的 caller.m
脚本
[y, z] = foo(x);
for ii=1:length(y)
fprintf(2, '%d %d\n',y(ii),z(ii));
end
注意 x
是从Python调用时赋值的;脚本共享相同的堆栈。
Note that x
is to be assigned when calling from Python; scripts share the same stack. (Remember NOT to clear
the workspace in the caller script. )
然后,从Python
Then, call the script from Python
p = subprocess.Popen(['/home/user/Matlab/bin/matlab', '-nodesktop', '-nosplash', '-r "x=' + str(array) + ';caller; exit" >/dev/null'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
将Matlab结果存储在Python变量中
-
当通过
stdout
/stderr
管道传递数据时:Storing Matlab result in a Python variable
When passing data through
stdout
/stderr
pipe:请参阅此和
subprocess.check_output()
。Refer to this and
subprocess.check_output()
.在处理
double
或二进制文件等严重数据时:When handling serious data like
double
or binary:使用Matlab将数据写入外部文件。然后用Python读取这个文件。应该定义一种协议,利用该协议彼此进行双方通话。
Write the data into an external file with Matlab. Then read this file with Python. A protocol with which both side talk with each other should be defined.
这篇关于如何从python捕获matlab函数的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!