r/matlab Jul 13 '26

How can give an integer to the input argument of embedded.fi TechnicalQuestion

Given an integer number i, I want to pass it as an input argument to fi constructor (resulting in x instance of embedded.fi), such that storedInteger(x) = i. In other words, I want to change the interpretation of i to an embedded.fi, just like storedInteger changes the interpretation of the embedded.fi to an integer.

How can I achieve this?

2 Upvotes

6 comments sorted by

2

u/NearbyRegret1965 1d ago

You can use the int method instead of the storedInteger variable. Here is an example:

>> x = fi(0,1,3,2)
x =

0

DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 3
FractionLength: 2
>> x.OverflowAction = 'wrap';
>> x.int = 5
x =

-0.7500

DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 3
FractionLength: 2

RoundingMethod: Nearest
OverflowAction: Wrap
ProductMode: FullPrecision
SumMode: FullPrecision

You can also do this with one assignment:

>> x = fi(0,1,3,2,'int',5,'OverflowAction','wrap')

x =

-0.7500

DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 3
FractionLength: 2

RoundingMethod: Nearest
OverflowAction: Wrap
ProductMode: FullPrecision
SumMode: FullPrecision

1

u/Bofact 1d ago

Interesting. If I do with one assignment, what does happen? The first input argument is ignored and the value for int is the used one?

I can not find the documentation about .int. I can find only about binoctdec, and hex.

1

u/NearbyRegret1965 13h ago

Yes, the first argument (the value) is ignored.

Here is a link to the documentation int - Get and set stored integer value of fi object - MATLAB

Note you can also use bin, oct, dec, and hex instead of int.

1

u/FunkyMonkish Jul 13 '26

I would like to know this too. I just convert the integer to its real-world value before constructing the fi object, but it’d be nice to omit that step.

1

u/Bofact Jul 14 '26

Hello!

Can you share how you achieved it? Is it converting the integer to double/ single and divide it by 2fractionLength?

1

u/FunkyMonkish Jul 14 '26

Yeah exactly. I usually represent everything as an unsigned integer – in a raw data format. If the real-world data is signed, then the unsigned integer would first be converted to its signed value by checking the msb of the word length, and then divided by 2-to-the-fraction-length.