Get handle of (non graphics) object

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');

Réponses (1)

Matt J
Matt J le 22 Juil 2019
Modifié(e) : Matt J le 22 Juil 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

Catégories

Question posée :

le 22 Juil 2019

Modifié(e) :

le 22 Juil 2019

Community Treasure Hunt

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

Start Hunting!

Translated by