Redirecting Command Window output to stdout using tail

12 views (last 30 days)
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"

Accepted Answer

Walter Roberson
Walter Roberson on 6 Jul 2012
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 ?
  2 Comments
Jonathan
Jonathan on 6 Jul 2012
I honestly do not know how to display to the console associated with the .bat file.
Walter Roberson
Walter Roberson on 6 Jul 2012
Unfortunately I do not have access to MATLAB on an MS Windows machine, so I cannot test this.
On the other hand one of your other threads has the MEX workaround, so this approach is probably moot.

Sign in to comment.

More Answers (0)

Categories

Find more on Entering Commands in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!