Documentation

Create C/C++ S-Functions

Write S-functions using C or C++ code

Blocks

S-Function Include S-function in model
S-Function Builder Create S-function from C code that you provide

S-Function Callback Methods

mdlInitializeSizes Specify the number of inputs, outputs, states, parameters, and other characteristics of the C MEX S-function
mdlInitializeSampleTimes Specify the sample rates at which this C MEX S-function operates
mdlOutputs Compute the signals that this block emits
mdlTerminate Perform any actions required at termination of the simulation
mdlCheckParameters Check the validity of a C MEX S-function's parameters
mdlDerivatives Compute the C MEX S-function's derivatives
mdlDisable Respond to disabling of an enabled system containing this block
mdlEnable Respond to enabling of a enabled system containing this block
mdlGetSimState Return the C MEX S-function simulation state as a valid MATLAB data structure, such as a matrix structure or a cell array.
mdlGetTimeOfNextVarHit Specify time of the next sample time hit
mdlInitializeConditions Initialize the state vectors of this C MEX S-function
mdlProcessParameters Process the C MEX S-function's parameters
mdlProjection Perturb the solver's solution of a system's states to better satisfy time-invariant solution relationships
mdlRTW Generate code generation data for a C MEX S-function
mdlSetDefaultPortComplexSignals Set the numeric types (real, complex, or inherited) of ports whose numeric types cannot be determined from block connectivity
mdlSetDefaultPortDataTypes Set the data types of ports whose data types cannot be determined from block connectivity
mdlSetDefaultPortDimensionInfo Set the default dimensions of the signals accepted or emitted by a C MEX S-function's ports
mdlSetInputPortComplexSignal Set the numeric types (real, complex, or inherited) of the signals accepted by an input port
mdlSetInputPortDataType Set the data types of the signals accepted by an input port
mdlSetInputPortDimensionInfo Set the dimensions of the signals accepted by an input port
mdlSetInputPortDimensionsModeFcn Propagate the dimensions mode
mdlSetInputPortSampleTime Set the sample time of an input port that inherits its sample time from the port to which it is connected
mdlSetInputPortWidth Set the width of an input port that accepts 1-D (vector) signals
mdlSetOutputPortComplexSignal Set the numeric types (real, complex, or inherited) of the signals accepted by an output port
mdlSetOutputPortDataType Set the data type of the signals emitted by an output port
mdlSetOutputPortDimensionInfo Set the dimensions of the signals accepted by an output port
mdlSetOutputPortSampleTime Set the sample time of an output port that inherits its sample time from the port to which it is connected
mdlSetOutputPortWidth Set the width of an output port that outputs 1-D (vector) signals
mdlSetSimState Set the simulation state of the C MEX S-function by restoring the SimState.
mdlSetWorkWidths Specify the sizes of the work vectors and create the run-time parameters required by this C MEX S-function
mdlSimStatusChange Respond to a pause or resumption of the simulation of the model that contains this C MEX S-function
mdlStart Initialize the state vectors of this C MEX S-function
mdlUpdate Update a block's states
mdlZeroCrossings Update 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?