How do you use parameters with simulink coder?

3 views (last 30 days)
I am trying to generate code from simulink models using simulink coder. I tend to initialize my simulink models using parameters in the base workspace e.g.,
initial_condition = struct();
initial_condition.start_a = 11;
initial_condition.start_b = 22;
sim('test_toy_model.slx')
I attached a toy example where the "initial_condition" parameter gets input to different masked subsystems, and eventually gets used as the initial condition for an integrator and the value of a gain.
The problem is that when code is generated from the model it is no longer clear how to change the parameters and have a corresponding change in model behavior.
The default parameter behavior is tunable, so the values aren't hardcoded in the generated code. But, rather than generate one "initial_condition" structure, it generates a parameter storage structure (in "toy_model.h"):
/* Parameters (default storage) */
struct P_toy_model_T_ {
struct_qr0vgQj1ieUOrXT4kqD0DC initial_condition;/* Variable: initial_condition
* Referenced by: '<S1>/Gain1'
*/
real_T Integrator_IC[2]; /* Expression: [start.start_a; start.start_b]
* Referenced by: '<S2>/Integrator'
*/
real_T Gain_Gain[2]; /* Expression: ones(2,1)
* Referenced by: '<S1>/Gain'
*/
};
This parameter structure duplicates the initial condition structure, and the integrator initial conditions (which in the simulink model are set from the initial condition structure). There is no way for a user to change the value of "initial_condition.start_a" or "initial_condition.start_b" and have it update both the gain values and the integrator initial conditions (like what would happen with the simulink model).
Adding to the complication is that in the class definition
/* Tunable parameters */
static P_toy_model_T toy_model_P;
is private!
Is there a setting I can use to change this behavior? Or, do I have a fundamental misunderstanding of how parameters should be used with a simulink model?
  1 Comment
spencer preston
spencer preston on 28 Jun 2022
Did you ever get an answer to this question? I have the same problem.

Sign in to comment.

Answers (1)

Benjamin Thompson
Benjamin Thompson on 28 Jun 2022
You can change the parameter configuration using the Model Parameter Configuration dialog like below. But note that code from Simulink Coder is not intended to be compiled and run outside MATLAB. You may actually want Embedded Coder. With Simulink Coder you usally want to generate an executable for a desktop which will have an interface for changing parameter values, or target one of the Mathworks supported hardware platforms. All things things require access to your MATLAB license.
  1 Comment
spencer preston
spencer preston on 28 Jun 2022
Thank you for the reply! I only have grt, and need the c++ code to integrate with a c++ sim. I've gone through this same process with 2018b and didn't have any troubles with the public parameters. Now I can't seem to figure it out with 2021b. Did something change?

Sign in to comment.

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by