Specify Data Types for an Edit Parameter Using Data Type Parameter
You can convert the parameter or output signal values of a block using a mask datatype parameter. The datatype parameter is associated with Min, Max, and Value parameters. The importance of the datatype parameter emerges only when it is linked to a child block. Additionally, validation or conversion of the output signal occurs only during compile time, not during edit time. In the child block, the associated mask datatype parameter converts the datatype of the parameter, while the Min and Max parameters restrict the value of the parameter.
Explore the Model
Use the block DataTypeStr
for specifying data types and minimum and maximum values for an edit parameter. The masked block DataTypeStr
has an edit parameter Gain
, a min parameter Minimum
, a max parameter Maximum
, and a Data Type parameter DataTypeStrParameter
. The subsystem DataTypeStr
contains a Gain block.
Associate Data Types to an Edit Parameter
The block DataTypeStr
is masked and the required parameters are created. Follow these steps if you want to create a similar model.
1. Open the masked block DataTypeStr
.F
2. Create an edit parameter Gain
, a min parameter Minimum
, a max parameter Maximum
, and a data type parameter DataTypeStrParameter
.
3. To specify the allowed data types, minimum and the maximum values for the Gain
parameter, select Data Type parameter in the Parameter pane and then double-click Type options in the Property Editor pane. The type options editor opens. Select the Built-in-types pane and then select double
and single
:
4. Click Associations to associate a data type parameter with an Edit parameter. Select the a min parameter Minimum
, a max parameter Maximum
in Design minimum and Design maximum to associate the minimum and maximum parameter to the edit parameter Gain
parameter.
5. Click OK in the Type Options editor to save the rules selection.
6. Click Save Mask to save the mask.
Setting the acceptable datatypes through the data type parameter does not set the signal attribute or output data type for the associated edit parameter. Set the data type associated with the edit parameter directly on the underlying block of the masked subsystem that uses the edit parameter.
7. Right-click the block DataTypeStr
. Select Mask and then select Look Under Mask.
8. Double-click the Gain block, in the Block Parameters dialog, specify the value of Gain as Gain
to reference the value of the edit parameter.
9. Select Signal Attributes, specify Minimum
in Output minimum and Maximum
in Output maximum to restrict the minimum and maximum output value of the Gain block. Specify DataTypeStrParameter
in Output data type to convert the output signal of the edit parameter to the data type selected in the mask dialog.
10. Open the mask dialog of DataTypeStr
block. Specify the value of the Gain
parameter as 34
, select the data type as single
and simulate the model.
Observe that the Gain
value is validated against the minimum and maximum values and an error is displayed in the Diagnostic Viewer. Also note that you can select the data type only between single
and double
and the output signal is converted to single
.
Note: The difference between Data Type parameter and Parameter constraints is that data type parameter converts the input signal to the data type selected in the mask dialog and parameter constraint validates the signal against a condition.
Below is the information about the inheritance, built-in data types, fixed point, and user-defined datatypes rules.
Inherit rules — Specify inheritance rules for determining the data types. The inheritance rules are grouped under three categories: Common Simulink rules, Custom rules, and Advanced Simulink rules. The Advanced rules section allows you to inherit rules from the blocks such as Lookup table, Constant, and Gain. For example, breakpoint data, constant value, gain, table data, logic data, accumulator, product output, and Simulink. It also allows you to have same word length as input and have same data types for all ports. The Custom rules section lists any custom inheritance rules registered on the MATLAB® search path. For definitions of some Inheritance rules, see Data Type Inheritance Rules.
Built-in types — Specify one or more built-in Simulink data types, such as
double
orsingle
. For more information, see Data Types Supported by Simulink.
Fixed-point — Specify the scaling and signed modes for a fixed-point data type. For more information, see Specifying a Fixed-Point Data Type.
User-defined — Specify a bus object, an enumerated data type, or a string. For more information, see Specify an Enumerated Data Type.
Associate Data Type to an Edit Parameter Programmatically
1. Create a mask on the current model.
maskObj = Simulink.Mask.get(gcb);
2. Create edit
, min
, and max
parameters.
maskObj.addParameter('Name','Gain', 'Type','edit'); maskObj.addParameter('Name','Min', 'Type','min'); maskObj.addParameter('Name','Max', 'Type','max');
3. Add a data type
parameter and associate it to the edit parameter Gain
.
dataTypeParam = maskObj.addParameter('Name','DataType');
dataTypeParam.Type = 'unidt({a=DataType|Min|Max|Gain}{i=Inherit: auto|Inherit:Inherit via internal rule}{b=double|single})'
Where, Type
displays the values specified for the DataType parameter and has these definitions:
The associations are defined by
a
.
Inherit rules are defined by
i
and its corresponding value isInherit: Same as first input
.
Built-in types are defined by
b
and its corresponding value aredouble
andsingle
.