Main Content

matlab.lang.OnOffSwitchState class

Package: matlab.lang

Represent on and off states with logical values

Description

matlab.lang.OnOffSwitchState is an enumeration class that derives from the logical class. Use this class to specify the data type of properties that accept values of 'on' and 'off' and logical true, false, 1, or 0.

Use this class to constrain property values to any of these values:

ClassLogical TrueLogical False

Character vector

'on'

'off'

String scalar

"on"

"off"

Logical

true

false

Logical and numeric

1

0

Enumeration member

matlab.lang.OnOffSwitchState.on

matlab.lang.OnOffSwitchState.off

Because OnOffSwitchState derives from the logical class, you can use these enumeration members in logical expressions.

Enumeration Members

offLogical false
onLogical true

Examples

collapse all

Create a class to represent the state of a computer whose power and monitor can be turned on and off separately. Use OnOffSwitchState to define the class of these properties.

classdef SystemState
   properties
      Power matlab.lang.OnOffSwitchState = 'off'
      Monitor matlab.lang.OnOffSwitchState = 'off'
   end
   methods
      function val = isOn(obj)
         if ~(obj.Power && obj.Monitor)
            val = matlab.lang.OnOffSwitchState.off;
         else
            val = matlab.lang.OnOffSwitchState.on;
         end
      end
   end
end

Create a SystemState object and set the property values to 'on'.

a = SystemState;
a.Power = 'on';
a.Monitor = 'on';

Call the isOn method to determine the state of the system. The method returns a logical value provided by the OnOffSwitchState enumeration.

if isOn(a)
   ...% System is ready to use
end

More About

expand all

Version History

Introduced in R2017a