Fixed-Point Code for MATLAB Classes - MATLAB & Simulink (original) (raw)
Main Content
Automated 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, and specializations. For each specialization of a class,
class_name
, the conversion generates a separateclass_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
andset
methods such asget.PropertyName
,set.PropertyName
. These methods are called when properties are read or assigned. Theset
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.
- Namespaces.
- Constructors that use
nargin
andvarargin
.
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.Value; 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 propertyValue
.
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