How do I get MATLAB running in batch mode to *only* output the results of disp, fprintf, etc. and not the headers and >> prompts?
조회 수: 26 (최근 30일)
이전 댓글 표시
I have a MATLAB 2012b script called script.m. I would like to run this script in batch mode and direct the terminal output to a file. The script is not on the MATLAB path, so this command will not work:
matlab -nosplash -nodesktop -r script.m
If I run a command like this:
matlab -nosplash -nodesktop < script.m > OUTPUT.txt
I get a file OUTPUT.txt that looks like this:
< M A T L A B (R) >
Copyright 1984-2012 The MathWorks, Inc.
R2012b (8.0.0.783) 64-bit (glnxa64)
August 22, 2012
To get started, type one of these: helpwin, helpdesk, or demo.
For product information, visit www.mathworks.com.
>> >> >> >> >> >> >> >> >> >> >> >>
...
My output from fprintf, disp, etc.
which is problematic because I don't want all of the garbage at the top, e.g. the headers, console prompts (>>), etc. Is there no way to prevent MATLAB from writing this output and force the program to only write the output of functions like fprintf, disp, etc.?
The makefile runs hundreds of MATLAB programs, and it can't write the header output every time it runs because that ruins my output.
Any ideas? I doubt it's relevant, but all of my scripts do include an exit command at the end.
댓글 수: 0
채택된 답변
Walter Roberson
2013년 2월 17일
I see you are using Linux, glnxa64. The following should work on Linux or OS-X:
$ matlab -nosplash -nodesktop -r "try; cd $PWD; script; catch; end; exit";
but would require slight modification if your matlabpath does not include "."
Alternately, modify all your disp() to fprintf, and then modify all the fprintf that are going to standard output to go to standard error instead:
fprintf(....)
or
fprintf(1, ....)
to become
fprintf(2, ....)
Then,
$ matlab -nosplash -nodesktop < script.m >/dev/null 2> OUTPUT.txt
댓글 수: 3
Jan
2013년 2월 17일
You can use evalc to get the output of the complete program to a single string. Then you need one adjusted fprintf() only.
In addition you can shadow fprintf by a user-defined function, which forwards the output as wanted.
I'm using a dedicated function for the output to the command window. On demand the output is copied to one or more files, while the output to the command window can be suppressed. But still warning and error messages must be caught carefully.
추가 답변 (1개)
Image Analyst
2013년 2월 17일
Just use fprintf() inside your script to write exactly what you want, and don't use the command line option to echo output to a file. That way you'll have 100% absolute complete control.
댓글 수: 2
Image Analyst
2013년 2월 17일
I didn't say that. What we're saying is to not print anything that you need captured to the console, print it to the file directly. So don't use disp(), echo, or fprintf() with no file handle, or put the variable on its own line (which will print it out to the console) - use fprintf(fid, ....) for everything.
참고 항목
카테고리
Help Center 및 File Exchange에서 Entering Commands에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!