Generate Code for MATLAB Value Classes

This example shows how to generate code for a MATLAB® value class and then view the generated code in the code generation report.

  1. In a writable folder, create a MATLAB value class, Shape. Save the code as Shape.m.

    classdef Shape 
    % SHAPE Create a shape at coordinates 
    % centerX and centerY
        properties
            centerX;
            centerY;
        end
        properties (Dependent = true)
            area;
        end
        methods 
            function out = get.area(obj)
                out =  obj.getarea();
            end
            function obj = Shape(centerX,centerY)
                obj.centerX = centerX;
                obj.centerY = centerY;
            end
        end
        methods(Abstract = true)
            getarea(obj);
        end
        methods(Static)
            function d = distanceBetweenShapes(shape1,shape2)
                xDist = abs(shape1.centerX - shape2.centerX);
                yDist = abs(shape1.centerY - shape2.centerY);
                d = sqrt(xDist^2 + yDist^2);
            end
        end
    end  
  2. In the same folder, create a class, Square, that is a subclass of Shape. Save the code as Square.m.

    classdef Square < Shape 
    % Create a Square at coordinates center X and center Y 
    % with sides of length of side
        properties
            side;
        end
        methods
            function obj = Square(side,centerX,centerY)
                obj@Shape(centerX,centerY);
                obj.side = side;
            end
            function Area = getarea(obj)
                Area = obj.side^2;
            end
        end
    end
  3. In the same folder, create a class, Rhombus, that is a subclass of Shape. Save the code as Rhombus.m.

    classdef Rhombus < Shape
        properties
            diag1;
            diag2;
        end
        methods
            function obj = Rhombus(diag1,diag2,centerX,centerY)
                obj@Shape(centerX,centerY);
                obj.diag1 = diag1;
                obj.diag2 = diag2;
            end
            function Area = getarea(obj)
                Area = 0.5*obj.diag1*obj.diag2;
            end
        end
    end
  4. Write a function that uses this class.

    function [TotalArea, Distance] =   use_shape
    %#codegen
    s = Square(2,1,2);
    r = Rhombus(3,4,7,10);
    TotalArea  = s.area + r.area;
    Distance = Shape.distanceBetweenShapes(s,r);  
  5. Generate a static library for use_shape and generate a code generation report.

    codegen -config:lib -report use_shape

    codegen generates a C static library with the default name, use_shape, and supporting files in the default folder, codegen/lib/use_shape.

  6. Click the View report link.

  7. In the report, on the MATLAB code tab, click the link to the Rhombus class.

    The report displays the class definition of the Rhombus class and highlights the class constructor. On the Variables tab, it provides details of the variables used in the class. If a variable is a MATLAB object, by default, the report displays the object without displaying its properties. To view the list of properties, expand the list. Within the list of properties, the list of inherited properties is collapsed. In the following report, the lists of properties and inherited properties are expanded.

  8. At the top right side of the report, expand the Calls list.

    The Calls list shows that there is a call to the Rhombus constructor from use_shape and that this constructor calls the Shape constructor.

  9. The constructor for the Rhombus class calls the Shape method of the base Shape class: obj@Shape. In the report, click the Shape link in this call.

    The link takes you to the Shape method in the Shape class definition.

Was this topic helpful?