Documentation

## Define Chart Behavior by Using Actions

State and transition actions are instructions that you write inside a state or next to a transition to define how a Stateflow® chart behaves during simulation. For more information, see Model Finite State Machines.

### Example of State and Transition Actions

The actions in this chart define a state machine that empirically verifies one instance of the Collatz conjecture. For a given numeric input , the chart computes the hailstone sequence    … by iterating this rule:

• If is even, then .

• If is odd, then .

The Collatz conjecture states that every positive integer has a hailstone sequence that eventually reaches one. The chart consists of three states. At the start of simulation, the `Init` state initializes the chart data:

• The local data `n` is set to the value of the input `u`.

• The local data `n2` is set to the remainder when `n` is divided by two.

• The output data `y` is set to `false`.

Depending on the parity of the input, the chart transitions to either the `Even` or `Odd` state. As the state activity shifts between the `Even` and `Odd` states, the chart computes the numbers in the hailstone sequence. When the sequence reaches a value of one, the output data `y` becomes `true` and triggers a Stop Simulation block in the Simulink® model.

### State Action Types

State actions define what a Stateflow chart does while a state is active. The most common types of state actions are `entry`, `during`, and `exit` actions.

Type of State ActionAbbreviationDescription
`entry``en`Action occurs on a time step when the state becomes active.
`during``du`Action occurs on a time step when the state is already active and the chart does not transition out of the state.
`exit``ex`Action occurs on a time step when the chart transitions out of the state.

You can specify the type of a state action by its complete keyword (`entry`, `during`, `exit`) or by its abbreviation (`en`, `du`, `ex`). You can also combine state action types by using commas. For instance, an action with the combined type `entry,during` occurs on the time step when the state becomes active and on every subsequent time step while the state remains active.

This table lists the result of each state action in the hailstone chart.

StateActionResult
`Init`

```entry: n2 = rem(n,2); y = false;```

When `Init` becomes active at the start of the simulation, determines the parity of `n` and sets `y` to `false`.

```exit: y = isequal(n,1);```

When transitioning out of `Init` after one time step, determines whether `n` is equal to one.
`Even`

```entry,during: n = n/2; n2 = rem(n,2);```

Computes the next number of the hailstone sequence (`n` / 2) and updates its parity on:

• The time step when `Even` first becomes active.

• Every subsequent time step that `Even` is active.

`Odd`

```entry,during: n = 3*(n-y)+1; n2 = rem(n,2);```

Computes the next number of the hailstone sequence (3`n`+1) and updates its parity on:

• The time step when `Odd` first becomes active.

• Every subsequent time step that `Odd` is active.

Throughout most of the simulation, `y` evaluates to zero. On the last time step, when `n` = 1, `y` evaluates to one so this action does not modify `n` or `n2` before the simulation stops.

### Transition Action Types

Transition actions define what a Stateflow chart does when a transition leads away from an active state. The most common types of transition actions are conditions and conditional actions. To specify transition actions, use a label with this syntax:

`[condition]{conditional_action}`
`condition` is a Boolean expression that determines whether the transition occurs. If you do not specify a condition, an implied condition evaluating to true is assumed.

`conditional_action` is an instruction that executes when the condition guarding the transition is true. The conditional action takes place after the condition but before any `exit` or `entry` state actions.

This table lists the result of each transition action in the hailstone chart.

TransitionActionAction TypeResult
Default transition into `Init`

`n = u`

Conditional actionAt the start of the simulation, assigns the input value `u` to the local data `n`.
Transition from `Init` to `Even`

`n2 == 0`

ConditionWhen `n` is even, transition occurs. The number 1 at the source of this transition indicates that it is evaluated before the transition to `Odd`.
Transition from `Init` to `Odd` NoneWhen `n` is odd, transition occurs. The number 2 at the source of this transition indicates that it is evaluated after the transition to `Even`.
Transition from `Odd` to `Even`

`n2 == 0`

ConditionWhen `n` is even, transition occurs.
Transition from `Even` to `Odd`

`n2 ~= 0`

ConditionWhen `n` is odd, transition occurs.

`y = isequal(n,1)`

Conditional actionWhen transition occurs, determines whether `n` is equal to one.

### Examine Chart Behavior

Suppose that you want to compute the hailstone sequence starting with a value of nine.

1. In the Model Configuration Parameters dialog box, under Solver, select these options:

• Start time: `0.0`

• Stop time: `inf`

• Type: `Fixed-step`

• Fixed-step size: `1`

2. In the Property Inspector, select the symbol `n` for logging.

3. In the Constant block, enter an input of `u` = 9.

4. Click .

The chart responds with these actions:

• At time t = 0, the default transition to `Init` occurs.

• The transition action sets the value of `n` to 9.

• The `Init` state becomes active.

• The `entry` actions in `Init` set `n2` to 1 and `y` to `false`.

• At time t = 1, the condition `n2 == 0` is false so the chart prepares to transition to `Odd`.

• The `exit` action in `Init` sets `y` to `false`.

• The `Init` state becomes inactive.

• The `Odd` state becomes active.

• The `entry` actions in `Odd` set `n` to 28 and `n2` to 0.

• At time t = 2, the condition `n2 == 0` is true so the chart prepares to transition to `Even`.

• The `Odd` state becomes inactive.

• The `Even` state becomes active.

• The `entry` actions in `Even` set `n` to 14 and `n2` to 0.

• At time t = 3, the condition `n2 ~= 0` is false so the chart does not take a transition.

• The `Even` state remains active.

• The `during` actions in `Even` set `n` to 7 and `n2` to 1.

• At time t = 4, the condition `n2 ~= 0` is true so the chart prepares to transition to `Odd`.

• The transition action sets `y` to `false`.

• The `Even` state becomes inactive.

• The `Odd` state becomes active.

• The `entry` actions in `Odd` set `n` to 22 and `n2` to 0.

• The chart continues to compute the hailstone sequence until it arrives at a value of `n` = 1 at time t = 19.

• At time t = 20, the chart prepares to transition from `Even` to `Odd`.

• Before the `Even` state becomes inactive, the transition action sets `y` to `true`.

• The `Odd` state becomes active.

• The `entry` actions in `Odd` do not modify `n` or `n2`.

• The Stop Simulation block connected to the output signal `y` stops the simulation.  