MATLAB: How to build an S-function with tunable parameters from the MATLAB Command-Line using Real-Time Workshop 7.2 (R2008b)

simulink coder

I want to build an S-function with tunable parameters, from a subsystem in my Simulink model, via the MATLAB prompt. In other words, I want to execute right-clicking on the subsystem, followed by "Real-Time Workshop -> Generate S-function" from the command line.

Best Answer

If the S-function target is selected in "Configuration Parameters -> Real-Time Workshop -> Target selection", executing the RTWBUILD command triggers a subsystem build from the MATLAB prompt. For the tunable parameters to get built properly, the storage classes of the parameters must not be set to "auto", and the "Configuration Parameters -> Optimization -> Inline Parameters" option must be checked.
Attached is an example model. The variable used in the subsystem can be generated by executing:
x = Simulink.Parameter
x.Value = 2
After executing this code, go to the Workspace browser, double-click on 'x', and change the storage class to "SimulinkGlobal" (or another class that is not "auto"). Then, execute "rtwbuild('tunable_sfcn/Subsystem')" to build this code.
Note that double-clicking on the generated S-function block shows that the "x" parameter is indeed tunable.