Reuse Variant Parameter Values from Handwritten Code Using Simulink.Parameter Variables
This example shows how to use
variables to generate a code that imports variant parameter values from your existing code. Importing variant parameter values allows you to reuse the values that your existing code defines. You can then integrate and compile the generated and existing code into a single executable. You can choose to generate a code that imports variant parameter values at the beginning of code compile, simulation-loop, or model startup phases of simulation and code generation workflows based on the activation time you specify. For more information, see Activate Variant During Different Stages of Simulation and Code Generation Workflow.Simulink.Parameter
Limitations
When you use
Simulink.Parameter
variables as values in a variant parameter, corresponding properties of all theSimulink.Parameter
variables must be the same. Only the values ofSimulink.Parameter
variables can vary.
Simulink.Parameter
variables as values in a variant parameter can be defined only in the base workspace and data dictionary. DefiningSimulink.Parameter
variables in the mask or model workspace is not supported.
Simulink.Parameter
variables as values in a variant parameter within structures and that have data types other thanSimulink.Bus
objects are not supported.
Simulink.Parameter
variables as values in a variant parameter must have storage classes listed in Storage Classes for Different Variant Activation Times.
Prerequisite
Before you start this example, we recommend you complete Options to Represent Variant Parameters in Generated Code.
Explore the Model
Open the slexVariantParamsUsingSlParamChoices
model.
open_system('slexVariantParamsUsingSlParamChoices')
In this model, the Gain parameter of the Gain block is set to the variant parameter kv
.
Open the slexVariantParameterSlParamData.m
file. This file contains the definition of kv
. kv
has the two values kp1
and kp2
of Simulink.Parameter
type. The storage class of kp1
and kp2
is set to ImportedExtern, ImportedExternPointer
. The value of variant control variable V
is set to 1
, and its activation time is specified as startup
.
During simulation, the condition V == 1
evaluates to true
. Since, kp1
is associated with V == 1
, kp1
becomes active and kp2
becomes inactive. The code that you generate from this model, imports values of kp1
and kp2
from the handwritten code that is provided in Modeling > Model Settings > Code Generation > Custom Code > Additional Source Code > Additional Code. You can then compile the code into a single executable that switches the active values based on the value of variant control variable V
.
type slexVariantParameterSlParamData.m
% This file defines variant parameters using Simulink.Parameter variables % as choice values for slexVariantParamsUsingSlParamChoices example. The % variant parameter value is determined using the Simulink.Parameter values % in simulation and code generation. % Copyright 2021 The MathWorks, Inc. % Variant control variable V = Simulink.VariantControl('Value', 1, 'ActivationTime', 'startup'); % Simulink.Parameter choice variables kp1 = Simulink.Parameter(3); kp1.CoderInfo.StorageClass = 'ImportedExtern'; kp2 = Simulink.Parameter(6); kp2.CoderInfo.StorageClass = 'ImportedExtern'; % Variant parameter kv = Simulink.VariantVariable('Choices', {'V == 1', 'kp1', 'V == 2', 'kp2'});
Generate Code Using Embedded Coder
Before you generate code from the model, make sure that you have write permission in your current folder.
In the Apps gallery of the model toolstrip, click Embedded Coder. On the C Code tab, click Generate Code. For more information, see Generate Code Using Embedded Coder.
slbuild('slexVariantParamsUsingSlParamChoices')
### Starting build procedure for: slexVariantParamsUsingSlParamChoices ### Successful completion of build procedure for: slexVariantParamsUsingSlParamChoices Build Summary Top model targets: Model Build Reason Status Build Duration ======================================================================================================================================= slexVariantParamsUsingSlParamChoices Information cache folder or artifacts were missing. Code generated and compiled. 0h 0m 11.959s 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 13.015s
Review Generated Code
1. In the C Code tab, in the Results section, click Open Report.
2. From the Generated Code pane of the report, select the slexVariantParamsUsingSlParamChoices_private.h
file. kp1
and kp2
are declared as extern
variables to use values from your handwritten code.
/* Imported (extern) block parameters */ extern real_T kp1; /* Variable: kp1 * Referenced by: '<Root>/Gain' */ extern real_T kp2; /* Variable: kp2 * Referenced by: '<Root>/Gain'
Your handwritten code is imported in slexVariantParamsUsingSlParamChoices.c
file.
User code, define parameter choice values outside the generated code */ double kp1 = 3.0; double kp2 = 6.0;
During code compile, the generated and your handwritten code is integrated into a single executable. At model startup, that is, prior to running the executable, the value of the variant parameter kv
is determined based on the condition that evaluates to true
.
/* Model initialize function */ void slexVariantParamsUsingSlParamChoices_initialize(void) { /* Variant Parameters using Simulink.Parameter variables for choice values */ if (V == 1) { slexVariantParamsUsingSlParam_P.kv = kp1; } else if (V == 2) { slexVariantParamsUsingSlParam_P.kv = kp2; }
/* startup variant condition checks */ utAssert((V == 1) + (V == 2) == 1); }