Exporting Figures from MATLAB

I just discovered the WordPress.com MATLAB feed today. Frinkytown’s complaint about copying and pasting figure reminded me of things I had to do to write my M.S. thesis, and other things I discovered afterwards:

  1. MS Word is the devil, and Equation Editor is its evil spawn. When I started writing my thesis, I had been using WordPerfect for 9 years. I cannot imagine writing a technical document without Reveal Codes or a close equivalent. As you might also have guessed, I greatly preferred the old WP method for entering equations. That is, typing out a math code for an equation rather than pointing and clicking through palettes of symbols, clicking placeholders for subscripts, superscripts, and other elements, etc. I’m a [latex]\LaTeX[/latex] geek now.
  2. Windows Metafiles What Word does to Windows Metafiles is a close third on deviltry.

To the specific question of getting decent graphs from MATLAB to Word, I’d check the following: in a figure window, select Edit / Copy Options. Make sure Clipboard format is set to Metafile (may lose information) or Preserve information (metafile if possible) if you’re doing line graphs. Bitmaps will be ugly. Metafiles will look much better, since they’re normally a vector file format, and should be resolution-independent.

However, metafiles and Word are a match made somewhere with a statistically significant difference from heaven. Never, ever edit a MATLAB-generated metafile in Word. Word will screw up your lovely vectorized metafiles even if you don’t actually make any edits. You have been warned. Example:

matlab-sine.png
Original Matlab Figure

 

word-sine.png
Matlab Figure Pasted Into Word

 

word-sine-after.png
Matlab Figure After Edit Picture Menu

I hate Word. “Sure, go ahead: change my font weight. Misjustify my y-axis numbers. Change my y-axis label orientation by 90 degrees. And could you also make sure I can never, ever put it back the way it was? Thanks!”

The only way to avoid this problem (aside from never clicking Edit Picture) is to insert figures in a format that Word won’t try to edit. BMP files could be ok, but are bulky and have the low-resolution problem mentioned in the original post. PNG files are at least much smaller, but aren’t any higher quality. For me, that leaves Encapsulated PostScript (EPS).

EPS advantages:

  1. Natively exported from MATLAB
  2. Standard for “journal-quality” graphs.
  3. Can be converted to PDF relatively easily if you have [tex]\LaTeX[/tex] around somewhere, even if it’s on a remote Unix system.

EPS disadvantages:

  1. Requires a PostScript-compatible printer. No printing an final copy on my dad’s Dell all-in-one inkjet.
  2. Takes some extra effort to get a preview in Word.

I did all my M.S. figures in EPS; WordPerfect handled them fine, and we had plenty of PostScript printers at the university. I also wrote a quick-and-dirty .m file to print every open figure window consistently:

function out=printall(printcmd)
% printall - Print all currently open figures
%
% With no arguments, this prints all figures with a
% regular print command.
%
% printall('printcmd') prints all figures with the
% print command 'printcmd'
%
% Examples:
%
% printall('print -dmeta fig%d.wmf') prints all figures
%                                    to Windows metafiles
%                                    named fig1.wmf, fig2.wmf,
%                                    etc.
if nargin==0
printcmd='print';
end
figs=sort(get(0,'Children'));
for count=1:length(figs)
feval('figure',figs(count));
eval(sprintf(printcmd,figs(count)));
end

(Apologies for the formatting; I don’t yet have a decent syntax highlighting or code-including plugin.) This way, if I had a slew of figures that all needed the same axes limits, I could run printall('axis([0 10 -100 100])') at a MATLAB prompt and get all their axes sized consistently. A following printall('print -depsc2 fig%d.eps') would give me a fig1.eps, fig2.eps, … etc. from the current figure windows.

Now that I’m a [tex]\LaTeX[/tex] geek, my current favorite for getting figures out of MATLAB is printall('pdfprint') — with this combination of MATLAB and a [tex]\LaTeX[/tex] distribution, I can generate cropped PDF files ready to reference from a .tex file.

function result=pdfprint(pdfName)
% PDFPRINT Print a figure window to an Adobe PDF file.
% SYNTAX:
% pdfprint
% PDFPRINT alone sends the current figure to a PDF file named
% FigureN.pdf, where N is the current figure number.
%
% pdfprint filename.pdf
% Same as above but sends the output to a file named filename.pdf
if nargin<1
figureNumber=gcf;
pdfName=sprintf('Figure%d.pdf',figureNumber);
end
tempBasename=tempname;
psTempFile=sprintf('%s.ps',tempBasename);
pdfTempFile=sprintf('%s.pdf',tempBasename);
print('-depsc2',psTempFile);
epstopdfCommand=sprintf('epstopdf %s',psTempFile);
[status,output]=system(epstopdfCommand);
if status~=0
warning('epstopdf command had non-zero return value');
warning(output);
result=1;
else
if ispc
delCommand=sprintf('cmd /c del %s',psTempFile);
renameCommand=sprintf('cmd /c move %s %s',pdfTempFile,pdfName);
else
delCommand=sprintf('rm %s',psTempFile);
renameCommand=sprintf('mv %s %s',pdfTempFile,pdfName);
end
[status,output]=system(renameCommand);
if status~=0
warning('rename command had non-zero return value');
warning(output);
result=2;
end
[status,output]=system(delCommand);
if status~=0
warning('delete command had non-zero return value');
warning(output);
result=3;
end
end

Ben Hinkle at Mathworks posted some helpful figure export scripts in the File Exchange in 2001, and wrote an article about exporting figures in late 2000.

Leave a comment

Your email address will not be published. Required fields are marked *