Redirecting Command Window output to stdout using tail

9 次查看(过去 30 天)
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"

采纳的回答

Walter Roberson
Walter Roberson 2012-7-6
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 个评论
Jonathan
Jonathan 2012-7-6
I honestly do not know how to display to the console associated with the .bat file.
Walter Roberson
Walter Roberson 2012-7-6
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Entering Commands 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by