exporting figures
Rich recently discussed creating and exporting figures from MATLAB (associated files). He started by listing the things he wants: to produce a figure for a paper (e.g., for LaTeX); for fonts, line widths, etc. within the figure to be a specified size (e.g., 12 pts); for fonts and symbols in the figure to be identical in the text; to automate the process as much as possible since regenerating the figure is common {collect additional data; tweak simulations; reuse the figure in a new publication, talk or grant; make minor cosmetic alterations}; and to reuse the visualization code.
His thoughts on how to make these happen include: not to export figures using the ‘export’ menu function; not to modify figure properties using the mouse; to avoid using third party graphics manipulation programs where possible; to use functions and scripts to generate plots (allows for reusability); to specify fonts, line styles, axis positions and figure sizes as variables (allows for modifiability); and to export using the print command (allows for controllability).
He also listed the differences between vector graphics and raster graphics. In vector graphics or line art, the format is the object properties, whereas in raster graphics or bitmap it is the pixel values. The file size for line art in vector graphics is small, whereas it’s large for raster graphics. Conversely, the file size for images is massive in vector graphics, whereas it’s merely large for raster graphics. Tools for editing vector graphics include Adobe Illustrator, Inkscape and Intaglio. Those for raster graphics include Adobe Photoshop and GIMP. In most cases, you should export your MATLAB figures as EPS or PDF (vector graphics).
Here is some other useful stuff that Rich mentioned:
- patch.m - 2D polygon plotting, useful for plotting error bars
- plotyy.m - different y axes on each side of the plot
- annotation.m - add arrows, textboxes etc. to your figure
- legend.m, nudgeLegend.m - add legend to a plot, and tweak its appearance (my code)
- set(gca,’box’,’off’) - turns the figure bounding box off
- set(gca,’layer’,’top’) - bring the axis to the top to stop stuff being plotted over the black edges and tick marks
- set(gca,’TickDir’,’out’) - set the direction of the tick marks to point outwards
- pu = get(gcf,’PaperUnits’); pp = get(gcf,’PaperPosition’); set(gcf,’Units’,pu,’Position’,pp) To set the screen size to be the same as the papersize - more WYSIWYG
- for mixtures of vector and bitmaped graphics - export each part separately and overlay e.g. within latex itself
- transparent background in eps file - comment out lines in the eps file that read: “X X X PR” or “X X X X MP”, where X is some number.
- LaTeX trick: use the layout package to find out how wide the page is for setting figure dimensions.
To get a better sense for how to actually do this stuff, here is some snippet’s of his demoFigs.m as well as the associated images from demoFig.pdf. The idea of demoFigs is to progressively improve a figure. I’m including only the good parts. I suggest you mess around with the script yourself.
Version 1—nothing special, just exported with MATLAB’s Figure menu.

Version 2—automated export, using print.
filename = ‘demo2.eps’;
print(gcf,’-depsc’,filename);
Note his use of the bang command (‘!’), which steps out of MATLAB and calls some OS commands.
! epstopdf demo2.eps
! acroread demo2.pdf&

Version 3—set size of EPS/PDF, by specifying the paper size.
PS = [14,10]; % paper size (in centimeters)
PP = [0,0,PS]; % paper position on the printed page (in centimeters)
set(gcf,’paperpositionmode’,’manual’,’paperposition’, …
PP,’papersize’,PS, ‘paperunits’,’centimeters’);
filename = ‘demo3.eps’;
print(gcf,’-depsc’,filename,’-painters’,’-loose’); % loose option stops matlab cropping white space automatically

Version 4—make better axes with less white space, by setting their position and using axes to create them.
left = 0.12; % space on LHS of figure, normalised units
right = 0.02; % space on RHS of figure
top = 0.05; % space above figure
bottom = 0.1; % space below figure
hspace = 0.07;% horizontal space between axes
height = (1-top-bottom); % height of axis
width = (1-left-right-hspace)/2; % width of axis
across = [hspace+width,0,0,0];
pos1 = [left,1-top-height,width,height]; % position of axis
pos2 = pos1+across;
figure
ax1 = axes(‘position’,pos1); % produce axis replaces subplot(1,2,1)
plot(t,s1,’-‘)
ylabel(‘y’)
xlabel(‘\omega = 2 \pi c/\lambda’)
ax2 = axes(‘position’,pos2); % produce second axis replace subplot(1,2,2)
plot(t,s2,’-‘)
xlabel(‘\omega = 2 \pi c/\lambda’)

Version 5—make better font sizes and linewidths, by setting them.
% Fonts
FontName = ‘Times’;
FSsm = 7; % small font size
FSmed = 10; % medium font size
% Line widths
LWthin = 1; % thin lines
% Colors
col1 = [0,0,1];
——>
ax1 = axes(‘position’,pos1); % produce axis replaces subplot(1,2,1)
plot(t,s1,’-‘,’linewidth’,LWthin)
ylabel(‘y’,’fontname’,FontName,’FontSize’,FSmed)
xlabel(‘\omega = 2 \pi c/\lambda’,’fontname’,FontName,’FontSize’,FSmed)
set(ax1,’FontName’,FontName,’FontSize’,FSsm)
ax2 = axes(‘position’,pos2); % produce second axis replace subplot(1,2,2)
plot(t,s2,’-‘,’linewidth’,LWthin)
xlabel(‘\omega = 2 \pi c/\lambda’,’fontname’,FontName,’FontSize’,FSmed)
set(ax2,’FontName’,FontName,’FontSize’,FSsm)

Version 6—make better xlabel on second plot using PSfrag.
Nothing really to say about the MATLAB code here, since it’s done in LaTeX. Only thing would be to keep the text simple so that you can locate it in the LaTeX.

Version 7—make everything with a ‘shell’ function.
filename = ‘demo7’;
Plot1By2Demo(t,s1,s2,filename)
