Search code examples
matlabsimulinkmatlab-compiler

Simulink code generation: set tunable parameters programmatically


I use Simulink Coder to generate code from a huge model. I use a rsim target with tunable parameters to be able to give the executable variable inputs via a parameter file.

I can specify which model parameters should be tunable (by default all parameters will be in-lined in generated code on compile time) via the code generation settings:
code generation options > optimization > signals and parameters > configure default parameter behavior

enter image description here

Here I can manually choose from all workspace variables the ones I want to be globally tunable:

enter image description here

Q: Is there a way to add a variable (given its name) to this list programmatically?

For example if a have list of 50 variables i would want to add them to the tunables list, via a MATLAB script without having to add every single one manually. Or loop over the list and set the tunable setting on each one.

I can generate a parameter struct which contains a list of tunable parameters using rsimgetrtp('model_name'). But I was not able to find a function to actually set the parameters in the first place.

I use Matlab 2015b for this, since its legacy code.


Solution

  • I finally found a solution:

    You can set tunable parameters of a model via the following object parameters:

    set_param(gcs, 'TunableVars', 'var1,var2,var3')
    set_param(gcs, 'TunableVarsStorageclass', 'Auto,Auto,Auto')
    set_param(gcs, 'TunableVarsTypeQualifier', ',,')
    

    You need to set all three parameters so that the length of the lists is consistent.

    Note that the model needs to be initialized first.

    You can check the model Parameter Configuration Dialog for your new parameters