How do I copy an image of an axes in MATLAB without including the entire figure?

6 views (last 30 days)
I have an axes, with a plot and legend, inside of a larger figure (with extra components like buttons and tables). I want to copy an image of this axes to the clipboard and paste it in Microsoft Word. I know that I can use the "copygraphics" or "print" function to copy a figure. But I don't want the entire figure, just the axes. 

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 10 Oct 2024
Edited: MathWorks Support Team on 11 Oct 2024
There are three ways to copy an image of just the axes to the clipboard:
Consider a simple figure with tabular and plotted data:
x = [1;2;3];
y1 = [1;2;3];
y2 = [1;3;5];
f = figure;
t = uitable(f,'Data',[x y1 y2],'ColumnName',{'x','y1','y2'},'RowName',{},'Units','normalized');
t.Position = [.03 .65 t.Extent(3:4)];
t.Position(1:2) = [.05 .55];
ax = axes('Position',[.5 .1 .45 .8]);
plot(x,[y1 y2]);
legend({'y1','y2'});
1. Hover your mouse over the axes and a toolbar will appear at the top right corner. Hover over the leftmost icon and then select "Copy as image". This will copy an image of your axes to the clipboard exactly as is (including both plot and legend). If you cannot see the axes toolbar, make sure its "Visible" property is set to "on":
2. If you are using MATLAB R2020a or a later release, to achieve this programmatically, use "copygraphics". Specify the axes handle to copy the axes only.
For example,
copygraphics(ax);
 
3. If you are using a MATLAB release before MATLAB R2020a, an alternative programmatic solution is to use 'ScreenCapture', a File Exchange pick of the week. This code can copy images of figures - as well as specific components within a figure - to the clipboard.
More information can be found on this blog post:
Please note that this function is user-submitted, so it is not officially supported or checked for bugs.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!