Main Content

For Each Subsystem

Subsystem that repeats execution on each element or subarray of input signals or mask parameters and concatenates results

  • Library:
  • Simulink / Ports & Subsystems

    HDL Coder / Ports & Subsystems

  • For Each Subsystem block


The For Each Subsystem block is a Subsystem block preconfigured as a starting point for creating a subsystem that repeats execution during a simulation time step on each element or subarray of an input signal or mask parameter array.

The set of blocks within the subsystem represents the algorithm applied to a single element or subarray of the original signal or mask parameter array. The For Each block inside the subsystem allows you to configure the decomposition of the subsystem inputs or mask parameters into elements or subarrays, and to configure the concatenation of the individual results into output signals. The block parameters Partition Dimension and Partition Width specify the dimension through which to slice the input signal or mask parameter array and the width of each slice, respectively. To partition a row vector, specify the Partition Dimension as 2. To partition a column vector, specify the Partition Dimension as 1. Use the parameter Partition Offset to specify a gap or an overlap between partitions. Specify a Number of iterations to limit processing to a subset of the data. To learn more about the block parameters, see For Each.

Inside this subsystem, each block that has states maintains separate sets of states for each element or subarray that it processes. Consequently, the operation of this subsystem is similar in behavior to copying the contents of the subsystem for each element in the original input signal or mask parameter array and then processing each element using its respective copy of the subsystem.

For certain models, the For Each Subsystem block also improves the code reuse in Simulink® Coder™ generated code. Consider a model containing two reusable Atomic Subsystem blocks with the same scalar algorithm applied to each element of the signal. If the input signal dimensions of these subsystems are different, Simulink Coder generated code includes two distinct functions. You can replace these two subsystems with two identical For Each Subsystem blocks that are configured to process each element of their respective inputs using the same algorithm. In this case, Simulink Coder generated code consists of a single function parameterized by the number of input signal elements. This function is invoked twice, once for each unique instance of the For Each Subsystem block in the model. For each of these cases, the input signal elements have different values.

Multicore Execution Support

When you simulate your model in Rapid Accelerator mode, Simulink uses multicore execution for faster simulation of For Each subsystems. Simulink automatically profiles each eligible For Each subsystem the first two time steps it runs in Rapid Accelerator mode to compare parallel and serial execution times. Simulink then designates the For Each subsystem for parallel, multicore execution in subsequent time steps of the simulation run if doing so would speed up execution time. For nested For Each subsystems, multicore execution applies only to the top-level subsystem. Multicore execution does not apply to For Each subsystems containing continuous states or Function Caller blocks.

To suppress multicore execution for a given For Each subsystem, set the MultithreadedSim parameter of the For Each block within the subsystem (not the For Each Subsystem block itself) to 'off'.


To suppress multicore execution for all For Each subsystems in a model, set the model configuration parameter MultithreadedSim to 'off'.


To re-enable multicore execution, set the relevant MultithreadedSim parameter back to its default value of 'auto'.

For an example, see Multithreaded Simulation using For Each Subsystem.

S-Function Support

The For Each Subsystem block supports both C-MEX S-functions and Level-2 MATLAB® S-functions, provided that the S-function supports multiple execution instances using one of the following techniques:

  • A C-MEX S-function must declare ssSupportsMultipleExecInstances(S, true) in the mdlSetWorkWidths method.

  • A Level-2 MATLAB S-function must declare block.SupportsMultipleExecInstances = true in the setup method.

If you use the above specifications:

  • Do not cache run-time data such as DWork and Block I/O using global or persistent variables or within the user data of the S-function.

  • In a For Each Subsystem block, every S-function execution method from mdlStart up to mdlTerminate is called once for each element processed by the S-function. Consequently, you need to be careful not to free the same memory on repeated calls to mdlTerminate. For example, consider a C-MEX S-function that allocates memory for a run-time parameter within mdlSetWorkWidths. The memory only needs to be freed once in mdlTerminate. As a solution, set the pointer to be empty after the first call to mdlTerminate.


