Get handle of (non graphics) object

4 views (last 30 days)
No Name
No Name on 22 Jul 2019
Edited: Matt J on 22 Jul 2019
Getting the handle of a figure can be done like this, among others:
hFigure = findall(groot, 'Type', 'Figure');
Let's say I have a class defined like this:
classdef BasicHandle < handle
properties
Prop1
end
methods
% Constructor
function obj = BasicHandle(val)
if nargin > 0
obj.Prop1 = val;
end
end
end
end
Is it possible to find all objects of class 'BasicHandle', by using a similar method as findall?
Something like:
hBasic = getHandle('Class', 'BasicHandle');

Answers (1)

Matt J
Matt J on 22 Jul 2019
Edited: Matt J on 22 Jul 2019
The only way I can think of to implement something like that is using persistent variables, like in the classdef below. The workflow would then be like this,
>> obj1=BasicHandle(1);
>> obj2=BasicHandle(2);
>> H=BasicHandle.getHandles
H =
1×2 BasicHandle array with properties:
Prop1
>> BasicHandle.clearHandles;
classdef BasicHandle < handle
properties
Prop1
end
methods
% Constructor
function obj = BasicHandle(val)
if nargin > 0
obj.Prop1 = val;
recHandles(obj)
end
end
end
methods (Static)
function h=getHandles
h=recHandles;
end
function clearHandles
recHandles;
end
end
end
function objs=recHandles(obj)
persistent allHandles
if nargin
allHandles{end+1}=obj;
elseif nargout
if ~isempty(allHandles)
objs=[allHandles{:}];
else
objs=allHandles;
end
else
allHandles=[];
end
end

Categories

Find more on Graphics Object Properties 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!