主要内容

本页采用了机器翻译。点击此处可查看最新英文版本。

MATLAB 类和函数映射到 C++

本指南详细介绍了 MATLAB® 类和函数如何映射到其相应的 C++ 类和函数。通过涉及 MATLAB 类 MyPositionMyRectangle(属于包 +shapes 的一部分)以及 MATLAB 函数 calculatearea 的示例,我们探索了由形成 MATLAB 和 C++ 之间接口的 compiler.build.cppSharedLibrary 函数生成的 C++ 头文件 (libshapesv2.hpp)。

从高层次来看:

  • MATLAB 数据类型根据 argumentsproperties 模块中的类型规范映射到 C++ 数据类型。

  • MATLAB 包映射到同名的 C++ 命名空间。

  • MATLAB 类映射到同名的 C++ 类。

  • MATLAB 类的公共方法映射到同名的公共 C++ 方法。

  • MATLAB 类的属性在 C++ 类中被复制为 getter 和 setter 函数。属性名称前面加上 getset 来形成 getter 和 setter 函数。

 +shapes(包)

 MyPosition.m(类)

 MyRectangle.m(类)

 calculatearea.m(函数)

 libshapesv2.hpp(C++ 头文件)

标头映射摘录

classdef MyRectangle

    properties
        UpperLeft  (1,1) shapes.MyPosition
        LowerRight (1,1) shapes.MyPosition
    end
    methods
        function R = enlarge(R, n)
            arguments
                R (1,1) shapes.MyRectangle
                n (1,1) double {mustBeReal}
            end
            % code
        end
        function R = show(R)
            arguments
                R (1,1) shapes.MyRectangle
            end
            % code
        end
    end
end
namespace shapes {
    class MyRectangle : public MATLABObject<MATLABControllerType> { 
    public:

        // constructors
        MyRectangle : MATLABObject() {}

        // code

        // getter and setter property methods
        shapes::MyPosition getUpperLeft() {
            // code
        }
        void setUpperLeft(shapes::MyPosition obj) {
            // code
        }
        shapes::MyPosition getLowerRight() {
            // code
        }
        void setLowerRight(shapes::MyPosition obj) {
            // code
        }

        // public methods
        matlab::data::Array show() { 
            // code
        }

        matlab::data::Array enlarge(double n) { 
            // code
        }

    };
}

MATLAB 包、类和函数

+shapes

这是一个包含两个类的 MATLAB 包:MyPositionMyRectangle

MyPosition

此类是 shapes 包的一部分,表示二维空间中的位置。它包含两个属性:XY,都是实数 double 精度值。

MyRectangle

该类也是 shapes 包的一部分,它代表一个矩形。它包含两个属性:UpperLeftLowerRight,它们是 shapes.MyPosition 类的实例。该类还有两个方法:enlargeshowenlarge 方法将矩形放大 n 倍,调整 UpperLeftLowerRight 位置。show 方法显示 UpperLeftLowerRight 位置的当前值。

calculatearea

函数 calculateareashapes.MyRectangle 的实例作为参量并计算矩形的面积。

在 C++ 中映射

C++ 头文件为 MATLAB MyPositionMyRectangle 类以及 MATLAB 函数 calculatearea 提供了接口。这允许 C++ 和 MATLAB 之间的交互。

namespace shapes

MATLAB shapes 包映射到同名的 C++ 命名空间。

shapes::MyPosition

C++ shapes::MyPosition 类代表 MATLAB MyPosition 类。它包括属性 XY 的 getter 和 setter 方法,这些方法与 MATLAB 类中的属性相对应。它们是:getXsetXgetYsetY。构造函数创建 MATLAB MyPosition 类的实例。

shapes::MyRectangle

C++ shapes::MyRectangle 类代表 MATLAB MyRectangle 类。它包括属性 UpperLeftLowerRight 的 getter 和 setter 方法,这些方法与 MATLAB 类中的属性相对应。它们是:getUpperLeftsetUpperLeftgetLowerRightsetLowerRight。构造函数创建 MATLAB MyRectangle 类的实例。

shapes::MyRectangle 类还包括显示和放大的方法,它们与 MATLAB 类中的方法相对应。这些方法利用 MATLABControllerType 实例通过 feval 方法执行 MATLAB 函数。

calculatearea

calculatearea 函数对应 MATLAB calculatearea 函数。该函数以 shapes::MyRectangle 的实例作为参量并计算矩形的面积。通过使用 calculateareafeval 方法调用 MATLAB MATLABControllerType 函数来执行计算。

另请参阅

| |

主题