Print figure or save to specific file format
collapse all in page
Syntax
print(filename,formattype)
print(filename,formattype,formatoptions)
print
print(printer)
print(driver)
print(printer,driver)
print('-clipboard',clipboardformat)
print(resize,___)
print(resolution,___)
print(renderer,___)
print(fig,___)
cdata = print('-RGBImage');
Description
example
print(filename,formattype)
saves the current figure to a file using the specified file format, such as print('BarPlot','-dpng')
. If the file name does not include an extension, then print
appends the appropriate one.
print(filename,formattype,formatoptions)
specifiesadditional options that are available for some formats.
example
print
prints the current figure to thedefault printer.
print(printer)
specifies the printer. Specify the printer as a character vector or string containing the printer name preceded by -P
, for example, '-Pmy printer'
. The printer must be set up on your system.
print(driver)
specifiesthe driver. Use this option if you want to ensure that the printedoutput is either black and white or color.
print(printer,driver)
specifiesthe printer and the driver.
example
print(
copiesthe current figure to the clipboard using the format specified by '-clipboard'
,clipboardformat)clipboardformat
.You can paste the copied figure into other applications.
example
print(resize,___)
maximizes the figure size to fill the page. Specify resize
as '-bestfit'
to preserve the figure's aspect ratio or '-fillpage'
to ignore the aspect ratio. These options are valid only when saving to a page format (PDF, and PS) or printing to a printer. Use this option with any of the input arguments from the previous syntaxes.
example
print(resolution,___)
uses the specified resolution. Specify the resolution as a character vector or string containing an integer value preceded by -r
, for example, '-r200'
. Use this option with any of the input arguments from the previous syntaxes.
print(renderer,___)
uses the specified renderer. Specify the renderer as either '-vector'
or '-image'
.
example
print(fig,___)
savesor prints the figure or Simulink® block diagram specified by fig
.
example
cdata = print('-RGBImage');
returns the RGB image data for the current figure. This option differs from screen captures in that all printing features apply to the output. You can also specify the resolution, renderer, and fig options with this syntax. However, you cannot specify a Simulink block diagram.
Examples
collapse all
Print Paper Copy of Figure
Create a bar chart and print it to your systemdefault printer. If you do not specify the figure to print, then print
usesthe current figure.
bar(1:10)print
Copy Figure to Clipboard
Create a plot and copy it to the system clipboard.
plot(1:10)print('-clipboard','-dmeta')
You can paste the copied plot into other applications.
Save Figure as Image File
Create a plot and save it as a PNG image file.
bar(1:10)print('BarPlot','-dpng')
print
saves the plot as BarPlot.png
.
Save Figure as Vector Graphics File
Create a plot and save it as an Encapsulated PostScript® file.
bar(1:10)print('BarPlot','-depsc')
print
saves the plot as BarPlot.eps
.
Add TIFF Preview to EPS File
Save the current figure as an Encapsulated PostScript Fileand add a TIFF preview.
surf(peaks)print('SurfacePlot','-depsc','-tiff')
Specify Figure to Save
Save a specific figure by passing its objectvariable to print
.
fig = figure;plot(1:10)print(fig,'MySavedPlot','-dpng')
Alternatively, refer to a figure using the value of its Number
property,which is the integer value that displays in the figure window titlebar. For example, save the figure with Figure 2
displayedin the title bar. Precede the integer value by -f
.
figure(2);plot(1:10)print('-f2','MySavedPlot','-dpng')
Save Figure at Screen Size and Resolution
Save a surface plot to a PNG file. Set the PaperPositionMode
propertyfor the figure to 'auto'
so that it saves at thesize displayed on the screen. Use '-r0'
to saveit with screen resolution.
surf(peaks)set(gcf,'PaperPositionMode','auto')print('PeaksSurface','-dpng','-r0')
Save Figure that Fills Page
Save a figure that fills the page using the '-fillpage'
option.
bar([1 10 7 8 2 2 9 3 6])print('FillPageFigure','-dpdf','-fillpage')
Return RGB Image Data for Figure
Return the RGB image data for a figure.
surf(peaks)cdata = print('-RGBImage');
Display the image data at full resolution using imshow
.
imshow(cdata)
Create High Resolution Movie Frame
Create a surface plot. Return the RGB image data for the figure and specify the image resolution. Then, convert the image data to a movie frame, F
.
surf(peaks)cdata = print('-RGBImage','-r120');F = im2frame(cdata);
Input Arguments
collapse all
filename
— File name
character vector | string
File name, specified as a character vector or string containing the desired file name and path.
Example: 'My Saved Chart'
Example: 'Folder\My Saved Chart'
Example: "My Saved Chart"
The maximum file name length, including the path, is operatingsystem and file format specific. Typically, the file name should beno more than 126 characters, or if you include the path, then no morethan 128 characters.
Data Types: char
| string
formattype
— File format
'-djpeg'
| '-dpng'
| '-dtiff'
| '-dpdf'
| '-deps'
| ...
File format, specified as one of the options in these tables.
Image File
An image file contains a pixel-based representation of figure. The size of the generated file depends on the figure, the format, and your system resolution. Images are widely used by web browsers and other applications that display graphics. However, they do not support transparency or scale well and you cannot modify individual graphics objects, such as lines and text, in other graphics applications.
This table lists the supported image formats.
Option | Image Format | Corresponding File Extension |
---|---|---|
'-djpeg' | JPEG 24-bit | .jpg |
'-dpng' | PNG 24-bit | .png |
'-dtiff' | TIFF 24-bit (compressed) | .tif |
'-dtiffn' | TIFF 24-bit (not compressed) | .tif |
'-dmeta' | Enhanced metafile (Windows only) | .emf |
Vector Graphics File
Vector graphics files store commands that redraw the figure. This type of format scales well, but can result in a large file. In some cases, vector graphics might contain stray lines or other visual artifacts. Some applications support extensive editing of vector graphics formats. However, some applications do not support editing beyond resizing the graphic. In general, try to make all the necessary changes while your figure is still in MATLAB®.
If you set the Renderer
property for the figure, then print
uses that renderer when generating output. Otherwise, print
chooses the appropriate renderer. Typically, print
generates vector graphics files that scale well when resized. For some complex figures, the files might contain embedded images instead. These images don't scale well, and the extent to which you can edit them in other applications is limited. To ensure that print
uses the vector graphics renderer, specify '-vector'
as an input argument to the print
function.
If you want output that has transparency, then create a vector graphics file using a Metafile, PDF, or SVG format. If you use an EPS format, then transparency is only supported for the figure and axes backgrounds. Image files do not support transparency, but will closely match what is shown on screen to give the appearance of transparency.
Note
The default figure renderer is OpenGL®. If the figure rendererdiffers from the renderer used when generating output, some detailsof the saved figure can differ from the figure on the display. Ifnecessary, you can make the displayed figure and the saved figureuse the same renderer. Set the Renderer
propertyfor the figure or specify the renderer inputargument to the print
function.
This table lists the supported vector graphics formats.
Option | VectorGraphics Format | Corresponding 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' | SVG (Scalable Vector Graphics) | .svg |
You cannot save Simulink block diagrams as EPS files.
Note
Only the PDF format uses the first two elements of the PaperPosition
property. Other formats ignore these values.
formatoptions
— Additional formatting options
'-tiff'
| '-loose'
| '-cmyk'
| '-append'
Additional formatting options supported by some file formats,specified as one or more of these values:
'-tiff'
— Include a TIFFpreview. EPS files only.'-loose'
— Use a loose boundingbox. EPS and PS files only.'-cmyk'
— Use CMYK colorsinstead of RGB colors. EPS and PS files only.'-append'
— Append the figureto an existing PS file. PS files only.
Example: print('my file','-deps','-tiff','-loose')
savesthe current figure to the file my file.eps
usinga loose bounding box and includes a TIFF preview.
printer
— Printer name
character vector | string
Printer name, specified as a character vector or string containing -P
and the printer name.
Example: '-Pmy local printer'
Example: "-Pmy local printer"
To view a list of available printers, use this command:
[~,printers] = findprinters
If you do not specify a printer, then print
usesthe system default printer. If you want to set up a new printer orselect a different default printer, use the operating system printermanagement utilities. Restart MATLAB if you do not see a printerthat is set up already.
Data Types: char
| string
driver
— Printer driver
'-dwin'
| '-dwinc'
| '-dprn'
| '-dprnc'
Printer driver, specified as '-dwin'
, '-dwinc'
, '-dprn'
, or '-dprnc'
. If you do not specify a driver, then print
uses the default driver for your operating system.
The option you use depends on your system, for example:
System | Driver | Output |
---|---|---|
Windows | '-dwin' | Black and white |
'-dwinc' | Color | |
Linux® or Mac | '-dprn' | Black and white |
'-dprnc' | Color |
clipboardformat
— Format copied to clipboard
-dmeta
| -dbitmap
| -dpdf
Format copied to clipboard, specified as one of these options:
'-dmeta'
— Enhanced metafile(Windows only)'-dbitmap'
— Image file (Windows and macOS)'-dpdf'
— PDF file (Windows and macOS)
resize
— Option to expand figure to fill page
'-fillpage'
| '-bestfit'
Option to expand figure to fill page, specified as one of thesevalues:
'-fillpage'
— Maximize thesize of the figure to fill the page. Leave a .25 inch margin on allsides of the page. The tick marks, layout, and aspect ratio of thefigure might change.'-bestfit'
— Maximize thesize of the figure to fill the page, but preserve the aspect ratioof the figure. The figure might not fill the entire page. This optionleaves a minimum page margin of .25 inches.
Both options are valid only when printing a figure to a printeror saving to a page format such as PDF and PS. They are not validfor Simulink block diagrams.
resolution
— Resolution
character vector | string
Resolution, specified as a character vector or a string containing -r
and an integer value indicating the resolution in dots per inch. For example, '-r300'
sets the output resolution to 300 dots per inch. To specify screen resolution, use '-r0'
.
In general, using a higher resolution value yields higher-quality output, but at the cost of higher memory use and larger output files. The higher the resolution setting, the longer it takes to render your figure.
Specifying the resolution is useful when creating an image or when using the OpenGL renderer with a vector graphics file format (since OpenGL produces an image even with vector formats). Specifying the resolution has no effect when using the Painters renderer with a vector graphics file format, since Painters produces a true vector graphics file that contains the commands that redraw the figure.
Note
Simulink printing does not support the resolution option. For higher quality output of Simulink models, use a vector format such as SVG or PDF.
Data Types: char
| string
renderer
— Graphics renderer
'-image'
| '-vector'
Graphics renderer, specified as '-image'
or '-vector'
.
'-image'
— OpenGL renderer. Use this renderer when saving images. OpenGL produces an image even with vector formats, which might limit the extent to which you can edit the image in other applications.'-vector'
— Creates vector graphics output. Use this renderer when saving vector graphics files. If you save to a vector graphics file and if the figure RendererMode property is set to'auto'
, thenprint
automatically attempts to create vector graphics. If you want to ensure that your output format is a true vector graphics file, then specify the'-vector'
option. For example:print('-vector','-deps','myVectorFile')
Note
If you save a file with the '-vector'
option, you might encounter one or more of the following issues:
Longer rendering times
Incorrect arrangement of graphics objects in 3-D views
Stray lines
Lines that disappear if they are thinner than one pixel
If you do not specify the renderer, then print
automaticallyuses the appropriate renderer to produce the output format requested.However, if you set the Renderer
property for thefigure, then print
uses that renderer when generatingoutput.
fig
— Figure, Simulink block diagram
figure object | Simulink block diagram
Figure object or Simulink block diagram. You can referto a figure using either its object variable name or using the figurenumber preceded by -f
. For example, -f2
refersto the figure with a Number
property value of 2
.When specifying a Simulink block diagram, precede the model namewith -s
. Specify the current model using '-s'
.
You cannot save Simulink block diagrams as EPS files.
Output Arguments
collapse all
cdata
— Image data
n-by-m-by-3 array
Image data, returned as an n-by-m-by-3 array. The size of theimage data array depends on the PaperPosition propertyof the figure and the output resolution.
Note
Starting in R2015b, if you use print
withthe '-r0'
option on a high-resolution system, thenthe size of the cdata
output array is larger thanin previous releases or on other systems. Also, the number of elementsin cdata
might not match the size of the figurein pixels based on the figure’s PaperPosition
propertyand the root’s ScreenPixelsPerInch
property.For more information, see DPI-Aware Behavior in MATLAB.
Limitations
Starting MATLAB in no display mode on Linux orusing the
-noFigureWindows
startup option on anyplatform has these limitations forprint
:Printing or saving figures with visible uicontrols errors.
Always uses the painters renderer, even if you specify the
'-image'
option.
In MATLAB Online™,
print
only prints to PDF. For additional file format options, save the figure to a file by specifying a filename.When MATLAB is in no display mode, all Simulink printing functionality, including printing to a file, is turned off.
More About
collapse all
Current Figure
The current figure is typically the last figure that you create or click with the mouse. User interaction can change the current figure.
To print a specific figure, specify the figure as the first input argument. If you do not specify a figure, then the print
function acts on the figure returned by gcbf. If gcbf
returns empty, then print
acts on the figure returned by gcf.
Tips
You can set properties of the figure to control someprinting and saving parameters. This table lists properties of thefigure related to printing and saving.
Figure Property Description PaperPosition Size of the printed or saved figure. If printing to a printeror a full-page output format, then this property also determines thefigure location on the page. PaperPositionMode Specifies whether to use the PaperPosition
propertyor the size of the figure on the screen to set the size of the printedor saved figure.InvertHardcopy Specifies whether to use the current background color of thefigure or to change the background color to white when printing orsaving the figure. PaperOrientation Figure orientation on printed page. PaperType Standard printer paper size. PaperSize Custom width and height of printer paper. PaperUnits Units for the PaperSize
andPaperPosition
properties.If you are using a Linux or Mac system and get an error about an invalid or unrecognized printer, save the contents of the figure as a PDF file. For example:
Then print the PDF file using an external PDF viewer.print('MyPlot.pdf','-dpdf')
If you print a figure that has a callback definedfor the
SizeChangedFcn
property and if the outputsize differs from the size of the figure on the screen, then theprint
functiondisplays a warning message. To avoid the warning message, set thePaperPositionMode
propertyfor the figure to'auto'
.
Alternative Functionality
Since in R2020a
The exportgraphics function saves the contents of any axes, figure, chart that can be a child of a figure, tiled chart layout, or container such as a panel. This function provides a better alternative to the print
function when you want to:
Save graphics displayed in an app or in MATLAB Online
Minimize the white space around the content
Save a PDF fragment with embeddable fonts
Save a multipage PDF (since R2021b)
Save a subset of the content in the figure
Control the background color without having to modify properties on the figure
The copygraphics function provides much of the same functionality as the exportgraphics
function, except that it copies the content to your system clipboard instead of saving it to a file. Use this function to copy and paste content from MATLAB into other applications.
Version History
Introduced before R2006a
expand all
R2023b: UI components will not be supported in a future release
In a future release, the print
function will no longer support printing or exporting UI components. All UI components will be excluded regardless of whether you specify the '-noui'
option when calling print
.
To export a figure containing UI components, call the exportapp function. For example, create a simple app containing two buttons and a slider. Export the contents of the figure as a PDF file by calling the exportapp
function.
% Create figure with three UI componentsf = uifigure;button1 = uibutton(f,"Position",[150 300 100 50]);button2 = uibutton(f,"Position",[300 300 100 50]);slider1 = uislider(f,"Position",[150 250 250 3]);% Export the contents of the figureexportapp(f,"myapp.pdf")
Alternatively, call the getframe function to capture the contents of the figure. Then call the imwrite function to save the content. This time, save the content as a JPEG file.
F = getframe(f);imwrite(F.cdata,"myapp.jpg");
This change was announced in R2022b. In R2023b, the print
function issues a warning when you export a figure containing UI components.
R2023b: PostScript (.ps
) files will not be supported in a future release
In a future release, the print
function will no longer support creating full-page PostScript (.ps
) files. To export vector graphics files, use one of these methods:
Call the exportgraphics function. Specify an .eps
, .pdf
, or .emf
file extension and set the ContentType
option to "vector"
. This function captures content that is tightly cropped around plots, and it does not create full-page output. For example, create a plot and save the contents of the current figure as a PDF file containing vector graphics.
plot([0 3 2 4 1]);exportgraphics(gcf,"myplot.pdf","ContentType","vector")
Alternatively, call the print
function and specify an .eps
, .emf
, or .svg
file extension. For example, create a plot and save the contents of the current figure as an EPS file.
plot([0 3 2 4 1]);print("myplot.eps","-depsc")
For the PostScript options supported in earlier releases, see print (R2022a).
This change was announced in R2022b. In R2023b, the print
function issues a warning when you export a figure to a PostScript file.
R2023b: BMP, HDF, PBM, PCX, PGM, and PPM files will not be supported in a future release
The BMP, HDF, PBM, PCX, PGM, and PPM file formats will no longer be supported in a future release.
To export graphics using one of these formats, use the imwrite function instead. For example, create a line plot, and capture the contents of the current figure using the getframe function. Then save the content as a BMP file.
plot([0 3 2 4 1]);F = getframe(gcf);imwrite(F.cdata,"myplot.bmp");
For the file format options supported in earlier releases, see print (R2022a).
This change was announced in R2022b. In R2023b, the print
function issues a warning when you export a figure to one of these file formats.
R2021b: The print
options, -opengl
and -painters
, are not recommended
The following print
options are no longer recommended. There are no plans to remove the values, and they will continue to behave the same way as in previous releases. The following table lists the recommended replacement options.
Not Recommended | Replacement Option |
---|---|
The print('-opengl','-dpdf','myfigure.pdf') | Use the print('-image','-dpdf','myfigure.pdf') |
The print('-painters','-dpdf','myfigure.pdf') | Use the print('-vector','-dpdf','myfigure.pdf') |
R2016a: Printed and Saved Figures Match On-Screen Size
Printed and saved figures match the size of the figure on the screen by default. Previously, printed and saved figures were 8-by-6 inches by default.
See Also
savefig | saveas | copygraphics | exportgraphics | getframe | sprintf
Topics
- Compare Ways to Export Graphics from Figures
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本 (日本語)
- 한국 (한국어)
Contact your local office