how to create an enumeration class of strings ??

34 ビュー (過去 30 日間)
Owen Zhang
Owen Zhang 2018 年 12 月 6 日
回答済み: Brad Carman 2020 年 3 月 12 日
Hi,
I'd like to create some enumeration classes to replace the hard-coded string values. But I met the problem occurs in the attached picture.
It's OK to derive from int , but fails to derive from char or string.
Anyone has some idea ? thank you in advance.
(I need the string values , to guarantee the code transition.)
  1 件のコメント
Greg
Greg 2018 年 12 月 6 日
Why not just use a protected categorical array?

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

採用された回答

Owen Zhang
Owen Zhang 2018 年 12 月 7 日
i used the following approaches of using class constants.
and to replace hardcoded from:
if strcmp(somevar, 'PDOS')
to
if strcmp(somevar, LossModels.PDOS)
Untitled.png
but still , it's curious why enumeration class of strings is not supported in matlab.

その他の回答 (1 件)

Brad Carman
Brad Carman 2020 年 3 月 12 日
It's also possible to so simply convert the enumeration name to a char
classdef SolverType
enumeration
NE_BACKWARD_EULER_ADVANCER
NE_PARTITIONING_ADVANCER
end
end
Then to get char:
char(SolverType.NE_BACKWARD_EULER_ADVANCER)
Which gives
ans =
'NE_BACKWARD_EULER_ADVANCER'

カテゴリ

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

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by