I created my own class to act as an enumerated type. I redefined the disp () method, so when a variable containing this type is displayed in the command window, it shows something meaningful (in particular, the name of this enumerated value.)
classdef MyEnumeratedType properties(Constant) ENUMVAL1 = MyEnumeratedType(1, 'ENUMVAl1'); ENUMVAL2 = MyEnumeratedType(2, 'ENUMVAL2'); ENUMVAL3 = MyEnumeratedType(3, 'ENUMVAL3'); end properties(Access=private) ordinal name end methods(Access=private) function this = MyEnumeratedType(ord, name) this.ordinal = ord; this.name = name; end end methods function disp( this ) disp(this.name); end end end
Therefore, when I assign it to a variable in the command window, I get the desired result:
>> x = MyEnumeratedType.ENUMVAL2 x = ENUMVAL2
So far so good. BUT, when I assign a value of type MyEnumeratedType to the structure field, displaying this structure does not display the value, but only tells me that I have a value of type MyEnumeratedType.
>> mystruct.field1 = 42 mystruct = field1: 42 >> mystruct.field2 = MyEnumeratedType.ENUMVAL3 mystruct = field1: 42 field2: [1x1 MyEnumeratedType]
How do I get the value of field2 so that it displays the same way as for a double value in field1?
Clark source share