How to generate a Simulink S-Function programmatically?

12 views (last 30 days)
Hello, i have different Simulink models with lots of Subsystems in there. Some of these Subsystems must be generated as s-function. I was thinking about writing a script to generate the s-functions, but i don't found a function building s-functions programmatically. Is there any way?

Accepted Answer

Saurabh Gupta
Saurabh Gupta on 19 Jul 2017
You should be able to use a combination of instructions in the following two documentation sections to write a script that generates S-functions for you.
The high level idea is to
  1. set the System Target as 'rtwsfcn.tlc',
  2. make sure "Create new model" is selected, and
  3. use the subsystem handle to call the 'rtwbuild' function like
>> new_block = rtwbuild(subsystem_handle)
The 'rtwbuild' function will return the handle to the newly created S-function block, which you should be able to use to perform the intended operations on the new block (like replacing the sub-system with it or saving the block in a new model).
Moritz Burghardt
Moritz Burghardt on 23 Sep 2021
You need to deselect the option "Generate Code only". You can do it programatically:
set_param(cs,'GenCodeOnly',0); % Deselect "Generate code only"

Sign in to comment.

More Answers (0)


Find more on Block Authoring and Simulation Integration in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!