Friday, November 26, 2021

Check if the MATLAB instance you created has exited

When you run MATLAB from a Windows bat file and want to know if it has finished its job and exited (without considering other already open MATLAB instance), you can use the following:

start matlab.exe -nosplash -nodesktop -noFigureWindows
set pid=not found
for /f "tokens=2" %%a in ('tasklist^|find /i "matlab") do (set pid=%%a)
echo matlab pid = %pid%

:waitForMatlabToExit
timeout /t /5 /nobreak
tasklist /fi "pid eq %pid%" | find ":">nul
if errorlevel 1 goto waitForMatlabToExit
echo Matlab exited
exit /b

No comments:

Post a Comment