MATLAB: How to copy an image of an axes without including the entire figure

axesclipboardcomponentcopyhgexportMATLABprint

Best Answer

Consider a simple figure with tabular and plotted data:
f = figure;
x = [1;2;3];
y1 = [1;2;3];
y2 = [1;3;5];
t = uitable(f,'Data',[x y1 y2],'ColumnName',{'x','y1','y2'},'RowName',{},'Units','normalized');
t.Position = [.03 .65 t.Extent(3:4)];
ax = axes('Position',[.5 .1 .45 .8]);
plot(x,[y1 y2]);
legend({'y1','y2'});
Here are three ways to copy an image of just the axes to the clipboard:
1. Hover your mouse over the axes and a toolstrip 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. To achieve this programmatically, create a new empty figure and use "copyobj" to move a copy of just the axes into the new figure. You can then use "hgexport" or "print" as before.
For example,
f2 = figure;
copyobj(ax,f2);
hgexport(f2,'-clipboard');
3. Another 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:
For this example, you can take a screencapture of the axes without even copying the object into a new figure.Please note that this function is user-submitted, so it is not officially supported or checked for bugs.