You can use the Unix command "tail + n" to delete the first n lines of output. This header looks like 10 lines, so it will separate it.
$ matlab -nosplash -nodesktop -nodisplay -r test | tail +10
This is a bit fragile because warnings (for example, "without a window system") will be split and the size of the header will depend on what warnings occur (and these warnings are useful diagnostics). Also, this warning might be on STDERR instead of STDOUT, so "tail +9" might be what you need.
A more robust approach would be to modify the Matlab script to write to a separate file using fopen / fprintf / fclose. Thus, the title, warnings, errors, etc. From Matlab will be separated from the formatted output you want. To get the output of "disp", to go to that particular file descriptor, you can write it with evalc. The source file can be specified using the test () argument in the -r message, and the $$ env (process PID) variable is included in the file name to prevent collisions in a multiprocessor environment.
function test(ppid) outfile = sprintf('outfile-%d.tmp', ppid); fh = fopen(outfile, 'w'); myvar = rand(3,4); str = evalc('disp(myvar)'); fprintf(fh, '%s', str); fclose(fh);
To call it from bash, use this call form. (Perhaps these are minor syntax issues; I don't have a Unix window for testing right now.)
% matlab -nosplash -nodisplay -r "test($$)" -logfile matlab-log-$$.tmp
Let's say your bash PID is 1234. Now you have the output in outfile-1234.tmp and in the Matlab log in matlab-log-1234.tmp. Paste them in / tmp if you do not want them to depend on pwd. You can extend this to create multiple output files from a single matlab call, while saving on startup costs if you need to compute several things.
Andrew Janke Oct 05 '09 at 14:10 2009-10-05 14:10
source share