Search code examples
matlabmatlab-figure

Find all objects in Matlab figure which have a callback


In my current figure I need to find all objects which have some callback set. More specifically, all objects which have a non-empty ButtonDownFcn.

I tried everything, e.g. findobj(gca, '-regexp', 'ButtonDownFcn', '\s+'), but I get this error: Warning: Regular expression comparison is not supported for the 'Callback' property: using ordinary comparison.

The problem is that the objects have no tags or anything which would "define" them uniquely, except that they are all "clickable". Is there an easy way to do it? Using findobj or findall. Thanks.


Solution

  • findobj does not support regular expression comparison for the 'ButtonDownFcn' property, as the warning says. This probably happens because the contents of the property are not necessarily text (they can be function handles).

    You can use a for loop over all objects of the figure:

    result = [];
    hh = findobj(gcf);
    for h = hh(:).'
        if ~isempty(get(h, 'ButtonDownFcn'))
            result = [result h];
        end
    end
    

    Or equivalently you can use arrayfun:

    hh = findobj(gcf);
    ind = arrayfun(@(h) ~isempty(get(h, 'ButtonDownFcn')), hh);
    result = hh(ind);