image thumbnail

Export figure or axis as vector graphics or as a bitmap.

version 1.0.3 (5.31 KB) by Jesús Lucio
'export_figure_as_vecgra' exports a figure or axis as vector graphics; 'export_figure_as_bitmap' exports a figure or part of it as bitmap.

44 Downloads

Updated 17 Feb 2020

View License

function export_figure_as_vecgra(fh, filename, format0, axh)

EXPORTS A FIGURE OR AXIS PLOT TO A VECTOR GRAPHICS (PDF, EPS, EMF, SVG, PS) FILE.

INPUTS:
fh: Handle to figure. If omitted (or empty[]) the current figure is exported.
filename: A name for the file, without the extension. If omitted (or empty[]) a dialog asks for the name and format.
format0: A vector graphics format to export the figure (see possible values below). If omitted (or empty[]) a dialog allows to choose the format.
axh: Handle to axis in figure. If omitted, the whole figure is printed.
If there is a value, it must be a handle to the axis to be exported.
If empty[], then the current axis is exported.

Advise: possibly the best performance can be obtained exporting the figure or axis to SVG and, if needed, then editing (by using suitable software, like Inkscape (https://inkscape.org/), which is free and open-source) and exporting it to a different format.

Based on: FIGURE2PDF (https://uk.mathworks.com/matlabcentral/fileexchange/71053-figure2pdf) Export the current MATLAB figure to a PDF file.

OPTIONS FOR format0 FILE EXTENSION
'-dpdf' Full page Portable Document Format (PDF) color .pdf
'-deps' Encapsulated PostScript (EPS) Level 3 black and white .eps
'-depsc' Encapsulated PostScript (EPS) Level 3 color .eps
'-deps2' Encapsulated PostScript (EPS) Level 2 black and white .eps
'-depsc2' Encapsulated PostScript (EPS) Level 2 color .eps
'-dmeta' Enhanced Metafile (Windows® only) .emf
'-dsvg' Scalable Vector Graphics (SVG) .svg
'-dps' Full-page PostScript (PS) Level 3 black and white .ps
'-dpsc' Full-page PostScript (PS) Level 3 color .ps
'-dps2' Full-page PostScript (PS) Level 2 black and white .ps
'-dpsc2' Full-page PostScript (PS) Level 2 color .ps

EXAMPLES:
export_figure_as_vecgra : exports the current figure (complete), asking first for a file name and format.
export_figure_as_vecgra(f1) : exports the figure with handle 'f1' (complete), asking first for a file name and format.
export_figure_as_vecgra([], 'myfile') : exports the current figure (complete), with the given file name and asking for a format.
export_figure_as_vecgra([], 'myfile', '-dpdf') : exports the current figure (complete) as PDF, with the given file name and format.
export_figure_as_vecgra([], [], [], []) : exports the current axis, asking first for a file name and format.
export_figure_as_vecgra([], 'myfile', '-dsvg', []) : exports the current axis as SVG, with the given file name and format.
export_figure_as_vecgra(f1, 'myfile', '-dmeta', ax3) : exports the axis with handle 'ax3' of figure with handle 'f1' as EMF, with the given file name.

_____________________________________________________

function export_figure_as_bitmap(fh, filename, crop, varargin)

EXPORTS A FIGURE OR PART OF IT TO BITMAP (many possible formats) FILE.

INPUT ARGUMENTS:
fh: handle to the figure to be saved. If omitted ([], or nonexistent) the program takes the current figure;
filename: name of the file to be created (extension included). If omitted ([], or nonexistent) the program opens
a dialogue to ask for the name and extension. If the user chooses 'jpg' and no properties are given, the program asks for quality;
crop: logical value to ask for a crop of the figure. If omitted ([], or nonexistent) or it is false,
the program simply exports the whole figure, without cropping it;
property-value pairs: to set properties to certain formats.

VALID EXTENSIONS:
bmp: Windows Bitmap
gif: Graphics Interchange Format
hdf: Hierarchical Data Format (HDF4)
jpg (or jpeg): Joint Photographic Experts Group (JPEG). The program will ask for quality of file.
jp2: Joint Photographic Experts Group 2000 (JPEG 2000)
pbm: Portable Bitmap
pcx: ZSoft Paintbrush
pgm: Portable Graymap
png: Portable Network Graphics
pnm: Portable Anymap
ppm: Portable Pixmap
ras: Sun™ Raster
tif (or tiff): Tagged Image File Format
xwd: X Windows Dump

EXAMPLES:
export_figure_as_bitmap exports current figure, asking first for file name and extension.
export_figure_as_bitmap(f1) exports figure with handle 'f1', asking first for file name and extension.
export_figure_as_bitmap([], 'myfile') exports current figure, asking first for the extension.
export_figure_as_bitmap([], 'myfile.jpg') exports current figure, with the given file name and extension (it will ask for quality of final file).
export_figure_as_bitmap([], 'myfile.jpg', 0, 'quality', 100, 'comment', 'perfectimage') exports current figure, with the given file name and extension and property-value pairs.
export_figure_as_bitmap([], [], 1) exports current figure, asking for file name and extension.

Cite As

Jesús Lucio (2021). Export figure or axis as vector graphics or as a bitmap. (https://www.mathworks.com/matlabcentral/fileexchange/74168-export-figure-or-axis-as-vector-graphics-or-as-a-bitmap), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2018a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!