Search code examples
matlabmatlab-figure

How to check if an axes handle is cleared or not


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.


Solution

  • 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).