2017-04-17 83 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 
}