-1

How can I remove a children object from a figure in MATLAB?

1- Suppose I want to remove (not invisible) an axes from a figure. How to do it?
2- Since axes is a children of figure, can the same approach be used in removing any type of children of a figure?

I searched here, and docs but I don't want to make it invisible. I want to delete it.

Zeta.Investigator
  • 911
  • 2
  • 14
  • 31

1 Answers1

3

If you already have the axes handle (from either figure.Children or otherwise), you can simply use delete to remove it from the figure regardless of it's visibility.

fig = figure();
hax = axes('Parent', fig);

% Delete the axes directly
delete(hax)

If you don't have access to the handle, you can get it using findobj or findall (findall even locates axes with HandleVisibility turned to 'off') to locate axes which belong to your figure and then delete to remove it.

delete(findobj(gcf, 'type', 'axes'));
% delete(findall(gcf, 'type', 'axes'));

If your axes has a specific Tag property, you can further filter by that

delete(findobj(gcf, 'type', 'axes', 'tag', 'mytag'));
% delete(findall(gcf, 'type', 'axes', 'tag', 'mytag'));

You can pass any property / value pair to findobj and findall so you could even just delete all invisible axes:

delete(findobj(gcf, 'type', 'axes', 'visible', 'off'))
Suever
  • 64,497
  • 14
  • 82
  • 101