当我从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
。
发布于 2017-04-18 07:02:50
您要做的是将函数包装在一个try
catch
块中,以便查看它是否返回任何错误。你的代码应该看起来像这样:
$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
}
https://stackoverflow.com/questions/43460845
复制相似问题