Copy plot or graphics content to clipboard (2024)

Copy plot or graphics content to clipboard

Since R2020a

collapse all in page

Syntax

copygraphics(obj)

copygraphics(obj,Name,Value)

Description

example

copygraphics(obj) copies the content of the graphics object specified by obj to the system clipboard. The graphics object can be any type of axes, a figure, a standalone visualization, a tiled chart layout, or a container within a figure. The clipboard content is available for pasting into other applications and documents.

example

copygraphics(obj,Name,Value) specifies additional options for copying the content to the clipboard. For example, copygraphics(gca,'Resolution',300) copies the contents of the current axes to the clipboard as a 300-DPI image.

Examples

collapse all

Copy Axes

Open Live Script

Create a line plot and get the current axes. Then copy the contents of the axes to the clipboard.

plot(rand(5,5))ax = gca;copygraphics(ax)

Copy plot or graphics content to clipboard (1)

Specify Image Resolution

Open Live Script

Display an image and get the current axes. Then copy the contents of the axes as a 300-DPI image.

I = imread('peppers.png');imshow(I)ax = gca;copygraphics(ax,'Resolution',300)

Copy plot or graphics content to clipboard (2)

Copy Figure

Open Live Script

Display a plot with an annotation that extends beyond the bounds of the axes. Then copy the contents of the figure.

plot(1:10)annotation('textarrow',[0.06 0.5],[0.73 0.5],'String','y = x ')f = gcf;copygraphics(f)

Copy plot or graphics content to clipboard (3)

Copy Chart as Vector Graphics

Open Live Script

Display a bar chart and get the current axes. Then copy the contents of the axes as a vector graphic.

bar([10 22 31 43])ax = gca;copygraphics(ax,'ContentType','vector')

Copy plot or graphics content to clipboard (4)

Copy Tiled Chart Layout

Open Live Script

Display two plots in a tiled chart layout. Then copy both plots to the clipboard by passing the TiledChartLayout object to the copygraphics function.

t = tiledlayout(2,1);nexttileplot([1 2 3])nexttileplot([3 2 1])copygraphics(t)

Copy plot or graphics content to clipboard (5)

If you want to copy just one of the plots, call the nexttile function with the axes return argument. Then pass the axes to the copygraphics function.

Copy Heatmap With Transparent Background

Open Live Script

Display a heatmap chart. Then copy the chart as a vector graphic and specify a transparent background.

h = heatmap(rand(10,10));copygraphics(h,'ContentType','vector','BackgroundColor','none')

Copy plot or graphics content to clipboard (6)

Create App for Copying Plot

Create a program file called copyapp.m that displays a plot and a button for copying the axes content to the clipboard. In the callback function for the button, call the copygraphics function.

function copyappf = uifigure;ax = uiaxes(f,'Position',[25 25 400 375]);plot(ax,[0 0.3 0.1 0.6 0.4 1])b = uibutton(f,'Position',[435 200 90 30],'Text','Copy Plot');b.ButtonPushedFcn = @buttoncallback; function buttoncallback(~,~) copygraphics(ax) endend

Run the app by calling the copyapp function. When you click the Copy Plot button, the axes content is copied to the clipboard. The area surrounding the axes, including the button, is not copied.

copyapp

Copy plot or graphics content to clipboard (7)

Input Arguments

collapse all

objGraphics object
axes | figure | standalone visualization | tiled chart layout | ...

Graphics object, specified as one of these objects:

  • Any type of axes: an Axes, PolarAxes, or GeographicAxes object.

  • A figure created with either the figure or uifigure function.

  • A standalone visualization such as a heatmap chart.

  • A tiled chart layout, which you create with the tiledlayout function.

  • A container within a figure: a Panel, Tab, or ButtonGroup object.

Capture Area

copygraphics captures the contents of the object you specify. It does not capture UI components such as buttons or sliders.

It also does not capture adjacent containers or child containers. For example, consider a figure containing a line plot with an adjacent panel containing a heatmap:

f = figure;ax = axes(f,'Position',[0.1 0.1 0.4 0.8]);plot(ax,[0 1])p = uipanel(f,'Position',[0.55 0.1 0.4 0.8]);heatmap(p,rand(10,5))copygraphics(f)copygraphics(p)

Copy plot or graphics content to clipboard (8)

In the preceding code, the first copygraphics command copies the line plot, but not the heatmap. The second copygraphics command copies the heatmap, but not the line plot.

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: copygraphics(gca,'Resolution',300) copies the contents of the current axes as 300-DPI image.

ContentTypeType of content
'auto' (default) | 'vector' | 'image'

Type of content to copy, specified as one of these options:

  • 'auto' — MATLAB® controls whether the content is a vector graphic or an image.

  • 'vector' — Copies the content as a vector graphic that can scale to any size.

  • 'image' — Copies the content as a rasterized image.

Note

If you specify the 'vector' option, some visualizations might contain stray lines or other artifacts.

ResolutionResolution (DPI)
150 (default) | whole number

