Redirecting Command Window output to stdout using tail

Asked by Jonathan on 5 Jul 2012
Latest activity Commented on by Walter Roberson on 6 Jul 2012

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"

0 Comments

Jonathan

Products

No products are associated with this question.

1 Answer

Answer by Walter Roberson on 6 Jul 2012
Accepted 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 ?

2 Comments

Jonathan on 6 Jul 2012

I honestly do not know how to display to the console associated with the .bat file.

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.

Walter Roberson

Contact us