필터 지우기
필터 지우기

how to create an enumeration class of strings ??

조회 수: 24 (최근 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 CenterFile Exchange에서 String에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by