How to set an input/output data type based block mask parameters?
조회 수: 26 (최근 30일)
이전 댓글 표시
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?
댓글 수: 0
채택된 답변
Andy Bartlett
2021년 9월 3일
편집: Andy Bartlett
2021년 9월 3일
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.
댓글 수: 2
Korey Kautzman
2024년 11월 9일 0:16
I am wondering if it is possible to achieve this goal, except with a Simulink.Bus output data type? For example, in the mask, I want to specify the MATLAB function block output data type as either 'Bus: MyBusTypeA' or 'Bus: MyBusTypeB'. Is this possible?
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Author Block Masks에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!