Main Content

Use C Function Block Within For Each Subsystem

This example shows how to use a C Function block within a For Each subsystem. The example passes a vector signal into the For Each subsystem, which repeats execution of the C Function block on each element of the input signal and concatenates the results. The C Function block calls the custom code function customSaturateFcn(), which implements a saturation algorithm and outputs a clipped version of the input signal. A C Caller block with the same settings can also be used in place of a C Function block in a For Each subsystem.

This example illustrates the following concepts:

  • Calling a C function from a Simulink® block inside a For Each subsystem.

  • Configuring deterministic functions in custom code.

Examine the Model

This model calls custom code from a For Each subsystem. When the model runs, the scope displays the output.

open_system('slexCFunctionForEach');

Identify Custom Code Deterministic Functions

Open the Configuration Parameters dialog box.

In the Simulation Target pane, set the value of Deterministic functions to All. This setting tells the model that all custom code functions have deterministic behavior, that is, the same input values to the function always give the same outputs. If a custom code function is specified as deterministic, it can be safely called from a C Function or C Caller block inside a For Each subsystem. If not all custom code functions are deterministic, specific functions can be set to be deterministic with the By Function option. If a For Each subsystem contains a C Function or C Caller block that calls a custom code function that is not specified as deterministic, Simulink produces an error.

configset.highlightParameter(bdroot,'DefaultCustomCodeDeterministicFunctions');
set_param(bdroot,'DefaultCustomCodeDeterministicFunctions','All');

Simulate the Model

Simulate the model and view the results.

sim(bdroot);
open_system('slexCFunctionForEach/Scope');

See Also

| | | |