Quantcast

Documentation Center

  • Trial Software
  • Product Updates

Include Enumerated Data in Control Flow Statements

The following control statements work with enumerated operands in generated code. However, there are restrictions (see Restrictions on Use of Enumerated Data in for-Loops).

if Statement with Enumerated Data Types

This example is based on the definition of the enumeration types LEDcolor and sysMode. The function displayState uses these enumerated data types to activate an LED display.

Class Definition: sysMode

classdef(Enumeration) sysMode < int32
  enumeration
    OFF(0)
    ON(1)
  end
end

This definition must reside on the MATLAB® path in a file with the same name as the class, sysMode.m.

Class Definition: LEDcolor

classdef(Enumeration) LEDcolor < int32
    enumeration
        GREEN(1),
        RED(2),
    end
end

This definition must reside on the MATLAB path in a file called LEDcolor.m.

MATLAB Function: displayState

This function uses enumerated data to activate an LED display, based on the state of a device. It lights a green LED display to indicate the ON state and lights a red LED display to indicate the OFF state.

function led = displayState(state)
%#codegen

if state == sysMode.ON
    led = LEDcolor.GREEN;
else
    led = LEDcolor.RED;
end

Build and Test a MEX Function for displayState

  1. Generate a MEX function for displayState. Use the -args option to pass one of the allowable values for the enumerated data input as a sample value.

    codegen displayState -args {sysMode.ON}
  2. Test the function. For example,

    displayState(sysMode.OFF)

    You should get the following result:

    ans = 
    
        RED

switch Statement with Enumerated Data Types

This example is based on the definition of the enumeration types VCRState and VCRButton. The function VCR uses these enumerated data types to set the state of the VCR.

Class Definition: VCRState

classdef(Enumeration) VCRState < int32
    enumeration
        Stop(0),
        Pause(1),
        Play(2),
        Forward(3),
        Rewind(4)
    end
end

This definition must reside on the MATLAB path in a file with the same name as the class, VCRState.m.

Class Definition: VCRButton

classdef(Enumeration) VCRButton < int32
    enumeration
        Stop(1),
        PlayOrPause(2),
        Next(3),
        Previous(4)
    end
end

This definition must reside on the MATLAB path in a file with the same name as the class, VCRButton.m.

MATLAB Function: VCR

This function uses enumerated data to set the state of a VCR, based on the initial state of the VCR and the state of the VCR button.

function s = VCR(button) 
%#codegen

persistent state

if isempty(state)
    state = VCRState.Stop;
end

switch state
    case {VCRState.Stop, VCRState.Forward, VCRState.Rewind}
        state = handleDefault(button);
    case VCRState.Play
        switch button
            case VCRButton.PlayOrPause, state = VCRState.Pause;
            otherwise, state = handleDefault(button);
        end
    case VCRState.Pause
        switch button
            case VCRButton.PlayOrPause, state = VCRState.Play;
            otherwise, state = handleDefault(button);
        end
end
s = state;
 
function state = handleDefault(button)
switch button
    case VCRButton.Stop, state = VCRState.Stop;
    case VCRButton.PlayOrPause, state = VCRState.Play;
    case VCRButton.Next, state = VCRState.Forward;
    case VCRButton.Previous, state = VCRState.Rewind;
    otherwise, state = VCRState.Stop;
end

Build and Test a MEX Function for VCR

  1. Generate a MEX function for VCR. Use the -args option to pass one of the allowable values for the enumerated data input as a sample value.

    codegen -args {VCRButton.Stop}  VCR
  2. Test the function. For example,

    s = VCR(VCRButton.Stop)

    You should get the following result:

    s = 
    
        Stop   

while Statement with Enumerated Data Types

This example is based on the definition of the enumeration type State. The function Setup uses this enumerated data type to set the state of a device.

Class Definition: State

classdef(Enumeration) State < int32
    enumeration
        Standby(0),
        Boot(1),
        Ready(2)
     end
end

This definition must reside on the MATLAB path in a file with the same name as the class, State.m.

MATLAB Function: Setup

The following function Setup uses enumerated data to set the state of a device.

function s = Setup(initState)
%#codegen

state = initState;

if isempty(state)
    state = State.Standby;
end

while state ~= State.Ready
    switch state
        case State.Standby
            initialize();
            state = State.Boot;
        case State.Boot
            boot();
            state = State.Ready;
    end
end
s = state;

function initialize()
% Perform initialization.

function boot()
% Boot the device.

Build and Test a MEX Executable for Setup

  1. Generate a MEX executable for Setup. Use the -args option to pass one of the allowable values for the enumerated data input as a sample value.

    codegen Setup -args {State.Standby}
  2. Test the function. For example,

    s = Setup(State.Standby)

    You should get the following result:

    s = 
    
        Ready   
Was this topic helpful?