2015-09-16 77 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