MATLAB: Tunability of sample time in code (embedded coder)

Embedded Codersample timesimulinksimulink.parametertunability

I am trying to have models to be completely free of hard coded sample rate.
Right now, these models are used in both Simulation and to generate code (embedded coder). However, the sample time is hard-coded in the solver options. The models extensively uses S-functions blocks for which one of the inputs is the sample time. I am feeding that input using a probe block to get the sample time.
Is there an easy way to:
– when generating code, to not have any hard-coded sample time in the generated code
– Be able to perform simulation
– Be able to see sample time mismatch using the display sample time option (I am specifically concerned about mismatch between model sample time and the one being fed to the S-function sample time input)
– Only have to change the sample time in a single location for model and a single location for code (for e.g. by using an extern variable or as one of the inputs to the model).
– Solution should work for multi-rate models
– Support variable sample time during execution (was hoping to use Simulink.Parameter with a storage class set to ImportedExtern).

Best Answer

This solution seems to work:
- defined sample time as a Simulink Parameter object with a storage class as imported extern. This enables to have the sample time defined in the code (non Simulink generated code)
- use the parameter object in models where required (blocks and solver options). For example, in a constant blocks value field and sample time fields (in order to accurately see the sample time propagation in models)
- make sure that the diagnostic loss of tunability is not set to error