Capture axes or figure as movie frame (2024)

Main Content

Capture axes or figure as movie frame

collapse all in page

Syntax

F = getframe

F = getframe(ax)

F = getframe(fig)

F = getframe(___,rect)

Description

example

F = getframe capturesthe current axes as it appears on the screen as a movie frame. F isa structure containing the image data. getframe capturesthe axes at the same size that it appears on the screen. It does notcapture tick labels or other content outside the axes outline.

example

F = getframe(ax) capturesthe axes identified by ax instead of the currentaxes.

example

F = getframe(fig) capturesthe figure identified by fig. Specify a figureif you want to capture the entire interior of the figure window, includingthe axes title, labels, and tick marks. The captured movie frame doesnot include the figure menu and tool bars.

F = getframe(___,rect) capturesthe area within the rectangle defined by rect.Specify rect as a four-element vector of the form [leftbottom width height]. Use this option with either the ax or fig inputarguments in the previous syntaxes.

Examples

collapse all

Capture Contents of Current Axes

Plot two lines. Capture the axes and return the image data. getframe captures the interior of the axes and the axes outline. It does not capture content that extends beyond the axes outline.

plot([0 1; 1 2])F = getframe;

Capture axes or figure as movie frame (1)

F is a structure with the field cdata that contains the captured image data.

Display the captured image data using imshow.

figureimshow(F.cdata)

Capture axes or figure as movie frame (2)

Capture Contents of Figure

Create a surface plot. Capture the interior of the figurewindow, excluding the menu and tool bars.

surf(peaks)F = getframe(gcf);

F is a structure with the field cdata thatcontains the captured image data.

Display the captured image data in a figure with a darker background using imshow, so you can see captured area.

figure('Color',[0.5 0.5 0.5])imshow(F.cdata)

Capture axes or figure as movie frame (4)

Specify Rectangular Region to Capture

Capture the interior of an axes plus a marginof 30 pixels in each direction. The added margin is necessary to includethe tick labels in the capture frame. Depending on the size of thetick labels, the margin might need to be adjusted.

Plot two lines.

plot([0 1; 1 2])

Capture axes or figure as movie frame (5)

Change the axes units to pixels and return the currentaxes position. The third and fourth elements of the position vectorspecify the axes width and height in pixels.

drawnowax = gca;ax.Units = 'pixels';pos = ax.Position
pos = 73.8000 47.2000 434.0000 342.3000

Create a four-element vector, rect,that defines a rectangular area covering the axes plus the desiredmargin. The first two elements of rect specifythe lower left corner of the rectangle relative to the lower leftcorner of the axes. The last two elements of rect specifythe width and height of the rectangle. Reset the axes units to thedefault value of 'normalized'.

marg = 30;rect = [-marg, -marg, pos(3)+2*marg, pos(4)+2*marg];F = getframe(gca,rect);ax.Units = 'normalized';

Display the captured image data in a figure with a darker background using imshow, so you can see captured area.

figure('Color',[0.5 0.5 0.5])imshow(F.cdata)

Capture axes or figure as movie frame (6)

Calculate Region to Include Title and Labels

Calculate a margin around the axes so thatthe captured image data includes the title, axis labels, and ticklabels.

Create a plot with a title and an x-axis label.

plot([0 1; 1 2])xlabel('x values')title('Plot of Two Lines')

Capture axes or figure as movie frame (7)

Change the axes units to pixels and store the Position and TightInset propertyvalues for the axes. The TighInset property isa four-element vector of the form [left bottom right top].The values are the margins used around the axes for the tick valuesand text labels.

drawnowax = gca;ax.Units = 'pixels';pos = ax.Position;ti = ax.TightInset;

Create a four-element vector, rect,that defines a rectangular area covering the axes plus the automaticallycalculated margin. The first two elements of rect specifythe lower left corner of the rectangle relative to the lower leftcorner of the axes. The last two elements of rect specifythe width and height of the rectangle.

rect = [-ti(1), -ti(2), pos(3)+ti(1)+ti(3), pos(4)+ti(2)+ti(4)];F = getframe(ax,rect);

Display the captured image data in a figure with a darker background using imshow, so you can see captured area.

figure('Color',[0.5 0.5 0.5])imshow(F.cdata)

Capture axes or figure as movie frame (8)

Capture Specific Axes

Call the tiledlayout function to create a 2-by-1 tiled chart layout. Call the nexttile function to create the axes objects ax1 and ax2. Plot a line in each axes.

tiledlayout(2,1)ax1 = nexttile;plot(1:10,'b')ax2 = nexttile;plot(1:10,'r')

