ancestor

Finds the closest enclosing object of a given type, such as the axes or figure of a line.

Category: Graphics > Graphics Objects

Syntax

p = ancestor(h, type)

Walks up from h and returns the first ancestor of the given type.

p = ancestor(h, type, 'toplevel')

Returns the highest ancestor of that type instead of the nearest.

Description

ancestor follows the Parent chain upward until it meets an object of the requested type. If h itself has that type it is returned unchanged. This is handy inside callbacks, where the source object is known but its axes or figure is not.

Input arguments

h
Graphics handle or array of handles.
type
Type name such as 'axes' or 'figure', or a cell array of names.

Output arguments

p
Handle of the ancestor, or an empty array if none matches.

Example

Axes and figure of a line

f = figure;
ax = axes;
h = plot(1:3);
disp(ancestor(h, 'axes') == ax)
disp(ancestor(h, 'figure') == f)
Output
   1
   1

See also

allchild findobj gca gcf get