MATLAB: Is running a script from the Mac Terminal much slower than in the MATLAB Command Window

commandlineMATLABnodesktopnodisplayperformanceprintterminal

I have some code where I load in some data, build a waterfall plot, and then print that plot using the "print" command (with the `-dpng` and `-r300` arguments set).
When I run the script from the Command Window, it takes 60 seconds to run. When I run the same script from the command line (Terminal), it takes ~760 seconds to run.
$ /Applications/MATLAB_R2016a.app/bin/matlab -nodisplay -nosplash < test_waterfall.m
Why is running the script from the Terminal so much slower?

Best Answer

When MATLAB scripts are executed from the Mac Terminal with the "-nodisplay" flag, OpenGL is not available.  This means the "print" command will use painters to create the image. 
Rather than use "-nodisplay", try using "-nodesktop".  Then, when you create figures, try making them invisible using the following:
>> figure('Visible','off');
Also, using the shell to pipe commands into the MATLAB session operates on a line-by-line basis.  Using the "-r" flag instead to run code requires that your "test_waterfall.m" be on the MATLAB Path, but it will allow the compiler to do script-wide optimizations because it runs the script as a single command. This flag is used as follows:
$ /Applications/MATLAB_R2016a.app/bin/matlab -nodesktop -nosplash -r test_waterfall