Store, share, and configure parameter values
Create a Simulink.Parameter
object to set the value of one
or more block parameters in a model, such as the Gain parameter of
a Gain block. You create the object in a workspace or in a data
dictionary. Set the parameter value in the object, then reference the object from the
block.
Use a Simulink.Parameter
object to:
Share a value among multiple block parameters.
Represent an engineering constant or a tunable calibration parameter.
Separate a parameter value from its data type and other properties.
Configure parameter data for code generation.
The Value
property of the object stores the parameter value. To
use the object in a model, set the value of a block parameter to an expression that
includes the name of the object. Omit the Value
property from the
expression. For more information, see Use Parameter Objects.
For more information about block parameters, see Set Block Parameter Values and How Generated Code Stores Internal Signal, State, and Parameter Data (Simulink Coder).
Create a Simulink.Parameter
object:
Directly from a block dialog box or the Property Inspector. See Create, Edit, and Manage Workspace Variables.
By using the Model Data Editor. Inspect the Parameters tab. Right-click the row that contains a variable and from the context menu, select Convert to parameter object.
By using the Model Explorer. See Create Data Objects from Built-In Data Class Package Simulink.
By using the Simulink.Parameter
function described
below.
paramObj = Simulink.Parameter
returns a
Simulink.Parameter
object with default property
values.
paramObj = Simulink.Parameter(
returns a paramValue
)Simulink.Parameter
object and initializes the
Value
property to paramValue
.
At the command prompt, create a Simulink.Parameter
object.
myParam = Simulink.Parameter;
Assign a numeric value to the Value
property.
myParam.Value = 15.23;
Specify the minimum and maximum values the parameter can take with the
Min
and Max
properties.
myParam.Min = 10.11; myParam.Max = 25.27;
Open a new Simulink model. Add a Gain block and set its
Gain parameter to myParam
.
During simulation, the Gain parameter uses the
value 15.23
.
At the command prompt, create a Simulink.Parameter
object that stores the value 2.52
.
myParam = Simulink.Parameter(2.52);
Change the value by accessing the Value
property
of the object. This technique preserves the values of the other
properties of the object.
myParam.Value = 1.13;
To reduce model maintenance, you can leave the DataType
property at its default value, auto
. The parameter object
acquires a data type from the block parameter that uses the object.
To reduce the risk of the data type changing when you make changes to signal data types and other data types in your model, you can explicitly specify a data type for the parameter object. For example, when you generate code that exports parameter data to your custom code, explicitly specify a data type for the object.
At the MATLAB command prompt, create a
Simulink.Parameter
object that stores the value
18.25
.
myParam = Simulink.Parameter(18.25);
The expression 18.25
returns the number
18.25
with the double-precision, floating-point
data type double
. The Value
property stores the number 18.25
with double
precision.
Use the DataType
property to specify the
single-precision data type single
.
myParam.DataType = 'single';
When you simulate or generate code, the object casts the value of the
Value
property, 18.25
, to
the data type specified by the DataType
property,
single
.
This example shows how to set the value of a parameter object,
myParam
, to the sum of two other variables,
myVar
and myOtherVar
. With this technique,
when you change the values of the independent variables, Simulink immediately calculates the new value of the parameter object.
Create the two independent variables.
myVar = 5.2; myOtherVar = 9.8;
Create the parameter object.
myParam = Simulink.Parameter;
Set the value of the parameter object to the expression myVar +
myOtherVar
.
myParam.Value = slexpr('myVar + myOtherVar')
When you simulate or generate code, the expression evaluates to
15
.
Simulink.AliasType
| Simulink.Breakpoint
| Simulink.CoderInfo
| Simulink.LookupTable
| Simulink.Signal
| AUTOSAR.Parameter
(AUTOSAR Blockset)