1 %ISOLATE_AXES Isolate the specified axes in a figure on their own
4 % fh = isolate_axes(ah)
5 % fh = isolate_axes(ah, vis)
7 % This
function will create a
new figure containing the axes/uipanels
8 % specified, and also their associated legends and colorbars. The objects
9 % specified must all be in the same figure, but they will generally only be
10 % a subset of the objects in the figure.
13 % ah - An array of axes and uipanel handles, which must come from the
15 % vis - A
boolean indicating whether the
new figure should be visible.
19 % fh - The handle of the created figure.
21 % Copyright (C) Oliver Woodford 2011-2013
23 % Thank you to Rosella Blatt for reporting a bug to do with axes in GUIs
24 % 16/3/2012 Moved copyfig to its own function. Thanks to Bob Fratantonio
25 % for pointing out that the function is also used in export_fig.m.
26 % 12/12/12 - Add support for isolating uipanels. Thanks to michael for
28 % 08/10/13 - Bug fix to allchildren suggested by Will Grant (many thanks!).
29 % 05/12/13 - Bug fix to axes having different units. Thanks to Remington
30 % Reid for reporting the issue.
32 function fh = isolate_axes(ah, vis)
33 % Make sure we have an array of handles
35 error('ah must be an array of handles');
37 % Check that the handles are all for axes or uipanels, and are all in the same figure
38 fh = ancestor(ah(1), 'figure');
41 if ~ismember(get(ah(a), 'Type'), {
'axes',
'uipanel'})
42 error(
'All handles must be axes or uipanel handles.');
44 if ~isequal(ancestor(ah(a),
'figure'), fh)
45 error('Axes must all come from the same figure.');
48 % Tag the objects so we can find them in the copy
49 old_tag = get(ah, 'Tag');
53 set(ah,
'Tag',
'ObjectToCopy');
54 % Create a
new figure exactly the same as the old one
55 fh = copyfig(fh); %copyobj(fh, 0);
57 set(fh,
'Visible',
'off');
59 % Reset the
object tags
61 set(ah(a),
'Tag', old_tag{a});
63 % Find the objects to save
64 ah = findall(fh,
'Tag',
'ObjectToCopy');
67 error(
'Incorrect number of objects found.');
69 % Set the axes tags to what they should be
71 set(ah(a),
'Tag', old_tag{a});
73 % Keep any legends and colorbars which overlap the subplots
74 lh = findall(fh,
'Type',
'axes',
'-and', {
'Tag',
'legend',
'-or',
'Tag',
'Colorbar'});
77 set([ah(:); lh(:)],
'Units',
'normalized');
78 ax_pos =
get(ah,
'OuterPosition');
80 ax_pos = cell2mat(ax_pos(:));
82 ax_pos(:,3:4) = ax_pos(:,3:4) + ax_pos(:,1:2);
83 leg_pos =
get(lh,
'OuterPosition');
85 leg_pos = cell2mat(leg_pos);
87 leg_pos(:,3:4) = leg_pos(:,3:4) + leg_pos(:,1:2);
88 ax_pos = shiftdim(ax_pos, -1);
90 M = bsxfun(@lt, leg_pos(:,1), ax_pos(:,:,3)) & ...
91 bsxfun(@lt, leg_pos(:,2), ax_pos(:,:,4)) & ...
92 bsxfun(@gt, leg_pos(:,3), ax_pos(:,:,1)) & ...
93 bsxfun(@gt, leg_pos(:,4), ax_pos(:,:,2));
94 ah = [ah; lh(any(M, 2))];
96 % Get all the objects in the figure
98 % Delete everything except
for the input objects and associated items
99 delete(axs(~ismember(axs, [ah; allchildren(ah); allancestors(ah)])));
102 function ah = allchildren(ah)
110 function ph = allancestors(ah)
113 h = get(ah(a), 'parent');
116 h = get(h, 'parent');