MATLAB: Using Simulink Coder, how to make sure that workspace parameters (variable references) in Simulink Blocks are recognized as such when generating code for S-function targets

Embedded Codersimulink coder

I am trying to convert a model to S-function executable, but discovered that while the generated code honors some instances of the parameters (via mxGetData() calls), in other calculations the values of the default, dummy, model workspace variables (required to define ref model arguments) are hard-wired.
How can I get the autocoded code to honor the run-time parameter values exclusively?

Best Answer

  • Sometimes, the S-function can be created with code generation options that prevent it from initializing all the parameters at run-time like you want.
    Please generate the S-Function again with the following options:
    - Keep the tlc selection you have previously rwtsfun.tlc
    - In the Code Generation -> Optimization tab, Select Default parameter behavior: Tunable.
    - In the Diagnostics -> Compatibility tab, Select S-function upgrades needed: none
    - Then generate code
    The resulting S-Function Block should open after code generation. If you double click on it, you will see that the Block's mask prompts you for all the input parameters in separate boxes. This means the generated code detected these parameters as needing user initialization, which you can provide by typing the name of the corresponding struct variables in each box.