MATLAB: How to get the figure window handle containing a given UI component

ancestoraxeschart;componentcontainingfigureMATLABparentplot

I have some UI component (e.g. axes, chart, line) handle and I want to find the handle to the figure window containing this component. How can I do this?

Best Answer

Use the "ancestor" function. Assuming the UI component is called "obj", you can use the following code to get the figure handle:
figureHandle = ancestor(obj, 'figure');
Note that this function will return an empty array if the component does not belong to a figure window.
Additionally, you may check parent of the UI component "obj.Parent".