Writing S-Functions    

A Simple M-File S-Function Example

The easiest way to understand how S-functions work is to look at a simple example. This block takes an input scalar signal, doubles it, and plots it to a scope.

The M-file code that contains the S-function is modeled on an S-function template called sfuntmpl.m, which is included with Simulink. By using this template, you can create an M-file S-function that is very close in appearance to a C MEX S-function. This is useful because it makes a transition from an M-file to a C MEX-file much easier.

Below is the M-file code for the timestwo.m S-function.

Below are the S-function subroutines that timestwo.m calls.

To test this S-function in Simulink, connect a sine wave generator to the input of an S-Function block. Connect the output of the S-Function block to a Scope. Double-click on the S-Function block to open the dialog box.

You can now run this simulation.


 Defining S-Function Block Characteristics Examples of M-File S-Functions