Stack
Store inputs into LIFO register
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:
Reset (Rst)
Push
Pop
Examples
Operation of Stack Block
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
In — Input
scalar | vector | matrix
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
Push — Trigger event to push input
scalar
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
Pop — Trigger event to pop elements
scalar
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
Rst — Empty stack contents
scalar
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
Out — Output
scalar | vector | matrix
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
Empty — Empty stack indicator port
scalar
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
Full — Full stack indicator port
scalar
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
Num — Number of stack entries port
scalar
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
Stack depth — Number of entries stack can hold
8
(default) | positive integer
Specify the number of entries that the LIFO register can hold as a positive integer.
Trigger type — Trigger type
Rising edge
(default) | Falling edge
| Either edge
| Non-zero sample
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 inRising edge
orFalling edge
.Non-zero sample
— Triggers execution of the block at each sample time that the trigger input is not 0.
Push full stack — Push full stack
Dynamic reallocation
(default) | Ignore
| Warning
| Error
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.
Pop empty stack — Pop empty stack
Warning
(default) | Ignore
| Error
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.
Show empty stack indicator port (Empty) — Enable empty stack indicator port
off
(default) | on
Enable the Empty output port, which is high
(1
) when the stack is empty, and low
(0
) otherwise.
Show full stack indicator port (Full) — Enable full stack indicator port
off
(default) | on
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.
Show number of stack entries port (Num) — Enable number of stack entries port
off
(default) | on
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.
Show reset port (Rst) to clear internal stack buffer — Enable reset port
off
(default) | on
Enable the Rst input port to empty the stack when the block receives the trigger specified in the Trigger type parameter.
Clear output port on reset — Clear output port on reset
off
(default) | on
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 |
|
Direct Feedthrough |
|
Multidimensional Signals |
|
Variable-Size Signals |
|
Zero-Crossing Detection |
|
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using Simulink® Coder™.
The following limitations apply:
Generated code relies on the
memcpy
ormemset
function (string.h
) under certain conditions.When you select
Dynamic reallocation
, the System target file parameter on the Code Generation pane of the Configuration Parameters dialog box must be set togrt_malloc.tlc - Generic Real-Time Target with dynamic memory allocation
.
Version History
Introduced before R2006a
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)