Documentation

coder.ClassType class

Package: coder
Superclasses: coder.ArrayType

Represent set of MATLAB classes

Description

Specifies the set of value class objects that the generated code can accept. Use only with the fiaccel -args option. Do not pass as an input to a generated MEX function.

Construction

t = coder.typeof(value_class_object) creates a coder.ClassType object for the object value_class_object.

t = coder.newtype(value_class_name) creates a coder.ClassType object for an object of the class value_class_name.

Input Arguments

value_class_object

Value class object from which to create the coder.ClassType object. value_class_object is an expression that evaluates to an object of a value class. For example:

v = myValueClass;
t = coder.typeof(v);

t = coder.typeof(myValueClass(2,3));

value_class_name

Name of a value class definition file on the MATLAB® path. Specify as a character vector or string scalar. For example:

t = coder.newtype('myValueClass');

Properties

When you create a coder.ClassType object by using coder.typeof, the coder.ClassType object has the same properties as the object from which it is constructed.

Copy Semantics

Value. To learn how value classes affect copy operations, see Copying Objects (MATLAB).

Examples

Create Type Based on Example Object

Create a type based on an example object in the workspace.

Create a value class myRectangle.

classdef myRectangle
    properties
        length;
        width;
    end
    methods
        function obj = myRectangle(l,w)
            if nargin > 0
                obj.length = l;
                obj.width = w;
            end
        end
        function area = calcarea(obj)
            area = obj.length * obj.width;
        end
    end
end

Create a function that takes an object of myRectangle as an input.

function z = getarea(r)
%#codegen
z = calcarea(r);
end

Create an object of myRectangle.

v = myRectangle(1,2)
v = 

  myRectangle with properties:

    length: 1
     width: 2

Create a coder.ClassType object based on v.

t = coder.typeof(v)
t = 

coder.ClassType
   1×1 myRectangle   
      length: 1×1 double
      width : 1×1 double

coder.typeof creates a coder.ClassType object that has the same properties names and types as v has.

Generate code for getarea. Specify the input type by passing the coder.ClassType object, t, to the -args option.

codegen getarea -args {t} -report

Create Type by Using coder.newtype

Create a coder.ClassType object for an object of the value class mySquare by using coder.newtype.

Create value class mySquare that has one property, side.

classdef mySquare
    properties
        side;
    end
    methods
        function obj = mySquare(val)
            if nargin > 0
                obj.side = val;
            end
        end
        function a = calcarea(obj)
            a = obj.side * obj.side;
        end
    end
end

Create a coder.ClassType type for mySquare.

t = coder.newtype('mySquare')

Specify the type of side.

t.Properties.side = coder.typeof(2)

Tips

  • After you create a coder.ClassType, you can modify the types of the properties. For example:

    t = coder.typeof(myClass)
    t.Properties.prop1 = coder.typeof(int16(2));
    t.Properties.prop2 = coder.typeof([1 2 3]);

  • After you create a coder.ClassType, you can add properties. For example:

    t = coder.typeof(myClass)
    t.Properties.newprop1 = coder.typeof(int8(2));
    t.Properties.newprop2 = coder.typeof([1 2 3]);

  • When you generate code, the properties of the coder.ClassType object that you pass to codegen must be consistent with the properties in the class definition file. However, if the class definition file has properties that your code does not use, the coder.ClassType object does not have to include those properties. The code generator removes properties that you do not use.

Introduced in R2017a

Was this topic helpful?