matlab.lang.OnOffSwitchState - Represent on and off states with logical values - MATLAB (original) (raw)

Main Content

Namespace: 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:

Class Logical True Logical 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

off Logical false
on Logical 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. UseOnOffSwitchState 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 theOnOffSwitchState enumeration.

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

More About

expand all

The OnOffSwitchState class enables you to form text expressions by concatenating enumeration members with character vectors or strings. For example, if an object property named Power contains an enumeration member of the OnOffSwitchState class, you can form a character array with a character vector and the property value:

a = SystemState; a.Power = true; ['The power switch is currently ' a.Power]

ans =

'The power switch is currently on'

Normally, when forming an array by concatenating character vectors or strings with enumeration members, MATLABĀ® attempts to convert the text to the class of the enumeration member. However, the OnOffSwitchState class defines specialized behavior that enables concatenation of enumeration members and text in cases where the text does not correspond to enumeration members defined by the class. In these cases, MATLAB creates an array of the same type as the text.

Here are the rules that MATLAB applies when concatenating character vectors or strings with enumeration members of the OnOffSwitchState class.

The OnOffSwitchState class supports the eq (==) andisequal functions for testing equality between enumeration members and char, string, logical, or numeric types having values of 1 or 0.

Version History

Introduced in R2017a

expand all

Before R2024a, negating a matlab.lang.OnOffSwitchState enumeration member returned a logical value.

a = matlab.lang.OnOffSwitchState.on

a =

OnOffSwitchState enumeration

on

MATLAB now preserves the class of matlab.lang.OnOffSwitchState members when you negate them. In the example, the value of b in R2024a ismatlab.lang.OnOffSwitchState.off.

a =

OnOffSwitchState enumeration

off