フィルターのクリア

Create enumeration instance by comparison to variable

2 ビュー (過去 30 日間)
AstroJon
AstroJon 2021 年 12 月 8 日
コメント済み: AstroJon 2021 年 12 月 13 日
Can an instance of an enumeration class be created by comparison to a string or character of the same name as the enumeration? Can it be done by referencing its index in the enumeration list? I want to use the enumeration class to manage the execution of a sequence of functions. Below is my enumeration class (edited for privacy):
classdef types
properties
prop1
prop2
end
enumeration
type1 (1,0)
type2 (0,1)
type3 (1,1)
end
methods
function obj = types(p1,p2)
obj.prop1 = p1;
obj.prop2 = p2;
end
end
end
A function asks the user to select either a single type or a sequence of types. Here's some of the code:
function seq = sequence(choice)
list = enumeration('types');
if choice == 0
[ind,sel] = listdlg('PromptString',"Select a type.",...
'SelectionMode','single','ListString',list);
if sel == 1
switch list(ind)
case types.type1
seq = types.type1;
case types.type2
seq = types.type2;
case types.type3
seq = types.type3;
end
else
seq = 0;
end
elseif choice == 1
% prompts to create sequence and assign enumeration member in a
% similar manner
end
end
Is there a more efficient way to assign the enumeration member to the 'seq' variable?

採用された回答

AstroJon
AstroJon 2021 年 12 月 8 日
Nevermind... This totally works:
seq = list(ind);
<sigh>
As for using the enumeration class to manage the execution of a sequence of functions, I'm still working on that and happy to take suggestions.
  1 件のコメント
AstroJon
AstroJon 2021 年 12 月 13 日
If anybody is interested, my solution to the second question (using the enumeration class to manage the execution of a sequence of functions) is:
classdef main < handle
properties
property
end
methods
function obj = main()
% Use a listdlg to prompt the user to select the method of
% defining the sequence.
obj.property = sequence()
% where sequence() is the class that is instantiated to collect
% the sequence from the user.
for m=1:length(obj.property)
exec = strcat(string(obj.property),'add additional parts of func name here if needed');
results = feval(exec);
end
end
end
end

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by