2015-09-16 78 views
1

我叫蟒蛇Matlab代码为从MATLAB优雅地返回回蟒蛇

matlab_cmd_string = MatlabExePth+ " -nosplash -nodesktop -wait -logfile FileIoReg_MatlabRemoteRun.log -minimize -r " 
fname = 'CompareMse ' 
mat_cmd = matlab_cmd_string + fname + ", exit\"" 

它被译为

“C:\ Program Files文件\ MATLAB \ R2013b \ BIN \ matlab.exe -nosplash -nodesktop -wait -logfile FileIoReg_MatlabRemoteRun.log -minimize -r CompareMse,退出”

的Matlab代码,它的工作,然后打印错误并停止Ë使用以下构造的执行:

if(mse> thr) 
    error('mse has increased'); 
end 

但是,该控件不会返回给python。

我尝试下面的命令在python:

msg=subprocess.check_output(mat_cmd,stderr=subprocess.STDOUT,shell=False) 

味精来自空控制台窗口不到风度显示任何东西作为控制不回来。同样的,下面的方法:

proc = subprocess.Popen(mat_cmd , stdout=subprocess.PIPE, shell=True) 
out, err = proc.communicate() 
output = out.upper() 
proc.returncode  

如果我在MATLAB写以下,

if(mse> thr) 
    warning('mse has increased'); 
    return 
end 

我得到控制权交还给蟒蛇有以下几点:

msg=subprocess.check_output(mat_cmd,stderr=subprocess.STDOUT,shell=False) 
proc = subprocess.Popen(mat_cmd , stdout=subprocess.PIPE, shell=True) 
    out, err = proc.communicate() 
    output = out.upper() 
    proc.returncode 

味精,出显示为“”,err is NONE,和proc.returncode

什么是需要的是在Matlab功能:

for i=1:3 
% Some code here 
if(mse> thr) 
     [print error,return user defined exit code and error message back to python variable] 
if (mse_new >mse_old) 
     [print warning,do not return, but capture warning back to 
     Python variable] 
% some code here 

警告的困难是,如果警告情况发生在循环迭代1,而不是第二次和第三次,Python应该能够理解,Matlab代码没有错误,但只有一个警告,应该捕获(并且matlab不应该在for循环的迭代1处退出,但完成所有迭代)

有什么想法?
sedy

+0

你是怎么调用Matlab的?一旦停止执行,流程应该返回到父级(Python)。 –

+0

@Eugene请看我的第一个报价,其中显示的命令.. – user915783

+0

这不是代码。 Python中的线是什么? –

回答

1

尝试使用subprocess.check_output(*popenargs, **kwargs)。您可以捕获任何给定命令的输出。检查Python中的Python 2.7 subprocess doc here

import subprocess 
msg = subprocess.check_output([MatlabExePth, "-nosplash", "-wait", "-logfile", "FileIoReg_MatlabRemoteRun.log", "-minimize", "-r", fname, ", exit"]) 
print msg