How to add C++ S-function to Simulink

In our previous examples such as Matlab Level 2 S-Function example: Sine wave (continous output) we’ve shown how to create a simple S-function in C++.

Here’s how to add a C++ S-function to Simulink:

Step 1: Create the source code file of the S-functon

In the directory you have opened in Matlab/Simulink, create a new file named e.g. sine_wave_generator.cpp. See Matlab Level 2 S-Function example: Sine wave (continous output) for an example of the C++ code.

I recommend using Visual Studio Code instead of Matlab as a C++ editor.

Step 2: Compile the S-function

Open a terminal and run

mex sine_wave_generator.cpp

I do not recommend running this in Matlab even though this is possible. I recommend always using a system terminal.

The compilation process works only if the last line of the output is MEX completed successfully.

If you see any other output, you need to debug the issue.

Common issues are:

For testing, I recommend to create a new Simulink model instead of re-using an old one.

Step 4: Add an S-function block

First start by click the dialog to add a new block:

Simulink library browser

Now, click User-defined functions and select the S-function block. Do not select the Matlab Level-2 S-function block even if you are using a Level 2 s-function. Level 2 S-functions work just fine with the normal S-function block.

Simulink add S function block

Drag this block into the main Simulink model area, i.e. the big white empty space in the other window which usually is just behind the library browser dialog:

Simulink drag S function

Step 5 : Configure the S-function block

Double-click the S-function block to open the configuration dialog:

Simulink double click S function

In the dialog, edit the Name and enter the name of the S-function.

The name of the S-function is always the name of the .cpp file without the .cpp extension.

In our case, the C++ file is named sine_wave_generator.cpp, hence the name of the S-function is sine_wave_generator.

Press Apply to save the changes and close the dialog.

In our example, we can connect the S-function block to a Scope block to visualize the output.

This is not mandatory but it’s a good way to verify that the S-function works as expected.

In order to add the Scope block, click on Library browser icon in the navbar again:

Simulink library browser

Click on Sinks and select the Scope block:

Simulink add Scope block

Drag the Scope block into the main Simulink model area:

Simulink drag scope

Close the Library browser dialog.

Now, connect the output of the S-function block to the input of the Scope block:

Simulink connect S function

The connected blocks should now look like this:

Simulink connected S function

Click the Run button in the Simulink toolbar to run the simulation:

Simulink run simulation

Step 8: Check the scope

If everything works as expected, you can now double-click the Scope block to see the output of the S-function:

Simulink double click scope

This will open a new window showing the scope output of the S-function:

MATLAB Continous S Function Sine Wave Generator