How do I get MATLAB running in batch mode to *only* output the results of disp, fprintf, etc. and not the headers and >> prompts?

35 views (last 30 days)
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.

Accepted Answer

Walter Roberson
Walter Roberson on 17 Feb 2013
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 Comments
Jan
Jan on 17 Feb 2013
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.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 17 Feb 2013
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 Comments
Ricardo
Ricardo on 17 Feb 2013
Except I need to echo output to a file, so just displaying text inside the MATLAB console, even if it's perfectly customized to what I want, isn't what I need.
Image Analyst
Image Analyst on 17 Feb 2013
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.

Sign in to comment.

Categories

Find more on Entering Commands in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!