Generate HLS Code for MATLAB Value Classes
This guide offers a comprehensive step-by-step approach to generating code for MATLAB® value classes. By following this example, you will learn how to create a class hierarchy, implement functionality, and review the generated code using the code generation report. This process not only enhances your understanding of MATLAB's capabilities but also equips you with the skills to optimize code for performance and integration in larger applications.
In MATLAB®, value classes are a type of class that defines how objects are copied and passed with in the code. This example shows how to generate HLS code for a MATLAB value class and then view the generated code in the code generation report.
Follow these steps to generate code for value classes:
In a writable folder, create a MATLAB value class named
Shapeand save the code asShape.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
Create a class named
Squarein the same folder, making it a subclass ofShape. Save this code asSquare.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
Create another class named
Rhombus, also a subclass ofShape, and save it asRhombus.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
Write a function
use_shapethat uses these classes to demonstrate their functionality.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);
Generate HLS Code for
use_shapeand generate a code generation report.cfg = coder.config('hdl'); cfg.Workflow = 'High Level Synthesis'; codegen -config cfg use_shape -reportClick the
View reportlink to open the code generation report.In the MATLAB Source pane, locate
Rhombus.mand click onRhombusto highlight the constructor of theRhombusclass.Click the Variables tab. You see that the variable
objis an object of theRhombusclass. To see its properties, expandobj.
In the MATLAB Source pane, click Call Tree.
The Call Tree view shows that
use_shapecalls theRhombusconstructor and that theRhombusconstructor calls theShapeconstructor.
In the code pane, in the
Rhombusclass constructor, move your pointer to this line:Theobj@Shape(centerX,centerY)Rhombusclass constructor calls theShapemethod of the baseShapeclass. To view theShapeclass definition, inobj@Shape, double-clickShape.