SaveFigure is a Matlab utility which provides aesthetically pleasing figure export which provides a few essential features not present in Matlab's built in figure export or any known utility on the FileExchange:
- instant export to multiple formats, including PDF, SVG, EPS, PNG, while ensuring that all formats look identical
identical output on multiple platforms (Linux and MacOS currently supported, Windows support should not be difficult to add, contact me if interested)
- preserves alpha blending and transparency on patches, lines, markers, etc.
- sets or preserves nicely rendered fonts (specified in options)
- preserves vector graphics
I've found the outputs to be more consistent, more faithful to the on-screen displayed figure. Please note that this submission includes code copied wholesale from Juerg Schwizer, Oliver Woodford, Yair Altman, and Peder Axensten.
sudo apt-get install inkscape imagemagick
For Mac, install homebrew [ http://brew.sh/ ] and then run:
brew install inkscape imagemagick
(you could also do "brew cask install inkscape" instead).
For Windows: doesn't work yet, but I should hopefully be able to figure this out soon. If you'd like to help, I'd be happy to accept pull requests on github!
See https://github.com/djoshea/matlab-save-figure for more details and to file issues.
To achieve faithful, WYSIWYG vector reproduction of Matlab figures, we generate SVGs, then convert to PDF using Inkscape, and then to PNG and EPS if requested using ImageMagick's convert utility. I've found that convert rarely does a nice job going from SVG to PDF directly, though this could simply be a matter of setting the correct flags. So Matlab is only responsible for generating the SVG. For newer versions of Matlab (R2014a or newer), saveFigure uses Matlab's new internal SVG engine, i.e. print -dsvg, which faithfully reproduces Matlab figures as SVG. For older versions, the code for generating SVGs is essentially a nice wrapper around Juerg Schwizer's plot2svg utility, with a few minor tweaks. The advantage of this approach is that we have complete control of figure output and appearance; the disadvantage is that it requires a complete reconstruction of the figure as an SVG. Consequently, it may not perfectly reproduce the figure in all instances, but it does a fairly decent job.
Daniel J O'Shea (2021). matlab-save-figure (https://github.com/djoshea/matlab-save-figure), GitHub. Retrieved .
FYI, for anyone using a super-old version of Matlab, the line"
"if contains(File, [FSep, '.'])"
Will break. I believe that can be replaced by
"if ~isempty( strfind(File, [FSep '.'] ) )"
Thanks Oliver, transparent backgrounds work for me on Mac. Try setting the figure and axes background to 'none', e.g. using set(gcf, 'Background', 'none') and set(gca, 'Background', 'none'); then use saveFigure to save a png or pdf. It looks transparent when I open it in Illustrator and place it on top of other things. Let me know if this doesn't work for you, or better yet, file an issue on Github so I can help fix it. Thanks!
It'll be great if it supports transparent background as well
how to use this script in win platform？？
It's the print -svg version that's freezing; haven't tested whether plot2svg would struggle as well, but probably. The figure in question includes a patch object with colors defined at the vertices; svg doesn't support color gradients, so on export these patches are broken into thousands of triangle component patches. This seems to surpass my computer's memory capabilities (though damned if it doesn't try its hardest!).
Re the path, it convert and imageMagick work w/o the full path on my system, but I also use a slightly non-standard way of setting my Matlab system path (I source my .bash_profile directly from the Matlab executable, so my internal Matlab $PATH always matches my Terminal $PATH).
Hi Kelly, thanks for pointing that out. I just removed the hard-coded paths and it's working fine for me. I must have put that in long ago; I'm guessing the system path on my mac wasn't setup correctly so I hard-coded it. Try it now and see if it works? Incidentally, I've also played around with clearing the LD_LIBRARY_PATH so that inkscape and convert can use the system libraries rather than Matlab's older versions.
I haven't come across the svg export issue yet. My code here either uses the print -dsvg option for newer versions of Matlab or calls out to a lightly modified version of Juerg Schwizer's SVG export for older versions. Do you know which path causes the freeze? If you can reproduce it, do you want to send me a .fig file I can try to debug with (although if it's Matlab's fault, maybe a support request)? My email is my github user name at gmail.
I use MacPorts rather than Homebrew, so both imageMagick and Inkscape are found in /opt/local/bin/ instead of /usr/local/bin/convert. Is hard-coding that really necessary? On my system, I can call both functions from within Matlab without using the full path.
Also, I've found that svg export can freeze Matlab if you have too many graphics objects. Not sure if that's something you can check for in advance though.
Otherwise, looks like a promising function.
Oliver: ugh, so sorry! I've fixed it in the description. Thanks, please let me know how it works for you!
Victor, Arnold: I'm very interested in getting it working for Windows, but haven't had time. As you've discovered, it should be as simple (or as difficult) as just calling out the command line and perhaps fixing some unix / windows shell issues. If you make any progress, please let me know! Arnold, would you like to file an issue on Github so we can look into the solution together? Thanks!
This sounds great. I look forward to trying it. I'm commenting now because you've spelt my name wrong.
anyone... how do I enable command line access for inkscape under windows properly?
I can't seem to get it running. Inkscape's path is included properly but I get an error in the matlab console stating that the 'export' command is either misspelled or couldn't be found.
i would like to try it in windows. Is that possible to update the function in order to allow it.
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!