Main Content

coder.ClassType Class

Namespace: coder
Superclasses: coder.ArrayType

Represent set of MATLAB classes acceptable for input specification

Description

Objects of the coder.ClassType specify value class objects that the generated code accepts. Use objects of this class only with the -args option of the fiaccel function. Do not pass as an input to a generated MEX function.

Creation

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

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

Note

You can create and edit coder.Type objects interactively by using the Coder Type Editor. See Create and Edit Input Types by Using the Coder Type Editor.

Input Arguments

expand all

Value class object for which to create the coder.ClassType object. This input is an expression that evaluates to an object of a value class.

Name of a value class definition file on the MATLAB path specified as a character vector or string scalar.

Properties

When you create a coder.ClassType object t by passing a value class object v to coder.typeof, t has same as the properties as v with the Constant attribute set to false.

Similarly, when you create a coder.ClassType object t by passing the name of the value class object, v to coder.newtype, t has same as the properties as v with the Constant attribute set to false.

Examples

collapse all

This example shows how to create a type object based on an example object in the workspace.

Create a value class myRectangle.

type myRectangle.m
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 the input.

type getarea.m
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   
      Properties : 
      	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.

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

codegen getarea -args {t} -report
Code generation successful: To view the report, open('codegen/mex/getarea/html/report.mldatx')

This example shows how to create a coder.ClassType object for an object of the value class mySquare by using coder.newtype.

Create a value class mySquare that has one property, side.

type mySquare.m
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 without assigning any property values.

t = coder.newtype('mySquare')
t = 
coder.ClassType
   1×1 mySquare -- class with no properties

To ensure that t has the properties of mySquare, specify the type of side by using t.Properties.

t.Properties.side = coder.typeof(2)
t = 
coder.ClassType
   1×1 mySquare   
      Properties : 
      	side : 1×1 double

Tips

  • After you create a coder.ClassType, you can modify the types of the properties. For example, modify the type of the prop1 and prop2 properties of an object t:

    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 object, you can add properties. For example, add the newprop1 and newprop2 properties of an object t:

    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 the codegen function 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 ignores properties that your code does not use.

Version History

Introduced in R2017a