The For Each Subsystem block has the following limitations and workarounds.


You cannot log bus or an array of bus signals directly in the For Each subsystem.

Use one of these approaches:

  • Use a Bus Selector block to select the signals you want to log and mark those signals for signal logging.

  • Attach the signal to an Outport block and log the signal outside the For Each subsystem.

You cannot log a signal inside a referenced model that is inside a For Each subsystem if either of these conditions exists:

  • The For Each subsystem is in a model simulating in Rapid Accelerator mode.

  • The For Each subsystem itself is in a model referenced by a Model block in Accelerator mode.

For the first condition, use Accelerator mode.

For the second condition, use Normal or Rapid Accelerator mode.

You cannot log the states of the blocks in a For Each subsystem.

Save and restore the simulation state.

You cannot use Normal mode to simulate a Model block inside a For Each subsystem.

Use Accelerator or Rapid Accelerator mode.

Reusable code is generated for two For Each Subsystems with identical contents if their input and output signals are vectors (1-D or 2-D row or column vector). For n-D input and output signals, reusable code is generated only when the dimension along which the signal is partitioned is the highest dimension.

Permute the signal dimensions to transform the partition dimension and the concatenation dimension to the highest nonsingleton dimension for n-D signals.

The For Each Subsystem block does not support these features:

  • You cannot include these blocks or S-functions inside a For Each Subsystem:

    • Data Store Memory, Data Store Read, or Data Store Write blocks

    • The From Workspace block if the input is a Structure with Time that has an empty time field

    • The To Workspace and To File data saving blocks

    • Goto and From blocks that cross the subsystem boundary

    • Referenced model with simulation mode set to Normal

    • Shadow Inports

    • ERT S-functions

    For a complete list of the blocks that support the For Each Subsystem, type showblockdatatypetable at the MATLAB command line.

  • You cannot use these types of signals:

    • Signals with an external storage class inside the system

    • Frame signals on subsystem input and output boundaries

    • Variable-size signals

  • Creation of a linearization point inside the subsystem

  • Propagating the Jacobian flag for the blocks inside the subsystem. You can check this condition in MATLAB using J.Mi.BlockAnalyticFlags.jacobian, where J is the Jacobian object. To verify the correctness of the Jacobian of the For Each Subsystem block:

    • Look at the tag of the For Each Subsystem Jacobian. If it is “not_supported”, then the Jacobian is incorrect.

    • Move each block out of the For Each Subsystem and calculate its Jacobian. If any block is " not_supported " or has a warning tag, the For Each Subsystem Jacobian is incorrect.

  • You cannot perform these types of code generation:

    • Generation of a Simulink Coder S-function target

    • Simulink Coder code generation under both of the following conditions:

      • A Stateflow® or MATLAB Function block resides in the subsystem.

      • This block tries to access global data outside the subsystem, such as Data Store Memory blocks or Simulink.Signal objects of ExportedGlobal storage class.

    • PLC code generation



expand all

Signal input to a Subsystem block, specified as a scalar, vector, or matrix. Placing an Inport block in a Subsystem block adds an external input port to the block. The port label matches the name of the Inport block.

Use Inport blocks to receive signals from the local environment.

Data Types: half | single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | Boolean | fixed point | enumerated | bus


expand all

Signal output from a Subsystem block, returned as a scalar, vector, or matrix. Placing an Outport block in a Subsystem block adds an external output port to the block. The port label matches the name of the Outport block.

Use Outport blocks to send signals to the local environment.

Data Types: half | single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | Boolean | fixed point | enumerated | bus

Model Examples

Block Characteristics

Data Types

Boolean[a] | bus[a] | double[a] | enumerated[a] | fixed point[a] | half[a] | integer[a] | single[a]

Direct Feedthrough


Multidimensional Signals


Variable-Size Signals


Zero-Crossing Detection


[a] Actual data type or capability support depends on block implementation.

Extended Capabilities

Introduced in R2010a