is there a way to define 'fixdt' in a Matlab script and use this variable in a Simulink User-Defined Function?

12 views (last 30 days)
Matlab script code....DT_HD_accum = fixdt(0,24,23);
Simulink User-Defined Function (compiles fine)
result_nt = numerictype(0,24,23);
but would prefer to use:
result_nt = numerictype(DT_HD_accum);
error: Undefined function or variable 'DT_HD_accum'.
For code generation, all variables must be fully defined before use.

Accepted Answer

Kiran Kintali
Kiran Kintali on 13 Feb 2024
Can you share a bit more details of this usecase?
A sample model would be helpful. Are you using this in the context of a MATLAB function block?
I see you tagged Simulink and HDL Coder products, I am assuming you would like to generate HDL from such model.

More Answers (2)

Andy Bartlett
Andy Bartlett on 14 Feb 2024
Edited: Andy Bartlett on 14 Feb 2024
Inside the Simulink Library "User Defined Functions" section, there are about 16 different kinds of blocks. I'm guessing you are talking about the MATLAB Function Block kind.
If that guess is correct, this information may help.
MATLAB code inside a MATLAB Function Block or called by that code is handled by MathWorks code generation technologies even for simulation. Those technologies place some restrictions on the use of numerictype and fixdt.
Neither fixdt nor numerictype can be function argument, use dummy var instead
I believe neither numeric type object can be an input or output argument of a MATLAB function being handled by code generation technologies. The technique to pass numeric type information in or out of these functions is to create a dummy constant variable.
dummyVarToHoldDataType = fi( 0, numerictype( 1, 8, 7 ) )
dummyVarToHoldDataType =
0 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 8 FractionLength: 7
numerictype OK in body, but not fixdt, until R2024a
Inside the body of the MATLAB function being handled by coder technologies you can create numerictype variables. But again, I believe these can't cross function boundaries, that variable needs to stay inside that function body.
Suppose you passed numeric type information into a function using a dummy variable, you could then extract that information to a numerictype object using this utility.
nt = fixed.extractNumericType(dummyVarToHoldDataType)
nt = DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 8 FractionLength: 7
FYI: fixed.extractNumericType can support many other types of inputs.
help fixed.extractNumericType
extractNumericType Extract embedded.numerictype from input. T = fixed.extractNumericType(X) returns an embedded.numerictype object that is extracted from numeric value input X, or is specified by the input argument X. The following inputs are supported. Numeric values: MATLAB's 11 builtin numeric type variables double, single, logical, int8, ... uint64 Half-precision floating-point (half) Fixed-point numeric object (fi) Numeric type specification objects: embedded.numerictype objects Simulink.NumericType objects Data type name strings: Class name of MATLAB's 11 builtin numeric types 'double', 'single', 'logical', 'int8', ... 'uint64' Simulink's canonical name of a data type (not aliases) 'bool','sfix16_En3' Constructor strings that evaluate to a numeric type object: embedded.numerictype 'numerictype(1,33,55)' Simulink.NumericType 'fixdt(0,77,22)' Examples: % To extract the numeric type from a numeric value: T = fixed.extractNumericType(pi) % numerictype('double') T = fixed.extractNumericType(single(pi)) % numerictype('single') T = fixed.extractNumericType(half(pi)) % numerictype('half') T = fixed.extractNumericType(int8(0)) % numerictype(1,8,0) T = fixed.extractNumericType(fi(pi,1,24,12)) % numerictype(1,24,12) % To extract the numeric type from a numeric type specification object: T = fixed.extractNumericType(numerictype(1,32,16)) % numerictype(1,32,16) T = fixed.extractNumericType(fixdt(0,18,0)) % numerictype(0,18,0) % To extract the numeric type from a data type name string: T = fixed.extractNumericType('int8') % numerictype(1,8,0) T = fixed.extractNumericType('sfix16_En3') % numerictype(1,16,3) % To extract the numeric type from a constructor string: T = fixed.extractNumericType('numerictype(1,33,55)') % numerictype(1,33,55) T = fixed.extractNumericType('fixdt(0,77,22)') % numerictype(0,77,22) See also fi, numerictype, fixdt. Documentation for fixed.extractNumericType doc fixed.extractNumericType
In R2023b and earlier, a fixdt object cannot be used inside the body of a function being handled by coder technologies.
Once R2024a ships, that restriction on fixdt will be removed.

David Forey
David Forey on 13 Feb 2024
I am trying to use this as part of a Simulink model which contains a User-Defined Function. I am also using a Matlab script to define data types, so I have a command of "DT_HD_accum=fixdt(1,24,23);"
I'd like to be able to control the output data type of a Simulink User-Defined Function (SUDF) using a line such as "result_nt = numerictype(DT_HD_accum);" but this generates an error. Even though 'DT_HD_accum' is known within the Matlab workspace and is known in portions of the Simulink model when defining data types. It is just not recognized in SUDF.
The workaround I've been using is to add a dummy Constant Block ('dummy_in') as an input, with the data type I want for the output, and then define the output data type using "result_nt = numerictype(dummy_in);"

Categories

Find more on Code Generation in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!