When does a Simulink.Parameter automatically change its DataType based on Value?

I am using a Simulink.Parameter in my model, and I noticed the DataType changes automatically. The code below demonstrates the behavior I am seeing. If I specify a single Value then it switches the DataType from 'auto' to 'single':
>> x = Simulink.Parameter;
>> x.DataType
ans =
'auto'
>> x.Value = single(3);
>> x.DataType
ans =
'single'
But then any time I change the Value field, the DataType gets modified:
>> x.Value = 3;
>> x.DataType
ans =
'auto'
Is that expected behavior? Under what conditions will the DataType be changed automatically? I would like to specify the data type once, and for it to never be changed.

 Accepted Answer

One scenario where the DataType will be changed automatically is covered in the documentation here: https://www.mathworks.com/help/simulink/slref/simulink.parameter.html#d123e524167
The documentation states: "When you set the Value property to something other than a double number, the object typically sets the DataType property based on the value of the Value property. For example, when you set the Value property to int8(5), the object sets the value of the DataType property to 'int8'."
The other scenario is if the Value property of the Simulink.Parameter is switched from a non-double number to a double number. In that case, the DataType property is automatically changed to 'Auto'. For example:
%This stores a single in Value
>> x1 = Simulink.Parameter;
>> x1.Value = single(3.0); %Value is technically a single. x1.DataType will be automatically updated.
>> disp(x1)
Parameter with properties:
Value: 3
CoderInfo: [1×1 Simulink.CoderInfo]
Description: ''
DataType: 'single'
Min: []
Max: []
Unit: ''
Complexity: 'real'
Dimensions: [1 1]
In the example above, the Value property has a non-double datatype, ie
>> class(x1.Value)
ans =
'single'
Now if you specify the Value of the parameter using a double (the MATLAB default), you will see the DataType property of the Simulink.Parameter change back to Auto:
>> x1.Value = 3.0; %this is a double because that is default for MATLAB
>> disp(x1);
Parameter with properties:
Value: 3
CoderInfo: [1×1 Simulink.CoderInfo]
Description: ''
DataType: 'auto'
Min: []
Max: []
Unit: ''
Complexity: 'real'
Dimensions: [1 1]
If you don't want the DataType to change, we recommend storing a double number in the Value property of the parameter, and then specifying the desired DataType:
%This stores a double in Value, but will cast to single when used in a model
x1.Value = 3.0; %Value is technically a double. Can verify by checking class(x1.Value)
x1.DataType = "single";
Then as long as you continue specifying a double for the Value property of the parameter, the DataType property will not change. Simulink will automatically cast the parameter to the specified data type when used in simulation or code generation. 

More Answers (0)

Products

Release

R2021b

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!