nav-left cat-right
Matthias Pospiech > Programming > matlab > Matlab figures – solving export problems

Matlab figures – solving export problems

How to make a really beautiful and publishable graph with matlab is not easy but possible. It is very good descriped in this article making pretty graphs.

However once you want to export your file you run into many problems, which are build in bugs of matlab, and have not been fixed since years. They are however well documented by matlab…
The two most important bugs are the missing font support and wrong linestyles. If you use any not standard postscript font such as „Times, Helvetica, Courier, Bookman“ then the font gets replaced by Courier and your graph looks really ugly. The linestyles can only be fixed in the eps file directly, not in any other output format. Additionaly matlab suffers from the legend box size bug, which most often is too small in width.

Matlab offers three renders. Most important are ‚painters‘ which is the only vector renderer. It should be used for vector suitable data such as lines. The other is ‚opengl‘ which is used by default for 3D data such as ’surf‘ plots.

In the following I demonstrate these problems and also show the results of macros providing better results. These are export_fig, savefig, plot2svg and matlabfrag. In the end I show the results of my own approach currently named ‚latexfigure‘. It reuses many code and ideas of the other matlab projects.

  1. figure code

figure code

clf; clc; clear all;
figure_width  = 8*2;
figure_height = 6*2;
FontSize = 11*1.5;
FontName = 'MyriadPro-Regular';
xaxis = linspace(0,10,50);
data1 =sin(xaxis);
data2 =sin(xaxis + 0.6);
data3 =sin(xaxis + 1.2);
hfig  = figure(1); clf;
    set(gcf, 'units', 'centimeters', 'pos', [0 0 figure_width figure_height])
    % set(gcf, 'Units', 'pixels', 'Position', [100 100 500 375]);
    set(gcf, 'PaperPositionMode', 'auto');
    set(gcf, 'Color', [1 1 1]); % Sets figure background
    set(gca, 'Color', [1 1 1]); % Sets axes background
    set(gcf, 'Renderer', 'painters'); 
hLine1 = line(xaxis, data1);
hLine2 = line(xaxis, data2);
hLine3 = line(xaxis, data3);
hdots = line(xaxis, data3);
set(hLine1                        , ...
  'LineStyle'       , '--'        , ...
  'LineWidth'       , 2           , ... 
  'Color'           , [0.75 0 0]  );
set(hLine2                        , ...
  'LineStyle'       , ':'         , ...
  'LineWidth'       , 2           , ...   
  'Color'           , [0 0 0.75]  );
set(hLine3                        , ...
  'LineStyle'       , '-'         , ...
  'LineWidth'       , 2           , ...   
  'Color'           , [0.5 0 0.5]  );
set(hdots                         , ...
  'LineStyle'       , 'none'      , ...
  'Marker'          , 'o'         , ...
  'MarkerSize'      , 6           , ...
  'MarkerEdgeColor' , [.5 .0 .5]  , ...
  'MarkerFaceColor' , [.7 .5 .7]  );
hTitle  = title ('test graphics');
hXLabel = xlabel('x-axis');
hYLabel = ylabel('y-axis');
hLegend = legend( ...
  [hdots, hLine1, hLine2, hLine3], ...
  'Data' , ...
  'Model'    , ...  
  'Fit'      , ...
  'Validation Data'       , ...  
  'location', 'Best' );
  %'Data (\mu \pm \sigma)' , ...
  %'Model (\it{C x^3})'    , ...  
  %'Fit (\it{C x^3})'      , ...
set( gca                       , ...
    'FontName'   , FontName );
set([hTitle, hXLabel, hYLabel], ...
    'FontName'   , FontName);
set([hLegend, gca]             , ...
    'FontSize'   , FontSize - 2);
set([hXLabel, hYLabel]  , ...
    'FontSize'   , FontSize    );
set( hTitle                    , ...
    'FontSize'   , FontSize    , ...
    'FontWeight' , 'bold'      );
set(gca, ...
  'Box'         , 'off'     , ...
  'TickDir'     , 'out'     , ...
  'TickLength'  , [.02 .02] , ...
  'XMinorTick'  , 'on'      , ...
  'YMinorTick'  , 'on'      , ...
  'YGrid'       , 'on'      , ...
  'XColor'      , [.0 .0 .0], ...
  'YColor'      , [.0 .0 .0], ...
  'YTick'       , -1:0.5:1, ...
  'LineWidth'   , 1.0         );
