Documentation

For Each Subsystem

Subsystem that repeats execution on each element or subarray of input signal and concatenates results

  • Library:
  • Ports & Subsystems

Description

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.

The set of blocks within the subsystem represents the algorithm applied to a single element or subarray of the original signal. The For Each block inside the subsystem allows you to configure the decomposition of the subsystem inputs into elements or subarrays, and to configure the concatenation of the individual results into output signals.

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 and then processing each element using its respective copy of the subsystem.

An additional benefit of the For Each Subsystem block is that, for certain models, it 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. For 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.

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 userdata of the S-function.

  • Every S-function execution method from mdlStart up to mdlTerminate is called once for each element processed by the S-function, when it is in a For Each Subsystem block. 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.

Limitations

The For Each Subsystem block has these limitations, and these are the workarounds.

LimitationWorkaround

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 Acclerator 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 inside the subsystem

    • 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

    • Model Reference block 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:

    • Test-pointed signals or signals with an external storage class inside the system

    • Frame signals on subsystem input and output boundaries

    • Variable-size signals

    • Function-call signals crossing the boundaries of the subsystem

  • 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, perform these steps:

    • 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 tthese 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

Ports

Input

expand all

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 get signals from the local environment.

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

Output

expand all

Placing an Outport block in a subsystem block adds an output port from the block. The port label on the subsystem block is the name of the Outport block.

Use Outport blocks to send signals to the local environment.

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

Introduced in R2010a

Was this topic helpful?