Fixed-Point Code for MATLAB Classes
R2026bAutomated Conversion Support for MATLAB Classes
The automated fixed-point 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.
Supports class methods, properties, constant properties, and specializations. For each specialization of a class,
class_name, the conversion generates a separateclass_name_fixpt.mfile. For every instantiation of a class, the generated fixed-point code contains a call to the constructor of the appropriate specialization.Supports classes that inherit from
handle,matlab.System, orhdl.BlackBox.Supports constructors that use
narginandvarargin.Supports classes that have
getandsetmethods such asget.PropertyName,set.PropertyName. These methods are called when properties are read or assigned. Thesetmethods can be specialized.
Unsupported Constructs
The automated conversion process does not support:
Inheritance from user-defined classes.
Classes defined within namespaces (packages).
handleclass destructors (deletemethods).Property validation functions.
Local functions called from inside class definitions.
vararginandvarargoutat the entry-point function level.
Coding Style Best Practices
When you write MATLAB code that uses MATLAB classes:
Initialize properties with scalar numeric values that can be evaluated statically.
Avoid initializing properties with expressions that cannot be evaluated at parse time.
For example, this Counter class is compatible with
the automated fixed-point conversion process.
classdef Counter < handle properties Value = 0; end properties(Constant) MAX_VALUE = 128 end methods 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
You can also use constructors with nargin to provide
default values for properties. When you use this pattern, call the constructor
with all arguments in your design function so that the conversion can determine
the types of all input arguments.
classdef Counter < handle properties Value; Increment; end properties(Constant) MAX_VALUE = 128 end methods function this = Counter(startVal, inc) if nargin < 1 startVal = 0; end if nargin < 2 inc = 1; end this.Value = startVal; this.Increment = inc; end function out = next(this) out = this.Value; if this.Value == this.MAX_VALUE this.Value = 0; else this.Value = this.Value + this.Increment; end end end end
function out = counter_design() persistent c; if isempty(c) c = Counter(0, 1); end out = c.next(); end
Exclude Functions from Conversion
If your design calls functions that do not need fixed-point conversion, use
coder.float2fixed.skip
to exclude them from the conversion process. Place the pragma at the beginning of the
entry-point function or the calling function. The skipped functions remain as
floating-point code in the generated output, and the conversion process casts their
return values to the appropriate fixed-point types at the call site.
This is useful for initialization code that computes coefficients using floating-point math (such as transcendental functions) that does not need to be converted to fixed point. For example, this design uses a helper function to convert a gain value from decibels to linear scale:
function out = apply_gain(in) coder.float2fixed.skip({'compute_gain'}); persistent g; if isempty(g) g = compute_gain(6.0); end out = in * g; end function g = compute_gain(dB) g = 10^(dB/20); end
In the generated fixed-point code, compute_gain remains as
floating-point code. The conversion process casts the result to fixed point when it is
assigned to the persistent variable g.