MATLAB: Redirecting Command Window output to stdout using tail

batchcommand windowloglogfileoutputpausestderrstdouttail

I am trying to redirect the Command Window output to stdout and stderr when running MATLAB in batch mode. I am trying to achieve this by starting MATLAB, creating a log file using the -logfile command, finding its process ID, using tail to output the log file to standard output, pausing to let MATLAB finish before moving on, then killing the MATLAB process using the taskkill command, and then echoing that it's complete.
However, I have encountered many problems. The pause command pauses indefinitely. I need it to "un-pause" as soon as MATLAB has finished running. Is there a different command I should use? Also, the tail command is not displaying the information that's in the log file in standard output, as it should. When the log file is already created before hand, I do not receive an error message, tail just doesn't print to standard output, and if the log file is nonexistant when I run it, tail gives me an error: TAIL: can't open output.txt
Here is my code:
@echo off
echo "running..."
matlab -nosplash -nodisplay -minimize -logfile output.txt -r myScript
for /F "TOKENS=1,2,*" %%a in ('tasklist /FI "IMAGENAME eq MATLAB.exe"') do set MyPID=%%b
echo %MyPID%
tail -F output.txt
pause
taskkill /PID MyPID
echo "complete"

Best Answer

I am not much experienced with .bat files. It seems to me that when you run with -nodisplay and -r and do not use & after the command, that matlab would not return to the .bat file until matlab had finished executing.
At least that's what the case would be in Unix systems.
I do not understand why you create a log file and tail that, rather than just allowing the output to display to the console associated with the .bat file ?