How to set an input/output data type based block mask parameters?

22 views (last 30 days)
In a MATLAB function block of simulink, I want to set the output data type based on the block's mask parameter values. However, Simulink does not allow that when generating code. Is there any way to set the output data type by writing an expression of the mask parameter values or DataTypeStr Mask parameter or by any other approach?

Accepted Answer

Andy Bartlett
Andy Bartlett on 3 Sep 2021
Edited: Andy Bartlett on 3 Sep 2021
One way to achieve this is to use the Mask Initialization code to create a dummy "value holder variable" of the same type specified by the dialog parameter. Then pass that dummy "value holder variable" as a parameter into the MATLAB Function block.
For example, if the mask dialog parameter specifies the data type 'single', then create dummyVarToCarryDesiredType = single(0). Note the value held by this dummy variable is unimportant. I used the value 0 as an example, but it could just as well have been 54.32. What matters is the type of the value holder, not its value.
As another example, if the mask dialog parameter specifies the data type 'fixdt(1,8,3)', then create dummyVarToCarryDesiredType = fi(0,1,8,3).
Once you get the dummy variable passed inside the MATLAB Function block, you can get the type information from the dummy and do the desired operations. I've attached an example library block that does a cast operation.
Inside the MATLAB Function block code for this example, the elegant cast-like syntax is used.
function y = fcn(u,dummyVarToCarryDesiredType)
y = cast( u, 'like', dummyVarToCarryDesiredType);
end
Open the mask editor on this block to see how the rest works.
I've written this example to be polymorphic to all numerictypes: double, single, logical/boolean, int8, ... uint64, all fixed-point types, and half.
A key to the approach is the Mask Initialization code.
numericTypeFromMask1 = fixed.extractNumericType(dtOut);
% Cannot directly pass numerictype
% as a MATLAB Function Block input argument
% Create an dummy variable with that type
% in order to pass it as an input argument
%
if ishalf(numericTypeFromMask1)
% trap half precison floating-point as special case
dummyVarToCarryDesiredType = half(0);
showName = true;
else
temp1 = fi(0,numericTypeFromMask1);
temp2 = castFiToMATLAB(temp1);
dummyVarToCarryDesiredType = castIntToFi(temp2);
showName = ~isfi(temp2);
end
if ~showName
numericTypeForMLFBParam = numericTypeFromMask1;
maskIconTypeStr = numericTypeFromMask1.tostringInternalFixdt;
else
numericTypeForMLFBParam = numericTypeFromMask1.tostringInternalSlName;
maskIconTypeStr = numericTypeForMLFBParam;
end
Here is a screenshot of it working with all base MATLAB types.
Here is a screenshot of it working with fixed-point and half.
Both example models (using Release R2021a) are attached.
The first example simulates with just base Simulink.
The second example will also require Fixed-Point Designer license to simulate.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!