{"id":427,"date":"2007-03-08T11:31:06","date_gmt":"2007-03-08T17:31:06","guid":{"rendered":"http:\/\/blogs.cae.tntech.edu\/mwr\/2007\/03\/08\/exporting-figures-from-matlab\/"},"modified":"2024-10-27T14:26:20","modified_gmt":"2024-10-27T14:26:20","slug":"exporting-figures-from-matlab","status":"publish","type":"post","link":"https:\/\/sites.tntech.edu\/renfro\/2007\/03\/08\/exporting-figures-from-matlab\/","title":{"rendered":"Exporting Figures from MATLAB"},"content":{"rendered":"<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>\n<ol>\n<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 [latex]\\LaTeX[\/latex] geek now.<\/li>\n<li><strike>Windows Metafiles<\/strike> What Word does to Windows Metafiles is a close third on deviltry.<\/li>\n<\/ol>\n<p><!--more-->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>\n<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>\n<p align=\"center\"><a href=\"http:\/\/sites.tntech.edu\/renfro\/wp-content\/uploads\/sites\/111\/2007\/03\/matlab-sine.png\" title=\"matlab-sine.png\"><img decoding=\"async\" src=\"http:\/\/sites.tntech.edu\/renfro\/wp-content\/uploads\/sites\/111\/2007\/03\/matlab-sine.png\" alt=\"matlab-sine.png\" \/><\/a><br \/>\nOriginal Matlab Figure<\/p>\n<p align=\"center\">&nbsp;<\/p>\n<p align=\"center\"><a href=\"http:\/\/sites.tntech.edu\/renfro\/wp-content\/uploads\/sites\/111\/2007\/03\/word-sine.png\" title=\"word-sine.png\"><img decoding=\"async\" src=\"http:\/\/sites.tntech.edu\/renfro\/wp-content\/uploads\/sites\/111\/2007\/03\/word-sine.png\" alt=\"word-sine.png\" \/><\/a><br \/>\nMatlab Figure Pasted Into Word<\/p>\n<p align=\"center\">&nbsp;<\/p>\n<p align=\"center\"><a href=\"http:\/\/sites.tntech.edu\/renfro\/wp-content\/uploads\/sites\/111\/2007\/03\/word-sine-after.png\" title=\"word-sine-after.png\"><img decoding=\"async\" src=\"http:\/\/sites.tntech.edu\/renfro\/wp-content\/uploads\/sites\/111\/2007\/03\/word-sine-after.png\" alt=\"word-sine-after.png\" \/><\/a><br \/>\nMatlab Figure After Edit Picture Menu<\/p>\n<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>\n<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>\n<p>EPS advantages:<\/p>\n<ol>\n<li>Natively exported from MATLAB<\/li>\n<li>Standard for &#8220;journal-quality&#8221; graphs.<\/li>\n<li>Can be converted to PDF relatively easily if you have [tex]\\LaTeX[\/tex] around somewhere, even if it&#8217;s on a remote Unix system.<\/li>\n<\/ol>\n<p>EPS disadvantages:<\/p>\n<ol>\n<li>Requires a PostScript-compatible printer. No printing an final copy on my dad&#8217;s Dell all-in-one inkjet.<\/li>\n<li>Takes some extra effort to get a preview in Word.<\/li>\n<\/ol>\n<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>\n<pre>\nfunction out=printall(printcmd)\n% printall - Print all currently open figures\n%\n% With no arguments, this prints all figures with a\n% regular print command.\n%\n% printall('printcmd') prints all figures with the\n% print command 'printcmd'\n%\n% Examples:\n%\n% printall('print -dmeta fig%d.wmf') prints all figures\n%                                    to Windows metafiles\n%                                    named fig1.wmf, fig2.wmf,\n%                                    etc.\nif nargin==0\nprintcmd='print';\nend\nfigs=sort(get(0,'Children'));\nfor count=1:length(figs)\nfeval('figure',figs(count));\neval(sprintf(printcmd,figs(count)));\nend<\/pre>\n<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>\n<p>Now that I&#8217;m a [tex]\\LaTeX[\/tex] geek, my current favorite for getting figures out of MATLAB is <code>printall('pdfprint')<\/code> &#8212; with this combination of MATLAB and a [tex]\\LaTeX[\/tex] distribution, I can generate cropped PDF files ready to reference from a .tex file.<br \/>\n<code><br \/>\nfunction result=pdfprint(pdfName)<br \/>\n% PDFPRINT   Print a figure window to an Adobe PDF file.<br \/>\n%    SYNTAX:<br \/>\n%      pdfprint<br \/>\n%        PDFPRINT alone sends the current figure to a PDF file named<br \/>\n%        FigureN.pdf, where N is the current figure number.<br \/>\n%<br \/>\n%      pdfprint filename.pdf<br \/>\n%        Same as above but sends the output to a file named filename.pdf<br \/>\nif nargin&lt;1<br \/>\nfigureNumber=gcf;<br \/>\npdfName=sprintf('Figure%d.pdf',figureNumber);<br \/>\nend<br \/>\ntempBasename=tempname;<br \/>\npsTempFile=sprintf('%s.ps',tempBasename);<br \/>\npdfTempFile=sprintf('%s.pdf',tempBasename);<br \/>\nprint('-depsc2',psTempFile);<br \/>\nepstopdfCommand=sprintf('epstopdf %s',psTempFile);<br \/>\n[status,output]=system(epstopdfCommand);<br \/>\nif status~=0<br \/>\nwarning('epstopdf command had non-zero return value');<br \/>\nwarning(output);<br \/>\nresult=1;<br \/>\nelse<br \/>\nif ispc<br \/>\ndelCommand=sprintf('cmd \/c del %s',psTempFile);<br \/>\nrenameCommand=sprintf('cmd \/c move %s %s',pdfTempFile,pdfName);<br \/>\nelse<br \/>\ndelCommand=sprintf('rm %s',psTempFile);<br \/>\nrenameCommand=sprintf('mv %s %s',pdfTempFile,pdfName);<br \/>\nend<br \/>\n[status,output]=system(renameCommand);<br \/>\nif status~=0<br \/>\nwarning('rename command had non-zero return value');<br \/>\nwarning(output);<br \/>\nresult=2;<br \/>\nend<br \/>\n[status,output]=system(delCommand);<br \/>\nif status~=0<br \/>\nwarning('delete command had non-zero return value');<br \/>\nwarning(output);<br \/>\nresult=3;<br \/>\nend<br \/>\nend<\/code><\/p>\n<p>Ben Hinkle at Mathworks posted <a href=\"http:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/loadFile.do?objectId=727&amp;objectType=FILE\" target=\"_blank\" rel=\"noopener noreferrer\">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\" rel=\"noopener noreferrer\">an article about exporting figures<\/a> in late 2000.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/sites.tntech.edu\/renfro\/2007\/03\/08\/exporting-figures-from-matlab\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Exporting Figures from MATLAB&#8221;<\/span><\/a><\/p>\n","protected":false},"author":87,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,11],"tags":[],"class_list":["post-427","post","type-post","status-publish","format-standard","hentry","category-latex","category-matlab","entry"],"_links":{"self":[{"href":"https:\/\/sites.tntech.edu\/renfro\/wp-json\/wp\/v2\/posts\/427","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sites.tntech.edu\/renfro\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sites.tntech.edu\/renfro\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sites.tntech.edu\/renfro\/wp-json\/wp\/v2\/users\/87"}],"replies":[{"embeddable":true,"href":"https:\/\/sites.tntech.edu\/renfro\/wp-json\/wp\/v2\/comments?post=427"}],"version-history":[{"count":1,"href":"https:\/\/sites.tntech.edu\/renfro\/wp-json\/wp\/v2\/posts\/427\/revisions"}],"predecessor-version":[{"id":510,"href":"https:\/\/sites.tntech.edu\/renfro\/wp-json\/wp\/v2\/posts\/427\/revisions\/510"}],"wp:attachment":[{"href":"https:\/\/sites.tntech.edu\/renfro\/wp-json\/wp\/v2\/media?parent=427"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sites.tntech.edu\/renfro\/wp-json\/wp\/v2\/categories?post=427"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sites.tntech.edu\/renfro\/wp-json\/wp\/v2\/tags?post=427"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}