Find all objects which have a callback

5 views (last 30 days)
John Pisoir
John Pisoir on 19 Jan 2021
Commented: Steven Lord on 19 Jan 2021
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.
  1 Comment
Steven Lord
Steven Lord on 19 Jan 2021
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'm curious about your use case. Why do you need to find all objects with a specific callback? What do you want to do that applies to axes, image objects, text objects, uicontrols, etc. simultaneously?
The problem is that the objects have no tags or anything which would "define" them uniquely
Is this a figure that you created? If so I would advise tagging the objects in the figure as you create them or storing their handles in arrays that you can later use to refer to all objects with a certain purpose.

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 19 Jan 2021
Check the Type and Style property to determine what kind of control it is. Here, this may give you ideas:
% DisableAllControls_Pointer is a utility for a GUI apps. It disables all
% controls in a figure, returning the control names as a list in a cell
% array. It will also change the pointer property of the figure.
%
% WasEnabled = DisableControls(handles, NewPointer)
% handles structure with handles and user data
% WasEnabled array of all control handles that had been enabled, and were now disabled.
%
% Example
% WasEnabled = DisableControls(handles, 'Watch');
% % some code that takes a lot of time....
% EnableControls(WasEnabled);
function WasEnabled = DisableControls(handles, NewPointer)
try
AllObjs = findobj(gcf);
WasEnabled = [];
for Index = 1 : length(AllObjs)
thisHandle = AllObjs(Index);
% fprintf('Control #%d is type "%s".\n', Index, thisHandle.Type);
if strcmpi(thisHandle.Type, 'uicontrol')
fprintf(' Control #%d is type "%s", Style "%s", Tag "%s".\n', Index, thisHandle.Type, thisHandle.Style, thisHandle.Tag);
% Don't disable any button on the figure that has "Exit" as the string because we don't want to prevent the user from bailing out if necessary.
itsTheExitButton = strcmpi(thisHandle.Style, 'pushbutton') && contains(thisHandle.String, 'Exit', 'IgnoreCase', true);
if strcmpi(get(thisHandle, 'enable'), 'on') && ~itsTheExitButton
% If it was enabled, now disable it.
set(thisHandle, 'enable', 'off');
WasEnabled = [WasEnabled; thisHandle];
end
end
% Set pointer for figures (only) to specified pointer, such as a watch.
if strcmpi(thisHandle.Type, 'figure')
if exist('NewPointer', 'var')
% fprintf('Control #%d is type "%s".\n', Index, thisHandle.Type);
set(thisHandle, 'pointer', NewPointer);
end
end
end
drawnow;
catch ME
errorMessage = sprintf('Error in function %s() at line %d.\n\nError Message:\n%s', ...
ME.stack(1).name, ME.stack(1).line, ME.message);
fprintf(1, '%s\n', errorMessage);
uiwait(errordlg(errorMessage));
end
return;
  1 Comment
John Pisoir
John Pisoir on 19 Jan 2021
Edited: John Pisoir on 19 Jan 2021
Thanks, but I was hoping for a one-liner. Going in a loop through all the objects is not really what I am looking for.

Sign in to comment.

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!