Contents

Fixed-Point Code for MATLAB Classes

Automated Conversion Support for MATLAB Classes

The automated conversion process:

  • Proposes fixed-point data types based on simulation ranges for MATLAB® classes. It does not propose data types based on derived ranges for MATLAB classes.

    After simulation, the Fixed-Point Converter app:

    • Function list contains class constructors, methods, and specializations.

    • Code window displays the objects used in each function.

    • Provides code coverage for methods.

    For more information, see Viewing Information for MATLAB Classes.

  • Supports class methods, properties, and specializations. For each specialization of a class, class_name, the conversion generates a separate class_name_fixpt.m file. For every instantiation of a class, the generated fixed-point code contains a call to the constructor of the appropriate specialization.

  • Supports classes that have get and set methods such as get.PropertyName, set.PropertyName. These methods are called when properties are read or assigned. The set methods can be specialized. Sometimes, in the generated fixed-point code, assignment statements are transformed to function calls.

Unsupported Constructs

The automated conversion process does not support:

  • Class inheritance.

  • Packages.

  • Constructors that use nargin and varargin.

Coding Style Best Practices

When you write MATLAB code that uses MATLAB classes:

  • Initialize properties in the class constructor.

  • Replace constant properties with static methods.

For example, consider the counter class.

classdef Counter < handle
  properties
    Value = 0;
  end

  properties(Constant)
    MAX_VALUE = 128
  end
  
  methods
    function out = next(this)
      out = this.Count;
      if this.Value == this.MAX_VALUE
        this.Value = 0;
      else
        this.Value = this.Value + 1;
      end
    end
  end
end

To use the automated fixed-point conversion process, rewrite the class to have a static class that initializes the constant property MAX_VALUE and a constructor that initializes the property Value.

classdef Counter < handle
  properties
    Value;
  end

  methods(Static)
    function t = MAX_VALUE()
      t = 128;
    end
  end
  
  methods
    function this = Counter()
      this.Value = 0;
    end 
    function out = next(this)
      out = this.Value;
      if this.Value == this.MAX_VALUE
        this.Value = 0;
      else
        this.Value = this.Value + 1;
      end
    end
  end
end
Was this topic helpful?