Save Axes Without Saving UI Controls

This example shows two approaches for saving only the axes from a figure that has UI controls. First, the example shows how to use print with the '-noui' option. Then, the example shows how to copy the axes to a new figure and save the new figure instead.

Alternatively, you can interactively save only the axes from a figure using the export button in the axes toolbar. The toolbar appears when you hover over the axes. When you save axes using the export button, the saved output is tightly cropped around the axes content, including any legends or colorbars. The output does not include content that is not related to the axes, such as UI controls or other axes in the figure.

Create Figure with UI Controls

To create an example of a figure with UI controls, set your current folder to one to which you have write access. Then, copy this example code.

copyfile(fullfile(docroot,'techdoc','creating_guis','examples','simple_gui2*.*'));
simple_gui2

Save Axes Without Saving UI Controls

To save the figure and exclude the UI controls from the saved output, use print with the '-noui' option. print leaves blank space in place of the UI controls. If you do not specify the '-noui' option, then print includes the UI controls in the saved output.

To maintain the current figure background color in the saved figure, set the InvertHardcopy property of the figure to 'off'. Otherwise, the saved figure has a white background. Starting in R2014b, you can use dot notation to set properties. If you are using an earlier release, use the set function instead.

fig = gcf;
fig.InvertHardcopy = 'off';
print('PlotWithoutUIControls','-dpng','-noui')

Copy Axes to New Figure and Save

To eliminate the blank space, copy the axes to a new figure and resize the axes to fill the figure. For example, click the axes to make it the current axes. Use copyobj to copy it to a new figure. Then, set the Position property of the new axes to fill the figure.

ax_old = gca;
f_new = figure;
ax_new = copyobj(ax_old,f_new)
set(ax_new,'Position','default')

Save the new figure using either saveas or print.

print(f_new,'AxesOnly','-dpng')

See Also

|

Related Topics