2017-04-17 81 views
0

時候當我打電話MATLAB從PowerShell中,它工作正常:返回值調用Matlab

$command = "path-to-matlab.exe" 
$args = "-nosplash -nodesktop -r run('path-to-my-mfile.m');exit();" 
$process = Start-Process -Wait -PassThru $command $args 
write-output $process.ExitCode 

(類似this question here

然而,當有一個在MATLAB的錯誤,怎麼能PowerShell的知道嗎?

我試過了從MATLAB的exit(1),但變量$process.ExitCode仍然返回0

我也試過從MATLAB內部fprintf('some error message'),但它不打印到PowerShell控制檯,但只打印在MATLAB窗口。

回答

0

您想要做的是將函數包裝在trycatch塊中以查看它是否返回任何錯誤。你的代碼應該看起來像這樣:

$command = "path-to-matlab.exe" 
$args = "-nosplash -nodesktop -r run('path-to-my-mfile.m');exit();" 
try { 
    $process = Start-Process -Wait -PassThru $command $args 
    Write-Host "Success!" 
} 
catch { 
    $ErrorMessage = $_.Exception.Message 
    return $ErrorMessage 
    pause 
}