0

When I call MATLAB from PowerShell, it works fine:

$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

(Similar to this question here)

However, when there's an error in MATLAB, how can PowerShell know?

I tried exit(1) from MATLAB, but the variable $process.ExitCode still returns 0.

I also tried fprintf('some error message') from within MATLAB, but it doesn't get printed to the PowerShell console, but only printed in MATLAB window.

Community
  • 1
  • 1
Hendrata
  • 101
  • 1

1 Answers1

0

What you'll want to do is wrap the function in a try catch block in order to see if it returned any errors. Your code should end up looking like this:

$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
}
henrycarteruk
  • 12,708
  • 2
  • 36
  • 40
Bryce McDonald
  • 1,760
  • 12
  • 22