Writing S-Functions    

Specifying Port-Based Sample Times

If you want your S-function to use port-based sample times, you must specify the number of sample times as port-based in the S-function's mdlInitializeSizes method:

You must also specify the sample time of each input and output port in the S-function's mdlInitializeSizes method, using the following macros

For any given port, you can specify

Specifying Inherited Sample Time for a Port

To specify that a port's sample time is inherited, the mdlInitializeSizes method should set its period to -1 and its offset to 0. For example, the following code specifies inherited sample time for the S-function's first input port:

When you specify port-based sample times, Simulink calls mdlSetInputPortSampleTime and mdlSetOutputPortSampleTime to determine the rates of inherited signals.

Once all rates have been determined, Simulink calls mdlInitializeSampleTimes. Even though there is no need to initialize port-based sample times at this point, Simulink invokes this method to give your S-function an opportunity to configure function-call connections. Your S-function must thus provide an implementation for this method regardless of whether it uses port-based sample times or function-call connections. Although you can provide an empty implementation, you might want to use it to check the appropriateness of the sample times that the block inherited during sample time propagation.

Specifying Constant Sample Time for a Port

If your S-function uses port-based sample times, it can specify that any of its ports has a constant sample time. This means that the signal entering or leaving the port never changes from its initial value at the start of the simulation.

Before specifying constant sample time for an output port whose output depends on the S-function's parameters, the S-function should use ssGetInlineParameters to check whether the user has specified the Inline parameters option on the Advanced pane of the Simulation parameters dialog box. If the user has not checked this option, it is possible for the user to change the values the S-function's parameters and hence its outputs during the simulation. In this case, the S-function should not specify a constant sample time for any ports whose outputs depend on the S-function's parameters.

To specify constant sample time for a port, the S-function must perform the following tasks

See sfun_port_constant.c, the source file for the sfcndemo_port_constant demo, for an example of how to create ports with a constant sample time.

Configuring Port-Based Sample Times for Use in Triggered Subsystems

To be usable in a triggered subsystem, your port-based sample time S-function must perform the following tasks.

See sfun_port_triggered.c, the source file for the sfcndemo_port_triggered demo, for an example of how to create ports with a constant sample time.

Hybrid Block-Based and Port-Based Sample Times

The hybrid method of assigning sample times combines the block-based and port-based methods. You first specify, in mdlInitializeSizes, the total number of rates at which your block operates, including both internal and input and output rates, using ssSetNumSampleTimes. You then set the SS_OPTION_PORT_SAMPLE_TIMES_ASSIGNED, using ssSetOptions, to tell the simulation engine that you are going to use the port-based method to specify the rates of the input and output ports individually. Next, as in the block-based method, you specify the periods and offsets of all of the block's rates, both internal and external, using

Finally, as in the port-based method, you specify the rates for each port, using

Note that each of the assigned port rates must be the same as one of the previously declared block rates.


  Block-Based Sample Times Multirate S-Function Blocks