Resolution in dots per inch (DPI), specified as a whole number that is greater than or equal to 1.

Specifying the resolution has no effect when the ContentType is 'vector'.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

BackgroundColorBackground color
[1 1 1] (default) | 'current' | 'none' | RGB triplet | 'r' | 'g' | 'b' | ...

Background color, specified as 'current', 'none', an RGB triplet, a hexadecimal color code, or a color name. The background color controls the color of the margin that surrounds the axes or chart.

  • A value of 'current' sets the background color to the parent container's color.

  • A value of 'none' sets the background color to transparent or white, depending on the value of ContentType:

    • When ContentType='vector', the background color is transparent.

    • When ContentType='image', the background color is white.

    • When ContentType='auto', MATLAB sets the background color according to the heuristic it uses to determine the type content to copy.

  • Alternatively, specify a custom color or a named color.

Custom Colors and Named Colors

RGB triplets and hexadecimal color codes are useful for specifying custom colors.

  • An RGB triplet is a three-element row vector whose elements specify the intensities of the red, green, and blue components of the color. The intensities must be in the range [0,1]; for example, [0.4 0.6 0.7].

  • A hexadecimal color code is a character vector or a string scalar that starts with a hash symbol (#) followed by three or six hexadecimal digits, which can range from 0 to F. The values are not case sensitive. Thus, the color codes "#FF8800", "#ff8800", "#F80", and "#f80" are equivalent.

Alternatively, you can specify some common colors by name. This table lists the named color options, the equivalent RGB triplets, and hexadecimal color codes.

Color NameShort NameRGB TripletHexadecimal Color CodeAppearance
"red""r"[1 0 0]"#FF0000"

Copy plot or graphics content to clipboard (9)

"green""g"[0 1 0]"#00FF00"

Copy plot or graphics content to clipboard (10)

"blue""b"[0 0 1]"#0000FF"

Copy plot or graphics content to clipboard (11)

"cyan" "c"[0 1 1]"#00FFFF"

Copy plot or graphics content to clipboard (12)

"magenta""m"[1 0 1]"#FF00FF"

Copy plot or graphics content to clipboard (13)

"yellow""y"[1 1 0]"#FFFF00"

Copy plot or graphics content to clipboard (14)

"black""k"[0 0 0]"#000000"

Copy plot or graphics content to clipboard (15)

"white""w"[1 1 1]"#FFFFFF"

Copy plot or graphics content to clipboard (16)

Here are the RGB triplets and hexadecimal color codes for the default colors MATLAB uses in many types of plots.

RGB TripletHexadecimal Color CodeAppearance
[0 0.4470 0.7410]"#0072BD"

Copy plot or graphics content to clipboard (17)

[0.8500 0.3250 0.0980]"#D95319"

Copy plot or graphics content to clipboard (18)

[0.9290 0.6940 0.1250]"#EDB120"

Copy plot or graphics content to clipboard (19)

[0.4940 0.1840 0.5560]"#7E2F8E"

Copy plot or graphics content to clipboard (20)

[0.4660 0.6740 0.1880]"#77AC30"

Copy plot or graphics content to clipboard (21)

[0.3010 0.7450 0.9330]"#4DBEEE"

Copy plot or graphics content to clipboard (22)

[0.6350 0.0780 0.1840]"#A2142F"

Copy plot or graphics content to clipboard (23)

ColorspaceColorspace
'rgb' (default) | 'gray'

Colorspace of the saved graphic, specified as 'rgb' or 'gray'.

  • 'rgb' — Copy truecolor RGB content.

  • 'gray' — Convert the content to grayscale.

Limitations

The copygraphics function is not supported in MATLAB Online™ or in web apps hosted on the MATLAB Web App Server™.

Alternative Functionality

Hovering over the Export button Copy plot or graphics content to clipboard (24) in the axes toolbar reveals a drop-down menu with options for exporting content:

  • Copy plot or graphics content to clipboard (25): Save the content as a tightly cropped image or PDF.

  • Copy plot or graphics content to clipboard (26): Copy the content as an image.

  • Copy plot or graphics content to clipboard (27): Copy the content as a vector graphic.

Version History

Introduced in R2020a

See Also

exportgraphics

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.

Copy plot or graphics content to clipboard (28)

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

Contact your local office

Copy plot or graphics content to clipboard (2024)
Top Articles
Latest Posts
Article information

Author: Stevie Stamm

Last Updated:

Views: 6476

Rating: 5 / 5 (80 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Stevie Stamm

Birthday: 1996-06-22

Address: Apt. 419 4200 Sipes Estate, East Delmerview, WY 05617

Phone: +342332224300

Job: Future Advertising Analyst

Hobby: Leather crafting, Puzzles, Leather crafting, scrapbook, Urban exploration, Cabaret, Skateboarding

Introduction: My name is Stevie Stamm, I am a colorful, sparkling, splendid, vast, open, hilarious, tender person who loves writing and wants to share my knowledge and understanding with you.