Main Content

Set fi Object Properties

Set fi object properties when you create the fi object.

Set Fixed-Point Properties at Object Creation

You can set properties of fi objects at the time of object creation by including properties after the arguments of the fi constructor function. For example, to set the overflow action to Wrap and the rounding method to Convergent in the fi constructor.

a = fi(pi,'OverflowAction','Wrap',...
    'RoundingMethod','Convergent')
a = 

    3.1416

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 16
        FractionLength: 13

        RoundingMethod: Convergent
        OverflowAction: Wrap
           ProductMode: FullPrecision
               SumMode: FullPrecision

To set the stored integer value of a fi object, use the parameter name-value pair arguments for the 'int' property when you create the object. For example, create a signed fi object with a stored integer value of 4, 16-bit word length, and 15-bit fraction length.

x = fi(0,1,16,15,'int',4);

Verify that the fi object has the expected integer setting.

x.int
ans =

  int16

   4

Use Subscripted Assignment to Set Real-World Value of fi Object

You can set the real-world value of a fi object via subscripted assignment.

a = fi(pi);
a(:) = 2
a = 

     2

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 16
        FractionLength: 13

Direct Property Referencing to Read fi Object Properties

You can read fi object property values using MATLAB® structure-like referencing. You do this by using a period to index into a property by name.

For example, to get the WordLength of a.

a.WordLength
ans =

    16

Best Practices for Code Generation

The following methods for setting fi object properties are recommended for compatibility with code generation.

First, define the fi object a.

a = fi(pi,'OverflowAction','Wrap',...
    'RoundingMethod','Convergent')
a = 

    3.1416

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 16
        FractionLength: 13

        RoundingMethod: Convergent
        OverflowAction: Wrap
           ProductMode: FullPrecision
               SumMode: FullPrecision

You can get the fimath using dot indexing, use the fimath constructor to change the fimath settings, then use setfimath to set the local fimath object back into fi object a.

F = fimath(a.fimath,'OverflowAction','Saturate');
a = setfimath(a,F)
a = 

    3.1416

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 16
        FractionLength: 13

        RoundingMethod: Convergent
        OverflowAction: Saturate
           ProductMode: FullPrecision
               SumMode: FullPrecision

The setfimath function is useful for changing out the fimath altogether. For example:

a = fi(pi);
F = fixed.fimathLike(a);
a = setfimath(a,F)
a = 

    3.1416

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 16
        FractionLength: 13

        RoundingMethod: Floor
        OverflowAction: Wrap
           ProductMode: SpecifyPrecision
     ProductWordLength: 16
 ProductFractionLength: 13
               SumMode: SpecifyPrecision
         SumWordLength: 16
     SumFractionLength: 13
         CastBeforeSum: true

Alternatively, you can call the fi object constructor with the value input set to the original fi object, then add new fimath parameters directly in the fi object constructor. For example:

a = fi(pi,'OverflowAction','Wrap',...
    'RoundingMethod','Convergent')
a = 

    3.1416

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 16
        FractionLength: 13

        RoundingMethod: Convergent
        OverflowAction: Wrap
           ProductMode: FullPrecision
               SumMode: FullPrecision
a = fi(a,'OverflowAction','Saturate')
a = 

    3.1416

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 16
        FractionLength: 13

        RoundingMethod: Convergent
        OverflowAction: Saturate
           ProductMode: FullPrecision
               SumMode: FullPrecision

Note that using dot indexing to write fimath and numerictype object properties is not compatible with code generation. For example:

a.OverflowAction = 'Saturate' % Works in interpreted MATLAB only

Remove Local fimath Properties from fi Object

If you have a fi object b with a local fimath object, you can remove the local fimath object and force b to use default fimath values.

b = fi(pi,1,'RoundingMethod','Floor')
b = 

    3.1415

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 16
        FractionLength: 13

        RoundingMethod: Floor
        OverflowAction: Saturate
           ProductMode: FullPrecision
               SumMode: FullPrecision
b = removefimath(b)
b = 

    3.1415

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 16
        FractionLength: 13
isfimathlocal(b)
ans =

  logical

   0

See Also

| | | | |