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:
- How to fix Matlab mex error: You can’t use ‘macro parameter character #’ in vertical mode.
- Missing additional libraries or include paths: How to link additional libraries when compiling Matlab/Simulink S-function via ‘mex’
- If you are using features of modern C++, you might need to select the correct C++ standard: How to specify C++ version when compiling Matlab/Simulink S-function using ‘mex’
Step 3: Create a new Simulink model
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:
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.
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:
Step 5 : Configure the S-function block
Double-click the S-function block to open the configuration dialog:
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.
Step 6: Connect the S-function block in Simulink
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:
Click on Sinks
and select the Scope
block:
Drag the Scope
block into the main Simulink model area:
Close the Library browser
dialog.
Now, connect the output of the S-function block to the input of the Scope
block:
The connected blocks should now look like this:
Step 7: Run the Simulink model
Click the Run
button in the Simulink toolbar to run the 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:
This will open a new window showing the scope output of the S-function: