Godzy
Godzy

Reputation: 157

Matlab saves figure to a very large eps file for seemingly no reason

I use LaTeX for most of my writings and always uses eps files -- that Matlab can create. There wasn't any problem but, for a new case I started studying Matlab generated a 95MB file (and eps2pdf fails to produce a pdf with that)

This question would likely be difficult to answer. It seems like an unexpected Matlab behavior but I cannot share the original code unfortunately (plus the code is very long and depends of so many variables and functions...). I have tried to build a minimum working example using built-in functions but of course it doesn't reproduce the problem. Still, I can give the structure of the code and type of objects created (see end of post): curves, surfaces, transparency, no crazy things.

This code has been running without any problem for many different situations, and gives me files of a few MB. The number of points present in one of the surfac, the approximate shape, the number of points in the curves etc. nothing changes or the changes are minute. I cannot explain the file size difference.

I would welcome either an explanation of the behavior or an alternative for getting this picture to eps. Or both of course. For now I output it to png, but I'd like vector files very much. Has anyone encountered the issue of very large eps before?

colordef white;
figure('Color','White','Name','STUFF')
hold all

% Show a curve
lpot = plot3(//STUFF\\, '-',...
    'Color', [0 0 1], 'LineWidth', 2);

% Show a curve
ph = plot3(//STUFF\\,'-',...
        'Color', [1 0 0], 'LineWidth', 2);

% Show a curve
plot3(//STUFF\\, '-',...
        'Color', [0 1 0], 'LineWidth', 2);

% Show a point
plot3(//STUFF\\, ...
    'k', 'MarkerSize', 10, 'Marker', '^', ...
    'MarkerFaceColor', [1 1 0]);

% Show a surface
surf(//STUFF\\);
colormap(jet); 
shading interp;

% Show a surface in transparency
surf_1 = trisurf(//STUFF\\);
set(surf_1,'FaceColor', [1 1 1], 'EdgeAlpha', 0, 'FaceAlpha', 0.5);

% Axis adjustements
axis equal tight
xlabel('stuff');
ylabel('stuff');
zlabel('stuff');
view(2);
grid on;
h_cbar = colorbar;
ylabel(h_cbar, //STUFF\\);
legend(//STUFF\\,...
    {//STUFF\\});
set(gcf,'units','normalized','outerposition',[0.10 0.10 0.80 0.80])

hold off

hgexport(gcf, ['mytoobigfigure.eps'],...
    hgexport('factorystyle'), 'Format', 'eps');

The code runs on matlab r2014b, windows 7.

Thanks everyone!

Upvotes: 3

Views: 3746

Answers (2)

Go to File -> Export setup -> Rendering -> Custom rendering and change the painters (vector format) to OpenGL (bitmap format) and click on "Apply to figure". Now save the figure in whatever format. Even an eps file is not bigger than a few MB. This works for me!

Upvotes: 0

Godzy
Godzy

Reputation: 157

Yay, thanks to Peter comment, I solved the problem! The fix:

myfig = gcf;                   % or define myfig when the figure is created
myfig.RendererMode = 'manual'  % use a set with older versions of Matlab

Which does not mean I understand why this happens and this is most likely not a desired behavior of Matlab. I think that for some reason, the renderer would switch from OpenGL to painters while saving the figure. Examining the eps, it was clear the transparency was very wrong (off in most places) and the polygon offset (or the property that allows to draw line in front of their patches) was also very poor. Whether or not a bitmap was involved, it is beyond my skills to retrieve such an information. But clearly the saved figure was not the same as the displayed figure. It should also be noted that switching the renderer to manual does not affect the picture in any other way on the screen. I wonder if setting the renderer to manual by default would not be a good idea... would there be any drawbacks to forcing OpenGl all the time?

If somebody has a more detailed answer on the topic, I'd be happy to read it.

Upvotes: 2

Related Questions