How to check if an axes handle is cleared or not
Asked Answered
F

1

5

I want to check if some axes have been cleared or not, based on which some further task is to be performed. I use cla to clear some axes, not delete. For example:

figure

hs1 = subplot(121); plot(rand(100,2), 'x');

hs2 = subplot(122); plot(rand(100,2), 'o');

cla(hs1)

Then, the question is how to determine if hs1 is cleared.

Foresaid answered 13/12, 2018 at 19:28 Comment(0)
P
6

The cla function removes all child objects with visible handles by default. You can therefore check if an axes has been cleared by seeing if it has any child objects using the allchild function:

isCleared = isempty(allchild(hs1));

A couple notes to keep in mind:

  • The cla function will not clear objects with hidden handles by default. The option cla(hs1, 'reset') is needed to clear hidden handles.

  • The allchild function will find all child objects regardless of their handle visibility. If you just want to check for child objects with visible handles, you can use isempty(hs1.Children).

Predisposition answered 13/12, 2018 at 19:33 Comment(2)
@gnonice: Correct! An alternative I just found is: isempty(hs1.Children)Foresaid
@jingweimo: Yes, that's another option, but it will only show child objects with visible handles. allchild shows everything, including hidden handles.Predisposition

© 2022 - 2024 — McMap. All rights reserved.