How to get enumeration name in MATLAB

I define an enumerated type in MATLAB

classdef(Enumeration) Color < Simulink.IntEnumType enumeration RED(0), GREEN(1), BLUE(2), end end 

I can assign it:

 >> x = Color.RED x = RED 

I can display it like this:

 >> disp(x) RED 

or how is it

 >> x.display() x = RED 

How to access this name ("RED") as a string?

In other words, I'm looking for something like:

 s = x.toString() 

or

 s = tostring(x) 

both of them do not work.

+7
enums oop matlab
source share
1 answer

You can use:

 Β» str = char(Color.RED) str = RED Β» class(str) ans = char 

You can even override the default behavior:

 classdef(Enumeration) Color < int32 enumeration RED(0) GREEN(1) BLUE(2) end methods function s = char(obj) s = ['Color ' num2str(obj)]; %# or use a switch statement.. end function disp(obj) disp( char(obj) ) end end end 

and now:

 Β» char(Color.BLUE) ans = Color 2 
+8
source share

All Articles