Capture axes or figure as movie frame (9)

Capture the contents of the lower axes. getframe captures the interior and border of the plot. It does not capture tick values or labels that extend beyond the outline of the plot.

F = getframe(ax2);

Display the captured image data using imshow.

figureimshow(F.cdata)

Capture axes or figure as movie frame (10)

Record Frames and Play Movie

Record frames of thepeaksfunctionvibrating by using getframe in a loop.Preallocate an array to store the movie frames.

Z = peaks;surf(Z)axis tight manualax = gca;ax.NextPlot = 'replaceChildren';loops = 40;F(loops) = struct('cdata',[],'colormap',[]);for j = 1:loops X = sin(j*pi/10)*Z; surf(X,Z) drawnow F(j) = getframe(gcf);end

Playback the movie two times.

fig = figure;movie(fig,F,2)

Input Arguments

collapse all

axAxes to capture
Axes object | GeographicAxes object

Axes to capture, specified as an Axes object or a GeographicAxes object. Use this option if you want to capture an axes that is not the current axes.

getframe captures the content within thesmallest rectangle that encloses the axes outline. If you want tocapture all the tick values and labels, then use the fig inputargument instead.

Example: F = getframe(ax);

figFigure to capture
figure object

Figure to capture, specified as a Figure object.

rectRectangular area to capture
four-element vector of the form [left bottom widthheight]

Rectangular area to capture, specified as a four-element vectorof the form [left bottom width height] in pixels. The left and bottom elementsdefine the position of the lower left corner of the rectangle. Theposition is relative to the figure or axes that is specified as thefirst input argument to getframe. The width and height elementsdefine the dimensions of the rectangle.

Specify a rectangle that is fully contained within the figurewindow.

Note

In a future release, the rect argument will no longer capture the figure toolbar, menu bar, or the border around the figure. You will still be able to define a subsection of the figure to capture, but the toolbar, menu bar, and borders will not be included. As an alternative, you can use the exportapp function to capture the toolbar and menu bar, but not the borders.

Output Arguments

collapse all

F — Movie frame
structure

Movie frame, returned as a structure with two fields:

  • cdata — The image data storedas an array of uint8 values. The size of the imagedata array depends on your screen resolution.

  • colormap — The colormap.On true color systems, this field is empty.

Note

These are some important considerations about the size of cdata:

  • If you query the size of the region that getframe captures(either the figure, the axes, or the region specified by rect),the size in pixels mightnot match the number of elements in cdata. Thisdifference is because the number of elements in cdata dependson your screen resolution (and operating system settings), but pixelsin MATLAB® might not correspond to the actual pixels on your screen.

  • Starting in R2015b, if you are using a high-resolutionsystem, then the size of cdata might be largerthan in previous releases or on other systems.

Limitations

  • getframe does not support the following functionality in MATLAB Online™ or in Web Apps (MATLAB Compiler):

    • Capturing the contents of a figure created with the uifigure function or any axes in the figure.

    • Capturing the contents of an app created with App Designer or any axes in the app.

More About

collapse all

Pixels

Distances in pixels are independent of yoursystem resolution on Windows® and Macintosh systems:

  • On Windows systems, a pixel is 1/96th of an inch.

  • On Macintosh systems, a pixel is 1/72nd of aninch.

On Linux® systems, the size of a pixel is determinedby your system resolution.

Tips

  • For the fastest performance when using getframe,make sure that the figure is visible on the screen. If the figureis not visible, getframe can still capture thefigure, but performance can be slower.

  • For more control over the resolution of the imagedata, use the print functioninstead. The cdata output argument with print returnsthe image data. The resolution input argument controlsthe resolution of the image.

  • To ensure that colorbars and legends displayed next to 3-D plots are captured, specify the fig argument when you call getframe.

Version History

Introduced before R2006a

expand all

See Also

movie | frame2im | im2frame | image | imshow | print | writeVideo

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.

Capture axes or figure as movie frame (11)

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

Capture axes or figure as movie frame (2024)
Top Articles
Latest Posts
Article information

Author: Reed Wilderman

Last Updated:

Views: 5727

Rating: 4.1 / 5 (52 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Reed Wilderman

Birthday: 1992-06-14

Address: 998 Estell Village, Lake Oscarberg, SD 48713-6877

Phone: +21813267449721

Job: Technology Engineer

Hobby: Swimming, Do it yourself, Beekeeping, Lapidary, Cosplaying, Hiking, Graffiti

Introduction: My name is Reed Wilderman, I am a faithful, bright, lucky, adventurous, lively, rich, vast person who loves writing and wants to share my knowledge and understanding with you.