Generate S-Function from Subsystem
The Generate S-function menu option automates generating an S-function from a subsystem. The Generate S-function menu option presents parameters used within the subsystem and enables you to declare selected parameters as tunable.
For creating S-Function blocks from a subsystem, see Create S-Function Blocks from a Subsystem.
For example, consider
SourceSubsys. The objective is to
SourceSubsys from the model and build an
S-Function block from it. The workspace variable
K, which is the gain
factor of the Gain block within
SourceSubsys, is declared and
generated as a tunable variable.
To autogenerate an S-function from
SourceSubsys that has tunable
SourceSubsysmodel open, click the subsystem to select it.
Right-click the subsystem and select C/C++ Code > Generate S-Function.
In the Generate S-Function window you see variables or data objects that are referenced as block parameters in the subsystem. You can declare them as tunable.
The upper pane of the window displays these columns:
Variable Name: Name of the parameter.
Class: If the parameter is a workspace variable, its data type is displayed. If the parameter is a data object, its name and class is displayed.
Tunable: Lets you select tunable parameters. To declare a parameter tunable, select the check box. In this example, the parameter
Kis declared tunable.
When you select a parameter in the upper pane, the lower pane shows the blocks that reference the parameter, and the parent system of each block.
After selecting tunable parameters, click the Build button. The build process initiates code generation and compilation of the S-function by using the S-function target. The Create New Model option is automatically enabled.
The build process displays status messages in the MATLAB® Command Window. When the build is complete, the tunable parameters window closes and a new untitled model window opens.
The model window contains an S-Function block with the same name as the subsystem from which the block was generated (in this example,
SourceSubsys). Optionally, you can save the generated model containing the generated block.
The generated code for the S-Function block is stored in the current working folder. These files are written to the top-level folder:
is the subsystem name (for example,
is a platform-dependent MEX-file extension (for example,
The source code for the S-function is written to the subfolder
. The top level
.cppfile is a stub file that contains an include directive that you can use to interface other C/C++ code to the generated code.
The generated S-Function block has inports and outports whose widths and sample times correspond to those of the original model.
The following code, from the
mdlOutputs routine of the generated
S-function code (in
SourceSubsys_sf.c) shows how the tunable variable
K is referenced by using calls to the MEX API.
static void mdlOutputs(SimStruct *S, int_T tid)... /* Gain: '<S1>/Gain' incorporates: * Sum: '<S1>/Sum' */ rtb_Gain_n = (rtb_Product_p + (*(((const real_T**)ssGetInputPortSignalPtrs(S, 2))))) * (*(real_T *)(mxGetData(K(S)))); rtb_Gain_n = (rtb_Product_p + (*(((const real_T**)ssGetInputPortSignalPtrs(S, 2))))) * (*(real_T *)(mxGetData(K(S))));
In automatic S-function generation, the Use Value for Tunable
Parameters option is cleared or at the command line is set to
Use a MEX S-function wrapper only in the MATLAB version in which the wrapper is created.
Suppose that you apply a storage class such as
Define to a
Simulink.Parameter object so that the
parameter appears as a macro in the generated code. If you use the parameter object
inside a subsystem from which you generate an ERT S-function, you cannot select the
parameter object as a tunable parameter. Instead, the S-function code generator
applies the custom storage class to the parameter object. This generation of macros
in the S-function code enables you to generate S-functions from subsystems that
contain variant elements, such as Variant Subsystem blocks, that you
configure to produce preprocessor conditionals in the generated code. You cannot
change the value of the parameter during simulation of the S-function.
To select the parameter object as a tunable parameter, apply a different storage
class or create your own storage class. Storage classes that treat parameters as
CompilerFlag, and storage classes that you create by setting
Data initialization to
the Custom Storage Class Designer. If you use a non-macro storage class, you cannot
use the parameter object as a variant control variable and generate preprocessor
If you apply a storage class that treats the parameter object as an imported
macro, before you generate the ERT S-function, provide the macro definition. For
example, suppose that you apply the storage class
Simulink.Parameter object and use the
parameter object as a variant control variable in the subsystem. If you set the
when you generate the S-function, place the custom header file
myHdr.h in the current folder. The generated S-function uses
the macro value from your header file instead of the value from the
Value property of the parameter object.
To use a macro that you define through a compiler option, for example by applying
the storage class
CompilerFlag, use the model configuration
parameter Code Generation > Custom Code > Code information > Defines to specify the compiler option. For more information, see Code Generation Pane: Custom Code: Additional Build Information: