Main Content

Stack

Store inputs into LIFO register

  • Stack block

Libraries:
DSP System Toolbox / Signal Management / Buffers

Description

The Stack block stores a sequence of input samples in a last in, first out (LIFO) register. You set the register capacity in the Stack depth parameter. Inputs to the block can be scalars, vectors, or matrices.

The block pushes the input at the In port to the top of the stack when it receives a trigger event at the Push port. When it receives a trigger event at the Pop port, the block pops the top element off the stack and holds the Out port at that value. The last input that the block pushes on the stack is always the first to be popped off.

When you trigger two or more of the control input ports at the same time step, the block executes the operations in this order:

  1. Reset (Rst)

  2. Push

  3. Pop

Examples

expand all

This table shows how the Stack block operates for the following parameter values: Stack depth of 4, Trigger type of Either edge, and Clear output port on reset enabled. Because the block triggers on both rising and falling edges in this example, each transition from 1 to 0 or 0 to 1 in the Push, Pop, and Rst columns represents a distinct trigger event. A value of 1 in the Empty column indicates an empty buffer, while 1 in the Full column indicates a full buffer.

In

Push

Pop

Rst

Stack

Out

Empty

Full

Num

1

0

0

0

0

1

0

0

2

1

0

0

0

0

0

1

3

0

0

0

0

0

0

2

4

1

0

0

0

0

0

3

5

0

0

0

0

0

1

4

6

0

1

0

5

0

0

3

7

0

0

0

4

0

0

2

8

0

1

0

3

0

0

1

9

0

0

0

2

1

0

0

10

1

0

0

2

0

0

1

11

0

0

0

2

0

0

2

12

1

0

1

0

0

0

1

Note that at the last step shown, the Push and Rst ports are triggered simultaneously. The Rst trigger takes precedence, and the stack is first cleared and then pushed.

Ports

Input

expand all

Specify the input as a scalar, vector, or a matrix.

When the block receives a trigger event at the Push port, the block pushes the input at the In port onto the top of the stack.

Data Types: single | double | int8 | int16 | int32 | uint8 | uint16 | uint32 | Boolean | fixed point
Complex Number Support: Yes

Specify a trigger event at this port to push the input at the In port to the top of the stack. The last input that the block pushes to the stack is always the first to be popped off.

Inputs to this port must have the same built-in data type as inputs to the Pop and Rst input ports

Data Types: single | double | int8 | int16 | int32 | uint8 | uint16 | uint32 | Boolean

Specify a trigger event at this port to pop the top element off the stack and to hold the Out port at that value. The last input that the block pushes to the stack is always the first to be popped off.

Inputs to this port must have the same built-in data type as inputs to the Push and Rst input ports

Data Types: single | double | int8 | int16 | int32 | uint8 | uint16 | uint32 | Boolean

Specify a trigger event at this port to empty the stack contents. When you select Clear output port on reset, then a trigger event at the Rst port empties the stack and sets the value at the Out port to zero. This setting also applies when a disabled subsystem containing the Stack block is reenabled. In this case, when you select Clear output port on reset, the Out port value is only reset to zero.

Inputs to this port must have the same built-in data type as inputs to the Push and Pop input ports.

Dependencies

To enable this port, select Show reset port (Rst) to clear internal stack buffer.

Data Types: single | double | int8 | int16 | int32 | uint8 | uint16 | uint32 | Boolean

Output

expand all

Stack output, returned as a scalar, vector, or a matrix. When you specify a trigger event at the Pop port, the block pops the top element off the stack and holds the Out port at that value. The last input that the block pushes to the stack is always the first to be popped off.

Data Types: single | double | int8 | int16 | int32 | uint8 | uint16 | uint32 | Boolean | fixed point
Complex Number Support: Yes

The Empty port outputs a high (1) when the stack is empty and low (0) otherwise.

Dependencies

To enable this port, select Show empty stack indicator port (Empty).

Data Types: Boolean

The Full port outputs a high (1) when the stack is full and low (0) otherwise. The Full port remains low when you select Dynamic reallocation in the Push full stack parameter.