% print(gcf, '-r300', 'matlab_opengl.png', '-dpng');
% print(gcf, '-r300', 'matlab_opengl.pdf', '-dpdf');
% print(gcf, '-r300', 'matlab_opengl.eps', '-depsc');
% ghostscriptConvertTo('png', 'matlab_opengl.eps', 'matlab_opengl.eps.png')
% print(gcf, '-r300', 'matlab_painters.png', '-dpng');
% print(gcf, '-r300', 'matlab_painters.pdf', '-dpdf');
% print(gcf, '-r300', 'matlab_painters.eps', '-depsc');
% ghostscriptConvertTo('png', 'matlab_painters.eps', 'matlab_painters.eps.png')
% savefig('savefig', 'pdf', 'png', 'eps', '-fonts'); 
% export_fig('exportfig_painters.pdf', '-pdf', '-painters');
% export_fig('exportfig_painters.eps', '-eps', '-painters');
% export_fig('exportfig_painters.png', '-png', '-painters', '-r300');
% export_fig('exportfig_opengl.png', '-png', '-opengl', '-r300');
% plot2svg('plot2svg.svg', hfig);
% matlabfrag('matlabfrag.eps','handle',hfig) ;
% mlf2pdf(gcf,'matlabfrag');
% latexPackages = ['\renewcommand{\sfdefault}{pmy}\n \renewcommand{\rmdefault}{pmy}'];
% latexfigure(hfig,'latexfigure','eps',latexPackages);
% latexfigure(hfig,'latexfigure','png',latexPackages);
% latexfigure(hfig,'latexfigure','pdf',latexPackages);

Seiten: 1 2 3 4

