<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Mike Renfro's Blog &#187; LaTeX</title>
	<atom:link href="http://blogs.cae.tntech.edu/mwr/category/latex/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.cae.tntech.edu/mwr</link>
	<description>A partial repository of whatever comes to mind</description>
	<lastBuildDate>Sat, 31 Oct 2009 23:02:35 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Exporting Figures from MATLAB</title>
		<link>http://blogs.cae.tntech.edu/mwr/2007/03/08/exporting-figures-from-matlab/</link>
		<comments>http://blogs.cae.tntech.edu/mwr/2007/03/08/exporting-figures-from-matlab/#comments</comments>
		<pubDate>Thu, 08 Mar 2007 17:31:06 +0000</pubDate>
		<dc:creator>Mike Renfro</dc:creator>
				<category><![CDATA[LaTeX]]></category>
		<category><![CDATA[Matlab]]></category>

		<guid isPermaLink="false">http://blogs.cae.tntech.edu/mwr/2007/03/08/exporting-figures-from-matlab/</guid>
		<description><![CDATA[I just discovered the Wordpress.com MATLAB feed today. Frinkytown&#8217;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:

MS Word is the devil, and Equation Editor is its evil spawn. When I started writing my thesis, I had been using [...]]]></description>
			<content:encoded><![CDATA[<p>I just discovered the Wordpress.com MATLAB feed today. <a href="http://frinkytown.wordpress.com/2007/01/29/matlab-copy-and-paste-still-broken-after-all-these-years/">Frinkytown&#8217;s complaint about copying and pasting figure</a> reminded me of things I had to do to write my M.S. thesis, and other things I discovered afterwards:</p>
<ol>
<li>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&#8217;m a <img src='/latexrender/pictures/c51d7e23458ca0e7373a8ed6ab56b2b9.gif' title='\LaTeX' alt='\LaTeX' align=absmiddle> geek now.</li>
<li><strike>Windows Metafiles</strike> What Word does to Windows Metafiles is a close third on deviltry.</li>
</ol>
<p><span id="more-6"></span>To the specific question of getting decent graphs from MATLAB to Word, I&#8217;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&#8217;re doing line graphs. Bitmaps will be ugly. Metafiles will look much better, since they&#8217;re normally a vector file format, and should be resolution-independent.</p>
<p><strong>However</strong>, 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&#8217;t actually make any edits. You have been warned. Example:</p>
<p align="center"><a href="http://blogs.cae.tntech.edu/mwr/files/2007/03/matlab-sine.png" title="matlab-sine.png"><img src="http://blogs.cae.tntech.edu/mwr/files/2007/03/matlab-sine.png" alt="matlab-sine.png" /></a><br />
Original Matlab Figure</p>
<p align="center">&nbsp;</p>
<p align="center"><a href="http://blogs.cae.tntech.edu/mwr/files/2007/03/word-sine.png" title="word-sine.png"><img src="http://blogs.cae.tntech.edu/mwr/files/2007/03/word-sine.png" alt="word-sine.png" /></a><br />
Matlab Figure Pasted Into Word</p>
<p align="center">&nbsp;</p>
<p align="center"><a href="http://blogs.cae.tntech.edu/mwr/files/2007/03/word-sine-after.png" title="word-sine-after.png"><img src="http://blogs.cae.tntech.edu/mwr/files/2007/03/word-sine-after.png" alt="word-sine-after.png" /></a><br />
Matlab Figure After Edit Picture Menu</p>
<p>I hate Word. &#8220;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!&#8221;</p>
<p>The only way to avoid this problem (aside from never clicking Edit Picture) is to insert figures in a format that Word won&#8217;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&#8217;t any higher quality. For me, that leaves Encapsulated PostScript (EPS).</p>
<p>EPS advantages:</p>
<ol>
<li>Natively exported from MATLAB</li>
<li>Standard for &#8220;journal-quality&#8221; graphs.</li>
<li>Can be converted to PDF relatively easily if you have <img src='/latexrender/pictures/c51d7e23458ca0e7373a8ed6ab56b2b9.gif' title='\LaTeX' alt='\LaTeX' align=absmiddle> around somewhere, even if it&#8217;s on a remote Unix system.</li>
</ol>
<p>EPS disadvantages:</p>
<ol>
<li>Requires a PostScript-compatible printer. No printing an final copy on my dad&#8217;s Dell all-in-one inkjet.</li>
<li>Takes some extra effort to get a preview in Word.</li>
</ol>
<p>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:</p>
<pre>
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</pre>
<p>(Apologies for the formatting; I don&#8217;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 <code>printall('axis([0 10 -100 100])')</code> at a MATLAB prompt and get all their axes sized consistently. A following <code>printall('print -depsc2 fig%d.eps')</code> would give me a fig1.eps, fig2.eps, &#8230; etc. from the current figure windows.</p>
<p>Now that I&#8217;m a <img src='/latexrender/pictures/c51d7e23458ca0e7373a8ed6ab56b2b9.gif' title='\LaTeX' alt='\LaTeX' align=absmiddle> geek, my current favorite for getting figures out of MATLAB is <code>printall('pdfprint')</code> &#8212; with this combination of MATLAB and a <img src='/latexrender/pictures/c51d7e23458ca0e7373a8ed6ab56b2b9.gif' title='\LaTeX' alt='\LaTeX' align=absmiddle> distribution, I can generate cropped PDF files ready to reference from a .tex file.<br />
<code><br />
function result=pdfprint(pdfName)<br />
% PDFPRINT   Print a figure window to an Adobe PDF file.<br />
%    SYNTAX:<br />
%      pdfprint<br />
%        PDFPRINT alone sends the current figure to a PDF file named<br />
%        FigureN.pdf, where N is the current figure number.<br />
%<br />
%      pdfprint filename.pdf<br />
%        Same as above but sends the output to a file named filename.pdf<br />
if nargin&lt;1<br />
figureNumber=gcf;<br />
pdfName=sprintf('Figure%d.pdf',figureNumber);<br />
end<br />
tempBasename=tempname;<br />
psTempFile=sprintf('%s.ps',tempBasename);<br />
pdfTempFile=sprintf('%s.pdf',tempBasename);<br />
print('-depsc2',psTempFile);<br />
epstopdfCommand=sprintf('epstopdf %s',psTempFile);<br />
[status,output]=system(epstopdfCommand);<br />
if status~=0<br />
warning('epstopdf command had non-zero return value');<br />
warning(output);<br />
result=1;<br />
else<br />
if ispc<br />
delCommand=sprintf('cmd /c del %s',psTempFile);<br />
renameCommand=sprintf('cmd /c move %s %s',pdfTempFile,pdfName);<br />
else<br />
delCommand=sprintf('rm %s',psTempFile);<br />
renameCommand=sprintf('mv %s %s',pdfTempFile,pdfName);<br />
end<br />
[status,output]=system(renameCommand);<br />
if status~=0<br />
warning('rename command had non-zero return value');<br />
warning(output);<br />
result=2;<br />
end<br />
[status,output]=system(delCommand);<br />
if status~=0<br />
warning('delete command had non-zero return value');<br />
warning(output);<br />
result=3;<br />
end<br />
end</code></p>
<p>Ben Hinkle at Mathworks posted <a href="http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=727&amp;objectType=FILE" target="_blank">some helpful figure export scripts</a> in the File Exchange in 2001, and wrote <a href="http://www.mathworks.com/company/digest/december00/export.shtml" target="_blank">an article about exporting figures</a> in late 2000.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.cae.tntech.edu/mwr/2007/03/08/exporting-figures-from-matlab/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
