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:
- 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.
Windows MetafilesWhat 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 Figure Pasted Into Word
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).
- Natively exported from MATLAB
- Standard for “journal-quality” graphs.
- Can be converted to PDF relatively easily if you have [tex]\LaTeX[/tex] around somewhere, even if it’s on a remote Unix system.
- Requires a PostScript-compatible printer. No printing an final copy on my dad’s Dell all-in-one inkjet.
- 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.
% PDFPRINT Print a figure window to an Adobe PDF file.
% 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
warning('epstopdf command had non-zero return value');
delCommand=sprintf('cmd /c del %s',psTempFile);
renameCommand=sprintf('cmd /c move %s %s',pdfTempFile,pdfName);
renameCommand=sprintf('mv %s %s',pdfTempFile,pdfName);
warning('rename command had non-zero return value');
warning('delete command had non-zero return value');
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.