调用matlab时返回值

时间:2017-04-17 22:47:49

标签: matlab powershell

当我从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窗口中。

1 个答案:

答案 0 :(得分:0)

您要做的是将函数包装在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
}