18 Antworten : “Matlab figures – solving export problems”

  1. ~Ilja sagt:

    I wonder if any of those figure exporting methods help to unify the eps-files again?
    Please could you try with following test data:
    caxis([-1 1]); colormap(jet)
    and see the results whether the vector images look fragmented.

  2. pospiech sagt:

    The tool gswin32c.exe is either not installed or not within the windows path.
    Open a cmd window and try to execute the programm. You should see something like this

    C:\Users\Matthias Pospiech>gswin32c.exe
    GPL Ghostscript 9.07 (2013-02-14)
    Copyright (C) 2012 Artifex Software, Inc. All rights reserved.
    This software comes with NO WARRANTY: see the file PUBLIC for details.

  3. Johl sagt:

    I’d really like to use your promising tool.
    But somehow I always get the following error:

    Der Befehl „gswin32c.exe“ ist entweder falsch geschrieben oder
    konnte nicht gefunden werden.

    cmd =

    gswin32c.exe -q -dSAFER -dNOPAUSE -dBATCH -dEPSCrop -dUseFlateCompression=true -dAutoRotatePages=/None -dHaveTrueTypes -r600 -dGraphicsAlphaBits#4 -dTextAlphaBits#4 -sDEVICE=png16m -sOutputFile=“C:\Users\Stefan Flau\Desktop\Promotion\Matlab-Tools\LaTeX_Figure\example\TEMP1576.png“ -f „C:\Users\Stefan Flau\Desktop\Promotion\Matlab-Tools\LaTeX_Figure\example\TEMP1576.pdf“

    Das System kann die angegebene Datei nicht finden.

    What’s wrong here?

  4. pospiech sagt:

    I do not have a solution for you, sorry. Note that if you want to have a wider audience you should ask matlab related questions on The link to my plugin on matlabcentral is

  5. Jan sagt:

    Hi there. I have used the script excessively and it has produced a lot of nice looking figures. But is there a way to narrow down the legend?

    I define a size for the legend using a four element vector for the setting of the ‚location‘ but MATLAB is widening the legend so that the entry for the legend fits. When I use for the example of a revolution speed
    $n = 500/\textrm{min}$
    I get a very wide legend with a big white edge after latexfigure converts the term to n = 500/min.

  6. Jan sagt:

    Thanks, I added my issue to the existing issue to the page. I am running into the problem using your latexfigure.m function. It’s not a problem using Palatino. It’s a general problem, that I only get the Charter font no matter which FontName I define.

  7. pospiech sagt:

    You are not making it really clear what you are doing. If you are using the print command of matlab you should ask your question on If you are using my latexfigure script then I do not understand where the problem is. Note also that there is a special page for reporting issues (not questions):

  8. Jan sagt:

    Hi there, Jan again (from the third to last post). I ran into a little problem using Palatino for my figures. I tried setting the font name to ‚Palatino‘ and ‚Palatino Linotype‘ and MATLAB is setting the correct font in the figure but when I convert to eps or pdf the look is similar but not the same. This is very obvious when using the letter R, which is getting a little swing in the bottom right corner. I also checked the mathpazo font of LaTeX but that one has the same look as the Palatino Linotype of Windows although it is a slightly different clone of the original.

    Is there a solution to this problem or a hint where to look at?

  9. Jan sagt:

    Hey! Nice Tool so far, I was looking for something like that since „normal“ export via export_fig or print does mess up my unicode characters (I am working on Ubuntu Linux @ UTF-8).
    For Latex matlabfrag is the best choice I think, but i need a vectorized graphic with the true annotations, not just labels.

    But I realized the created fonts are no text anymore, is that correct? Is there no way back from the *.tex and the *.eps file with labels to the initial eps file (with the „real“ text)?

    Btw: For UTF-8 System you have to change line 165 to fprintf(fid,’\\usepackage[utf8]{inputenc}\n‘);

    Thank you!

    Best regards,

  10. pospiech sagt:

    You should try to run ‚latex‘ from a console. If that fails you should add it to your path. If you do not know how to do that ask it somewhere else.
    If it fails even with a set up path you can specify a path with the option ‚distpath‘.

  11. Jan sagt:

    thanks for the tool. I’m trying to get around my font-problem using „Palatino Linotype“ for my graphics. But I’m already running into problems using the example in „plotTest.m“

    Error using latexfigure/runLaTeX (line 204)
    Error 1 — LATEX:
    Das System kann den angegebenen Pfad nicht finden.

    Error in latexfigure (line 101)
    success = runLaTeX();

    Error in plotTest (line 91)
    latexfigure(hfig,’latexfigure‘,’eps‘,’packages‘, latexPackages);

    It seem’s that I need to define the path of my LaTeX-Distribution first. How do i do that?

  12. Andreas Hielscher sagt:

    Indeed, the gs converter has to be blamed – exporting the pdf via Adobe Acrobat Professional works perfectly. Thanks for your help!

  13. pospiech sagt:

    The resulting pdf is generated by LaTeX. Therefore you should find out if you really load a vector (type 1) font. The eps is generated from the pdf file using ghostscript. Please check if you also experience this problem with pdf output. If not I would blame the gs converter.

  14. Andreas Hielscher sagt:

    That is a really nice tool and it helped me to get around the „FontName“-Bug in Matlab. Thanks a lot!

    I do have one problem: After using latexfigure, my eps-file looks fine, but when I zoom in, the font is looking like a bitmap and not smooth like a vector. I tried several fonts, eg Cambria (my favourite one), a latex-font within the package kpfonts and a matlab-font Palatino. All fonts look like a bitmap.
    What should I change?

    Thank you in advance and best regards,
    Andreas Hielscher

  15. Jannes sagt:

    Thanks for the great code!

    I do have a problem when trying to plot over very small or large ranges (i.e. 1E-07, 1E07..). It seems that Ghostscript can’t handle that form of notation? I’m running Matlab on OSX (Snow Leopard).

    Thanks a lot

  16. pospiech sagt:

    Currently I have not the time for this code (I am partly still working on it). But I promise to publish it within the next month…

    In the meantime you can still use the matlabfrag package which is what I use internally.

  17. Oliver Woodford sagt:

    The output of latexfigure looks great! I’ve wanted to find a good solution to separating text and graphics for some time, and this looks like it. I would love to see it on the Matlab Central File Exchange, or else be able to download the latest version from somewhere.

Einen Kommentar schreiben

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.


This blog is kept spam free by WP-SpamFree.