Reputation: 203
I would like to define a data type in Matlab that can be 1 of 3 possible values. If I were doing this in C I would do the following:
typedef enum
{
TYPE1,
TYPE2,
TYPE3,
} new_type_t;
new_type_t variable = TYPE1;
How can I achieve something similar in Matlab?
Ideally I want to have a Matlab class with a property that is restricted to some enumerated type.
My attempt at the Matlab code:
classdef Node
classdef BoundaryTypes
enumeration
adiabatic
convective
conductive
end
end
properties
k
c_p
rho
BC %type of boundary condition
end
end
Upvotes: 0
Views: 377
Reputation: 24147
You would start by defining an enumeration class for your boundary types:
classdef BoundaryConditionType
enumeration
adiabatic
convective
conductive
end
end
Then you would construct your node class:
classdef Node
properties
k
c_p
rho
BC
end
end
If you wish to restrict the class of BC
, there are two ways. The first will only work in R2016a onwards:
classdef Node
properties
k
c_p
rho
BC BoundaryType
end
end
If you're using an older version, you can implement a set
method that will restrict the class of the property:
classdef Node
properties
k
c_p
rho
BC
end
methods
function obj = set.BC(obj, val)
assert(isa(val,'BoundaryConditionType'))
obj.BC = val;
end
end
end
Upvotes: 1
Reputation: 21561
As explained in the documentation, you can define an enumerator class.
classdef WeekDays
enumeration
Monday, Tuesday, Wednesday, Thursday, Friday
end
end
Upvotes: 2