print - Print figure or save to specific file format - MATLAB (original) (raw)

Print figure or save to specific file format

Syntax

Description

print([filename](#bukyb6e-1-filename),[formattype](#bukyb6e-1-formattype)) saves the current figure to a file using the specified file format, such asprint("BarPlot","-dpng"). If the file name does not include an extension, then print appends the appropriate one.

example

print([filename](#bukyb6e-1-filename),[formattype](#bukyb6e-1-formattype),[formatoptions](#bukyb6e-1-formatoptions)) specifies additional options that are available for some formats.

print prints the current figure to the default printer.

example

print([printer](#bukyb6e-1-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](#bukyb6e-1-driver)) specifies the driver. Use this option if you want to ensure that the printed output is either black and white or color.

print([printer](#bukyb6e-1-printer),[driver](#bukyb6e-1-driver)) specifies the printer and the driver.

print(`"-clipboard"`,[clipboardformat](#bukyb6e-1-clipboardformat)) copies the current figure to the clipboard using the format specified byclipboardformat. You can paste the copied figure into other applications.

example

print([resize](#bukyb6e-1-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](#bukyb6e-1-resolution),___) uses the specified resolution. Specify the resolution as a string containing an integer value preceded by "-r", for example, "-r200". Use this option with any of the input arguments from the previous syntaxes.

example

print([contenttype](#bukyb6e-1-renderer),___) specifies the type of content to create as "-vector" (for vector graphics) or "-image" (for images).

print([fig](#bukyb6e-1-fig),___) saves or prints the figure or Simulink® block diagram specified by fig.

example

[cdata](#bukyb6e-1-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 theresolution, contenttype, andfig options with this syntax. However, you cannot specify a Simulink block diagram.

example

Examples

collapse all

Create a bar chart and print it to your system default printer. If you do not specify the figure to print, then print uses the current figure.

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.

Create a plot and save it as a PNG image file.

bar(1:10) print("BarPlot","-dpng")

print saves the plot as BarPlot.png.

Create a plot and save it as an Encapsulated PostScript® file.

bar(1:10) print("BarPlot","-depsc")

print saves the plot as BarPlot.eps.

Save the current figure as an Encapsulated PostScript File and add a TIFF preview.

surf(peaks) print("SurfacePlot","-depsc","-tiff")

Save a specific figure by passing its object variable 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 title bar. For example, save the figure with Figure 2 displayed in the title bar. Precede the integer value by-f.

figure(2); plot(1:10) print("-f2","MySavedPlot","-dpng")

Save a surface plot to a PNG file. Set thePaperPositionMode property for the figure to"auto" so that it saves at the size displayed on the screen. Use "-r0" to save it with screen resolution.

surf(peaks) set(gcf,"PaperPositionMode","auto") print("PeaksSurface","-dpng","-r0")

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 the RGB image data for a figure.

surf(peaks) cdata = print("-RGBImage");

Display the image data at full resolution usingimshow.

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

File name, specified as a string or character vector 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 operating system and file format specific. Typically, the file name should be no more than 126 characters, or if you include the path, then no more than 128 characters.

Data Types: char | string

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®.

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 thatprint creates all vector graphics content, specify the "-vector" input argument.

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.

This table lists the supported vector graphics formats.

Option Vector Graphics 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 thePaperPosition property. Other formats ignore these values.

Additional formatting options supported by some file formats, specified as one or more of these values:

Example: print("myfile","-deps","-tiff","-loose") saves the current figure to the file myfile.eps using a loose bounding box and includes a TIFF preview.

Printer name, specified as a character vector or string containing -P and the printer name.

Example: "-Pmy local printer"

Example: "-Pmy local printer"

If you do not specify a printer, then print uses the system default printer. If you want to set up a new printer or select a different default printer, use the operating system printer management utilities. Restart MATLAB if you do not see a printer that is set up already.

Data Types: char | string

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

Format copied to clipboard, specified as one of these options:

Option to expand figure to fill page, specified as one of these values:

Both options are valid only when printing a figure to a printer or saving to a page format such as PDF and PS. They are not valid for Simulink block diagrams.

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, and in some cases, when creating a vector graphics file. Some vector graphics files include image content which can be affected by the resolution. If the content contains all vector graphics—for example, if you specify the "-vector" option—then the resolution has no effect.

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

Content type, specified as "-image" or"-vector".

Note

If you save a file with the "-vector" option, you might encounter one or more of the following issues:

If you do not specify the content type, then print selects a content type produce the output format requested. Some vector graphics formats, such as PDF and EPS, might contain nonvector (image) content.

Figure object or Simulink block diagram. You can refer to a figure using either its object variable name or using the figure number preceded by-f. For example, -f2 refers to the figure with a Number property value of2. When specifying a Simulink block diagram, precede the model name with-s. Specify the current model using"-s".

You cannot save Simulink block diagrams as EPS files.

Output Arguments

collapse all

Image data, returned as an n-by-m-by-3 array. The size of the image data array depends on the PaperPosition property of the figure and the output resolution.

Limitations

More About

collapse all

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 bygcf.

Tips

Alternative Functionality

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.

exportgraphics is a better alternative to theprint function when you want to:

The copygraphics function provides much of the same functionality as theexportgraphics 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

The print function no longer clips the content of a large figure when the page is smaller than the figure. Instead, it shrinks the content to fit on the page and preserves the aspect ratio of the figure.

The Renderer property of a figure has no effect on the output produced by the print function.

The InvertHardCopy property of a figure has no effect. If you call the print function save a figure to a file or copy it to the clipboard, the default background color corresponds to the theme of the figure. The background is white if the figure has the light theme. The background is dark gray (almost black) if the figure has the dark theme. If you set theColor property of the figure before callingprint, the output uses the color you specify regardless of the theme.

If you call the print function to print a hard copy, the background color is white (no ink) by default. If you set theColor property of the figure, the output uses that color.

If you write a SizeChangedFcn callback for a figure, the callback no longer executes when you call the print function.

The print function no longer supports figures that contain UI components, such as buttons and sliders, or containers, such as panels and tabs.

To export a figure containing UI components or containers, 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 theexportapp function.

% Create figure with three UI components f = 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 figure exportapp(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 JPG file.

F = getframe(f); imwrite(F.cdata,"myapp.jpg");

This change was announced in R2022b. In R2023b through R2024b, theprint function issues a warning if you export a figure containing UI components.

The print function no longer supports creating full-page PostScript (.ps) files. To export vector graphics files, use one of these methods:

Call the exportgraphics function. Specify an .svg,.eps, .pdf, or .emf file extension and set the ContentType name-value argument 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")

You can also use the exportgraphics function to append the output to an existing PDF file.

exportgraphics(gcf,"existingfile.pdf",Append=true)

Alternatively, call the print function and specify an.svg, .eps, .pdf, or.emf 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")

Before R2025a: See print (R2022a) for details about the PostScript options supported in earlier releases.

This change was announced in R2022b. In R2023b through R2024b, theprint function issues a warning when you export a figure to a PostScript file.

The print function no longer supports the BMP, HDF, PBM, PCX, PGM, and PPM file formats.

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 thegetframe function. Then save the content as a BMP file.

plot([0 3 2 4 1]); F = getframe(gcf); imwrite(F.cdata,"myplot.bmp");

Before R2025a: See print (R2022a) for the file format options supported in earlier releases.

This change was announced in R2022b. In R2023b through R2024b, theprint function issues a warning if you export a figure to one of these file formats.

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 -opengl renderer option. For example:print("-opengl","-dpdf","myfigure.pdf") Use the -image option. For example:print("-image","-dpdf","myfigure.pdf")
The -painters renderer option. For example:print("-painters","-dpdf","myfigure.pdf") Use the -vector option. For example:print("-vector","-dpdf","myfigure.pdf")

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.