Dependencies

To enable this port, select Show full stack indicator port (Full).

Data Types: Boolean

The Num port outputs the number of stack entries through this port. This value indicates the number of entries currently on the stack. The block outputs a double-precision floating-point value at this port when the input at the In port is of the double-precision floating-point data type. The block outputs a 32-bit unsigned integer value at this port when the input at the In port is not a double-precision floating-point value.

Dependencies

To enable this port, select Show number of stack entries port (Num).

Data Types: double | uint32

Parameters

expand all

Specify the number of entries that the LIFO register can hold as a positive integer.

Specify the type of event that triggers the block execution. The rate of the trigger signal must be the same as the rate of the data signal input.

Specify the triggering event for the Push, Pop, and Rst ports as one of these options:

  • Rising edge — Triggers execution of the block when the trigger input does one of the following:

    • Rises from a negative value to a positive value or 0

    • Rises from 0 to a positive value, where the rise is not a continuation of a rise from a negative value to 0 (see the figure)

  • Falling edge — Triggers execution of the block when the trigger input does one of the following:

    • Falls from a positive value to a negative value or 0

    • Falls from 0 to a negative value, where the fall is not a continuation of a fall from a positive value to 0 (see the figure)

  • Either edge — Triggers execution of the block when the trigger input behaves as described in Rising edge or Falling edge.

  • Non-zero sample — Triggers execution of the block at each sample time that the trigger input is not 0.

Use this parameter to specify how the block responds to the trigger at the Push port when the register is full.

  • Dynamic reallocation –– Dynamically resize the register to accept as many additional inputs as memory permits. To find out how many elements are on the stack at a given time, enable the Num output port by selecting the Show number of stack entries port parameter.

    To select this option, you must set the System target file parameter on the Code Generation pane of the Model Configuration Parameters dialog box to grt_malloc.tlc – Generic Real-Time Target with dynamic memory allocation.

    Note

    If your model contains any referenced models that use a Stack block with the Push full stack parameter set to Dynamic reallocation, you cannot simulate your top-level model in Simulink® Accelerator mode.

  • Ignore — Ignore the trigger event and continue the simulation.

  • Warning — Ignore the trigger event but display a warning message in the MATLAB® command window.

  • Error — Display an error dialog box and terminate the simulation.

Note

The Push full stack parameter is a diagnostic parameter. Like all diagnostic parameters on the Configuration Parameters dialog box, this parameter is set to Ignore in the code generated for this block by the Simulink Coder™ code generation software.

Use this parameter to specify how the block responds to the trigger at the Pop port when the register is empty.

  • Warning — Ignore the trigger event but display a warning message in the MATLAB command window.

  • Ignore — Ignore the trigger event and continue the simulation.

  • Error — Display an error dialog box and terminate the simulation.

Note

The Pop empty stack parameter is a diagnostic parameter. Like all diagnostic parameters on the Configuration Parameters dialog box, this parameter is set to Ignore in the code generated for this block by the Simulink Coder code generation software.

Enable the Empty output port, which is high (1) when the stack is empty, and low (0) otherwise.

Enable the Full output port, which is high (1) when the stack is full, and low (0) otherwise. The Full port remains low when you select Dynamic reallocation in the Push full stack parameter.

Enable the Num output port, which tracks the number of entries currently on the stack. When inputs to the In port are double-precision values, the outputs from the Num port are also double-precision values. Otherwise, the outputs from the Num port are 32-bit unsigned integer values.

Enable the Rst input port to empty the stack when the block receives the trigger specified in the Trigger type parameter.

Reset the Out port to 0 (in addition to clearing the stack) when the block receives a trigger at the Rst input port.

Block Characteristics

Data Types

Boolean | double | fixed point | integer | single

Direct Feedthrough

no

Multidimensional Signals

no

Variable-Size Signals

no

Zero-Crossing Detection

no

Extended Capabilities

Version History

Introduced before R2006a

See Also

Blocks