AddState

AddState()

Description

Adds a state to an existing state matrix.

Syntax

NewMatrix = AddState(ExistingMatrix, 'Name', StateName, 'Timer', TimerDuration, 'StateChangeConditions', Conditions, 'OutputActions', Actions)

Parameters

  • ExistingMatrix: The state matrix you are adding to. If this is the first state, ExistingMatrix is the output of NewStateMatrix().

  • StateName: A character string containing the unique name of the state.

    • The state will automatically be assigned a number for internal use and state synchronization via the sync port.

  • Timer: The state timer value, given in seconds

    • This value must be zero or positive, and can range between 0-3600s.

    • If set to 0s and linked to a state transition (see next bullet), the state will still take ~100us to execute the state's output actions before the transition completes.

  • StateChangeConditions: A cell array of strings listing pairs of input events and the state changes they trigger.

    • Each odd cell should contain the name of a valid input event.

    • Each even cell should contain the name of the new state to enter if the previously listed event occurs, or 'exit' to exit the matrix and return all captured data..

  • OutputActions: A cell array listing the output actions and corresponding values for the current state.

    • Each odd cell should contain the name of a valid output action.

    • Each even cell should contain the value of the previously listed output action (see output actions for valid values).

Returns

  • A state matrix struct, updated with the new state.

Example

% 1. This code generates a simple state matrix that drives BNC output channel 1 to 5V (high) for 1 second before exiting.

sma = NewStateMatrix();

sma = AddState(sma, 'Name', 'MyState', ...

'Timer', 1,...

'StateChangeConditions', {'Tup', 'exit'},...

'OutputActions', {'BNCState', 1});

% 2. This code generates a simple state matrix that flashes the port LEDs of ports 1-3 for 0.1 second each (assuming an LED is connected to the port's PWM line).

sma = NewStateMatrix();

sma = AddState(sma, 'Name', 'LightPort1', ...

'Timer', 0.1,...

'StateChangeConditions', {'Tup', 'LightPort2'},...

'OutputActions', {'PWM1', 255});

sma = AddState(sma, 'Name', 'LightPort2', ...

'Timer', 0.1,...

'StateChangeConditions', {'Tup', 'LightPort3'},...

'OutputActions', {'PWM2', 255});

sma = AddState(sma, 'Name', 'LightPort3', ...

'Timer', 0.1,...

'StateChangeConditions', {'Tup', 'exit'},...

'OutputActions', {'PWM3', 255});