Documentation

Create C/C++ S-Functions

Write S-functions using C or C++ code

Blocks

S-FunctionInclude S-function in model
S-Function BuilderCreate S-function from C code that you provide

S-Function Callback Methods

mdlInitializeSizesSpecify the number of inputs, outputs, states, parameters, and other characteristics of the C MEX S-function
mdlInitializeSampleTimesSpecify the sample rates at which this C MEX S-function operates
mdlOutputsCompute the signals that this block emits
mdlTerminatePerform any actions required at termination of the simulation
mdlCheckParametersCheck the validity of a C MEX S-function's parameters
mdlDerivativesCompute the C MEX S-function's derivatives
mdlDisableRespond to disabling of an enabled system containing this block
mdlEnableRespond to enabling of a enabled system containing this block
mdlGetSimStateReturn the C MEX S-function simulation state as a valid MATLAB data structure, such as a matrix structure or a cell array.
mdlGetTimeOfNextVarHitSpecify time of the next sample time hit
mdlInitializeConditionsInitialize the state vectors of this C MEX S-function
mdlProcessParametersProcess the C MEX S-function's parameters
mdlProjectionPerturb the solver's solution of a system's states to better satisfy time-invariant solution relationships
mdlRTWGenerate code generation data for a C MEX S-function
mdlSetDefaultPortComplexSignalsSet the numeric types (real, complex, or inherited) of ports whose numeric types cannot be determined from block connectivity
mdlSetDefaultPortDataTypesSet the data types of ports whose data types cannot be determined from block connectivity
mdlSetDefaultPortDimensionInfoSet the default dimensions of the signals accepted or emitted by a C MEX S-function's ports
mdlSetInputPortComplexSignalSet the numeric types (real, complex, or inherited) of the signals accepted by an input port
mdlSetInputPortDataTypeSet the data types of the signals accepted by an input port
mdlSetInputPortDimensionInfoSet the dimensions of the signals accepted by an input port
mdlSetInputPortDimensionsModeFcnPropagate the dimensions mode
mdlSetInputPortSampleTimeSet the sample time of an input port that inherits its sample time from the port to which it is connected
mdlSetInputPortWidthSet the width of an input port that accepts 1-D (vector) signals
mdlSetOutputPortComplexSignalSet the numeric types (real, complex, or inherited) of the signals accepted by an output port
mdlSetOutputPortDataTypeSet the data type of the signals emitted by an output port
mdlSetOutputPortDimensionInfoSet the dimensions of the signals accepted by an output port
mdlSetOutputPortSampleTimeSet the sample time of an output port that inherits its sample time from the port to which it is connected
mdlSetOutputPortWidthSet the width of an output port that outputs 1-D (vector) signals
mdlSetSimStateSet the simulation state of the C MEX S-function by restoring the SimState.
mdlSetWorkWidthsSpecify the sizes of the work vectors and create the run-time parameters required by this C MEX S-function
mdlSimStatusChangeRespond to a pause or resumption of the simulation of the model that contains this C MEX S-function
mdlStartInitialize the state vectors of this C MEX S-function
mdlUpdateUpdate a block's states
mdlZeroCrossingsUpdate zero-crossing vector

Examples and How To

C Code

Build S-Functions Automatically

How to use the S-Function Builder to generate S-functions automatically from specifications that you supply.

Basic C MEX S-Function

Illustrates the code needed to create a C S-function.

Write Callback Methods

How to write methods that the Simulink® engine calls as it executes your S-function.

S-Functions in Normal Mode Referenced Models

Describes requirements that must be met by a C S-function that is used in a referenced model that executes in Normal mode.

Convert Level-1 C MEX S-Functions

How to convert S-functions written for earlier Simulink releases to work with the current version.

C MEX S-Function Examples

Examples of C MEX S-functions.

C++ Code

Create a C++ Source File

Creating a C++ S-function from a C S-function

Make C++ Objects Persistent

How to create C++ objects that persist across invocations of the S-function.

Build C++ S-Functions

How to build a C++ S-function.

Concepts

About C S-Functions

Overview of writing a C S-function.

Creating C MEX S-Functions

You can create C MEX S-functions using any of the following approaches:

Simulink Engine Interaction with C S-Functions

Describes how the Simulink engine interacts with a C S-function. This is information that you need to know in order to create and debug your own C S-functions.

S-Function Builder Dialog Box

Describes the S-Function Builder dialog box.

Templates for C S-Functions

Describes code templates that you can use as starting points for writing your own C S-functions.

S-Function Callback Methods

Description of S-function callback methods

S-Function SimStruct Functions

Overview of SimStruct macros and functions.

SimStruct Macros and Functions Listed by Usage

SimStruct functions listed by usage.

Was this topic helpful?