在python中使用什么函数进行输出,如何从python捕获matlab函数的输出

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])

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

The solution must be applicable to the other matlab function. The 'mean' function is just an example.

解决方案

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]')

A few things to note:

Changed the Python command to subprocess.Popen, which allows stderr piping.

In Matlab command, use fprintf to write wanted info into stderr. This can separate code ouput from Matlab's header lines.

Back into Python, use Popen.communicate() to catch stderr output.

The exception ValueError handles exit event of Matlab (p is closed).

EDIT:

for a function that gives multiple outputs

say a Matlab function is

function [y, z] = foo(x)

y = x+1;

z = x*20;

end

The point is use fprintf to throw the output, while doing all other things as you always do normally in Matlab.

Method 1 - in-line script

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)

Method 2 - standalone script

First create a new caller.m script

[y, z] = foo(x);

for ii=1:length(y)

fprintf(2, '%d %d\n',y(ii),z(ii));

end

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. )

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)

Storing Matlab result in a Python variable

When passing data through stdout / stderr pipe:

When handling serious data like double or binary:

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值