MATLAB: How to print or save one axes from a set of axes and objects in a figure window

axesguiMATLABplotprintsavesinglesubplot

I would like to know how to print or save one axes from a set of axes and objects in a figure window.

Best Answer

There is no direct way to print a specific axes from a figure window, because the print routine is associated with the figure window rather than an axes. Since all the axes are part of one figure, you cannot print just one from it directly. However, you can use the COPYOBJ function to copy the required axes to a new figure and then print that figure window.
For example:
% make a figure with several axes
fig1 = figure;
xx = 0:pi/10:2*pi;
sp(1) = subplot(3,1,1);
plot(xx, 10*sin(xx));
sp(2) = subplot(3,1,2);
plot(xx, cos(xx));
sp(3) = subplot(3,1,3);
plot(xx, tan(xx));
% Create a Legend for the first axes
hLeg = legend(sp(1),'Signal')
% create a new figure for saving and printing
fig2 = figure('visible','off');
% copy axes into the new figure
newax = copyobj(sp(1),fig2);
% Since you have a LEGEND associated with the figure in the first axes
% you can also copy the legend to the new figure:
newLeg = copyobj(hLeg,fig2);
% If you would like to have the axes cover a larger area of the figure window rather
% than the original size as in the subplot, then change it's Position value:
set(newax, 'units', 'normalized', 'position', [0.13 0.11 0.775 0.815]);
% print and/or save the figure
print(fig2) % print it
hgsave(fig2,'myfig') % save it
close(fig2) % clean up by closing it
Now you can use the PRINT command, Print menu, or SAVEAS command to print the figure, export it to a graphics file, or save it to a FIG-file.