Suppress Matlab Start Message

I want to call matlab in bash non-interactively and use its result outside Matlab.

For example, I have a script test.m

rand(3,4) quit 

When I execute in bash

 $ matlab -nosplash -nodesktop -nodisplay -r test Warning: No window system found. Java option 'MWT' ignored < MATLAB (R) > Copyright 1984-2008 The MathWorks, Inc. Version 7.7.0.471 (R2008b) September 17, 2008 To get started, type one of these: helpwin, helpdesk, or demo. For product information, visit www.mathworks.com. ans = 0.8147 0.9134 0.2785 0.9649 0.9058 0.6324 0.5469 0.1576 0.1270 0.0975 0.9575 0.9706 

Is it possible to suppress the Matlab start message and show the results without "ans =".

Note. I am asking a general question not only for this example.

Thank you and welcome!

+14
command-line matlab
05 Oct '09 at 2:48
source share
5 answers

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.

+9
Oct 05 '09 at 14:10
source share
— -

Try using the -logfile command-line option :

 -logfile log - Make a copy of any output to the command window in file log. This includes all crash reports. 

Then you can easily delete the first few lines using any method (e.g. sed). Example:

 matlab.exe -nosplash -nodesktop -nojvm -logfile out.log -r 'rand(3,3), exit' sed '1,5d' out.log 

Also, if you are working with a script where you need to exit before continuing, use the -wait option :

 -wait - MATLAB is started by a separate starter program which normally launches MATLAB and then immediately quits. Using the -wait option tells the starter program not to quit until MATLAB has terminated. This option is useful when you need to process the the results from MATLAB in a script. The call to MATLAB with this option will block the script from continuing until the results are generated. 



More information on MATLAB startup options can be found here or on the matlab help pages: Windows / Unix

+11
Oct 05 '09 at 3:35
source share

I would recommend saving the output to a file and then reading in that file. This approach is somewhat more complex, but less fragile as formats change, etc. This gives you much more control. You will find many scripts on the Internet for converting Matlab files to another host language.

Example:

 A = randn(3, 2); save temp_output.mat A # Later, read temp_output.mat in whichever language you desire. 
+2
05 Oct '09 at 3:01
source share

To suppress the mapping ans = , you can use the DISP function:

 disp(rand(3,4)); 

To suppress this first warning message, you can try adding the -nojvm option to see if this helps.

To suppress everything else, you can try this solution from the MathWorks news feed, which solves the same problem.

+2
Oct 05 '09 at 3:12
source share

Call matlab like this

 matlab -nodisplay <test.m &>matlab.output 

dumps all startup messages and other displayed output to the matlab.output file (which can be called anything). If you then (following Peter's suggestion) have test.m, save the result you need for the file using

 csvwrite('temp_output.txt',A) 

or other appropriate output function, which you can then read in this file and continue.

+1
Oct 05 '09 at 3:26
source share



All Articles