フィルターのクリア

get list of subclasses

17 ビュー (過去 30 日間)
Jonas Reber
Jonas Reber 2011 年 4 月 12 日
コメント済み: Robin 2023 年 7 月 10 日
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!

採用された回答

Jonas Reber
Jonas Reber 2011 年 4 月 12 日
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 件のコメント
Robin
Robin 2023 年 7 月 10 日
Nice solution!
Be aware though that this will not work if classes are defined in a package folder (starting with +).

サインインしてコメントする。

その他の回答 (1 件)

Yannick T
Yannick T 2011 年 4 月 20 日
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 件のコメント
Yannick T
Yannick T 2011 年 4 月 20 日
Well, yes. But not in the conditional, which was the essential part for me :)
Jonas Reber
Jonas Reber 2011 年 5 月 3 日
I like that, thanks!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeAssembly についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by