This is a documented limitation of printing in headless mode:
Printing and Exporting without a Display
On a UNIX platform (including Macintosh), where you can start in
MATLAB nodisplay
mode (matlab?-nodisplay
), you can print using
most of the drivers you can use with a display and export to most of
the same file formats. The PostScript and Ghostscript devices all
function in nodisplay
mode on UNIX platforms. The graphic devices
-djpeg
, -dpng
, -dtiff
(compressed TIFF bitmaps), and -tiff
(EPS with TIFF preview) work as well, but under nodisplay
they use
Ghostscript to generate output instead of using the drivers built into
MATLAB. However, Ghostscript ignores the -r
option when generating
-djpeg
, -dpng
, -dtiff
, and -tiff
image files. This means that
you cannot vary the resolution of image files when running in
nodisplay
mode.
The same is true for the -noFigureWindows
startup option which
suppresses figures on all platforms. On Windows platforms the -dwin
,
-dwinc
, and -dsetup
options operate as usual under
-noFigureWindows
. However, the printpreview
GUI does not function
in this mode. Naturally, the Windows only -dwin
and -dwinc
output
formats cannot be used on UNIX or Mac platforms with or without a
display.
Resolution Considerations
Use -rnumber
to specify the resolution of the generated output. In
general, using a higher value will yield higher quality output but at
the cost of larger output files. It affects the resolution and output
size of all MATLAB built-in raster formats (which are identified in
column four of the table in Graphics Format Files).
Note: Built-in graphics formats are generated directly from MATLAB without conversion through the Ghostscript library. Also, in headless
(nodisplay
) mode, writing to certain image formats is not done by
built-in drivers, as it is when a display is being used. These formats
are -djpeg
, -dtiff
, and -dpng
. Furthermore, the -dhdf
and
-dbmp
formats cannot be generated in headless mode (but you can
substitute -dbmp16m
for -dbmp
). See "Printing and Exporting
without a Display" for details on printing when not using a display.
Unlike the built-in MATLAB formats, graphic output generated via
Ghostscript does not directly obey -r
option settings. However, the
intermediate PostScript file generated by MATLAB as input for the
Ghostscript processor is affected by the -r
setting and thus can
indirectly influence the quality of the final Ghostscript generated
output.
The effect of the -r
option on output quality can be subtle at
ordinary magnification when using the OpenGL or ZBuffer renderers and
writing to one of the MATLAB built-in raster formats, or when
generating vector output that contains an embedded raster image (for
example, PostScript or PDF). The effect of specifying higher
resolution is more apparent when viewing the output at higher
magnification or when printed, since a larger -r
setting provides
more data to use when scaling the image.
When generating fully vectorized output (as when using the Painters
renderer to output a vector format such as PostScript or PDF), the
resolution setting affects the degree of detail of the output; setting
resolution higher generates crisper output (but small changes in the
resolution may have no observable effect). For example, the gap widths
of lines that do not use a solid ('-'
) linestyle can be affected.
parfor
spawns headless MATLAB instances (both Windows and Unix), so according to the above, the worker processes will fallback to Ghostscript printing driver which ignores the -r
option.
When you export figures to raster graphics format (PNG, JPEG, TIFF, etc..) there are two cases:
if you printing in a normal session, MATLAB will use its built-in drivers to generate the graphics files directly, and should obey the resolution you specify
on the other hand, if you printing in headless mode, MATLAB will internally export the figure in Postscript vector format, and then use Ghostscript to convert it to the requested raster format using the following Ghostscript options:
-dNOPAUSE -q
-I"C:Program FilesMATLABR2014asysexternwin64ghostscriptps_files"
-I"C:Program FilesMATLABR2014asysexternwin64ghostscriptfonts"
-sDEVICE=jpeg
-g576x432
-sOutputFile="file.jpeg"
as you can see, for some reason MATLAB uses a fixed target size 576x432 in headless mode when converting the PS file to other formats.
Here is some code for quick experimentation. I've tested it on a local parallel pool; All of the raster formats (PNG, JPEG, TIFF, PPM) had a fixed size of 576x432 (-r
option ignored as previously explained). The PDF was also generated by converting the PS file to PDF (using -sDEVICE=pdfwrite
Ghostscript output device).
fmt = {'ppm', 'tiff', 'png', 'jpeg', 'epsc2', 'pdf'};
outfolder = 'C:UsersAmroDesktopprint_test';
parpool(4)
parfor i=1:4
fig = figure(i);
% a random plot
ax = axes('Parent',fig);
plot(ax, cumsum(rand(1000,1)-0.5))
% save in each specified format (-r option is mostly ignored)
for f=1:numel(fmt)
print(fig, ['-d' fmt{f}], '-r920', ...
fullfile(outfolder,sprintf('plot%d.%s',i,fmt{f})));
drawnow
end
% also save FIG-file
hgsave(fig, sprintf('plot%d.fig',i))
close(fig);
end
delete(gcp)
The way I see it, you ought to export as an EPS file, and manually convert it to whatever format you need. That way you get to specify the target image size in the Ghostscript command invoked (I wouldn't bother with the print -r
resolution option, because it has little effect on vector formats)
The alternative would be to export FIG-files inside parfor
. You would then load them in a normal MATLAB session with a display, and serially print with the desired resolution and format:
for i=1:4
fig = hgload('plotXX.fig');
movegui(fig, 'center')
print(fig, '-djpeg', '-r920', 'outXX.jpeg')
close(fig)
end