get list of subclasses

14 visualizaciones (últimos 30 días)
Jonas Reber
Jonas Reber el 12 de Abr. de 2011
Comentada: Robin el 10 de Jul. de 2023
Hello Matlabers
I have a class "Filter" and some subclasses "xxFilter, "yyFilter", etc. They have a const property called "Name". I would now like to implement a funcation that gives me a list (Name) of all available classes that are subclasses of "Filter".
what I have so far:
flist = getAllFiles('myFilterFolder');
for i=1:numel(flist)
[~,filename,fileext,~]=fileparts(flist{i}); % get those names
if (exist(filename,'class') == 8) && (strcmp(fileext,'.m'))
% -----------------------------------------------------------
% here I would like to check if the class with name filename a class
% of type "Filter", if it would be an object, I would use
% isa(filename,'Filter'). But how do I achieve this with a string?
% -----------------------------------------------------------
% then I could add the name (class with name filename).Name to my
% string list
else
flist{i}=[]; % empty the cell
end
end
% cleanup empty cells
flist(cellfun(@isempty,flist)) = [];
As pointed out above I would like to check If there exists a class with name "filename" that is of type "Filter" just as I would do with "isa(.., 'Filter')".
I therefore need a function that allows me to access the class properties of tha class with a name given as a string (here filename)
Thank you so much!

Respuesta aceptada

Jonas Reber
Jonas Reber el 12 de Abr. de 2011
I figured out how to resolve that problem when I realized the power of "eval()".
For anyone interested in doing something similar, here's the code I have now:
% list of all folders in FilterFunctions:
flist = getAllFiles('FilterFunctions');
% prepare result
filterlist = struct('Name',{},'Classname',{});
for i=1:numel(flist)
[~,filename,fileext]=fileparts(flist{i}); % get just the filename
if (exist(filename,'class') == 8) && (strcmp(fileext,'.m'))
try
if(isa(eval(filename),'Filter'))
filterlist(end+1) = struct( ...
'Name', eval([filename '.Name']), ...
'Classname',filename ...
);
end
catch % no problem - its eval
end
end
end
  1 comentario
Robin
Robin el 10 de Jul. de 2023
Nice solution!
Be aware though that this will not work if classes are defined in a package folder (starting with +).

Iniciar sesión para comentar.

Más respuestas (1)

Yannick T
Yannick T el 20 de Abr. de 2011
Hello Jonas,
I was trying to do something similar and your solution works fine. You could also do it in a slightly different way which doesn't require the use of eval():
if exist(filename, 'class') && ismember('Filter', superclasses(filename))
filterlist(end+1) = struct( ...
'Name', eval([filename '.Name']), ...
'Classname',filename ...
);
end
Yannick
  3 comentarios
Yannick T
Yannick T el 20 de Abr. de 2011
Well, yes. But not in the conditional, which was the essential part for me :)
Jonas Reber
Jonas Reber el 3 de Mayo de 2011
I like that, thanks!

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by