主要内容

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

将 MATLAB 类和函数映射到 C++

R2026b

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

从高层次来看:

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

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

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

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

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

+shapes
   MyPosition.m
   MyRectangle.m
classdef MyPosition
    properties
        X (1,1) double {mustBeReal}
        Y (1,1) double {mustBeReal}
    end
end
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
            R.UpperLeft.X = R.UpperLeft.X - n;
            R.UpperLeft.Y = R.UpperLeft.Y - n;
            R.LowerRight.X = R.LowerRight.X + n;
            R.LowerRight.Y = R.LowerRight.Y + n;
        end
        function R = show(R)
            arguments
                R (1,1) shapes.MyRectangle
            end
            disp([R.UpperLeft.X R.UpperLeft.Y R.LowerRight.X R.LowerRight.Y]);
        end
    end
end
function r = calculatearea(rect)

arguments
    rect (1,1) shapes.MyRectangle
end

r = (rect.LowerRight.Y - rect.UpperLeft.Y) * (rect.LowerRight.X - rect.UpperLeft.X);

end
#include "MatlabTypesInterface.hpp"
#include <map>

namespace shapes {
    class MyPosition : public MATLABObject<MATLABControllerType> {
    public:

        // constructors
        MyPosition() : MATLABObject() {}

        MyPosition(std::shared_ptr<MATLABControllerType> matlabPtr) :
            MATLABObject(matlabPtr, u"shapes.MyPosition")
        {}

        MyPosition(std::shared_ptr<MATLABControllerType> matlabPtr, 
            std::vector<matlab::data::Array> _args) {
            m_matlabPtr = matlabPtr;
            matlab::data::Array _result = MATLABCallOneOutputMethod(u"shapes.MyPosition", _args);
            m_object = _result;
        }

        MyPosition(std::shared_ptr<MATLABControllerType> matlabPtr, matlab::data::Array obj) :
            MATLABObject(matlabPtr, obj)
        {}

        // properties
        double getX() { return MATLABGetScalarProperty<double>(u"X"); }
        void setX(double value) { return MATLABSetScalarProperty(u"X", value); }
        double getY() { return MATLABGetScalarProperty<double>(u"Y"); }
        void setY(double value) { return MATLABSetScalarProperty(u"Y", value); }

        // methods
    };
}

namespace shapes {
    class MyRectangle : public MATLABObject<MATLABControllerType> {
    public:

        // constructors
        MyRectangle() : MATLABObject() {}

        MyRectangle(std::shared_ptr<MATLABControllerType> matlabPtr) :
            MATLABObject(matlabPtr, u"shapes.MyRectangle")
        {}

        MyRectangle(std::shared_ptr<MATLABControllerType> matlabPtr, 
            std::vector<matlab::data::Array> _args) {
            m_matlabPtr = matlabPtr;
            matlab::data::Array _result = MATLABCallOneOutputMethod(u"shapes.MyRectangle", _args);
            m_object = _result;
        }

        MyRectangle(std::shared_ptr<MATLABControllerType> matlabPtr, matlab::data::Array obj) :
            MATLABObject(matlabPtr, obj)
        {}

        // properties
        shapes::MyPosition getUpperLeft() {
            matlab::data::Array obj = MATLABGetScalarProperty<matlab::data::Array>(u"UpperLeft");
            return shapes::MyPosition(m_matlabPtr, obj);
        }
        void setUpperLeft(shapes::MyPosition obj) {
            MATLABSetScalarProperty(u"UpperLeft", matlab::data::Array(obj));
        }
        shapes::MyPosition getLowerRight() {
            matlab::data::Array obj = MATLABGetScalarProperty<matlab::data::Array>(u"LowerRight");
            return shapes::MyPosition(m_matlabPtr, obj);
        }
        void setLowerRight(shapes::MyPosition obj) {
            MATLABSetScalarProperty(u"LowerRight", matlab::data::Array(obj));
        }

        // methods
    private:
        template<size_t nargout>
        struct return_type_show { typedef void type; };

    public:
        template<size_t nargout = 1>
        typename return_type_show<nargout>::type show() {
            static_assert(nargout <= 1, "Too many outputs specified. Maximum outputs is 1.");
        }

    private:
        template<size_t nargout>
        struct return_type_enlarge { typedef void type; };

    public:
        template<size_t nargout = 1>
        typename return_type_enlarge<nargout>::type enlarge(double n) {
            static_assert(nargout <= 1, "Too many outputs specified. Maximum outputs is 1.");
        }

    };
    template<>
    struct MyRectangle::return_type_show<0> { typedef void type; };

    template<>
    struct MyRectangle::return_type_show<1> { typedef matlab::data::Array type; };

    template<>
    void MyRectangle::show<0>() {
        matlab::data::ArrayFactory _arrayFactory;
        std::vector<matlab::data::Array> _args = {
            m_object };
        m_matlabPtr->feval(u"show", 0, _args);
    }

    template<>
    matlab::data::Array MyRectangle::show<1>() {
        matlab::data::ArrayFactory _arrayFactory;
        std::vector<matlab::data::Array> _args = {
            m_object };
        matlab::data::Array _result_mda = m_matlabPtr->feval(u"show", _args);
        matlab::data::Array _result;
        _result = _result_mda;
        return _result;
    }

    template<>
    struct MyRectangle::return_type_enlarge<0> { typedef void type; };

    template<>
    struct MyRectangle::return_type_enlarge<1> { typedef matlab::data::Array type; };

    template<>
    void MyRectangle::enlarge<0>(double n) {
        matlab::data::ArrayFactory _arrayFactory;
        std::vector<matlab::data::Array> _args = {
            m_object,
            _arrayFactory.createArray<double>({1,1}, {n}) };
        m_matlabPtr->feval(u"enlarge", 0, _args);
    }

    template<>
    matlab::data::Array MyRectangle::enlarge<1>(double n) {
        matlab::data::ArrayFactory _arrayFactory;
        std::vector<matlab::data::Array> _args = {
            m_object,
            _arrayFactory.createArray<double>({1,1}, {n}) };
        matlab::data::Array _result_mda = m_matlabPtr->feval(u"enlarge", _args);
        matlab::data::Array _result;
        _result = _result_mda;
        return _result;
    }
}

template<size_t nargout>
struct return_type_calculatearea { typedef void type; };

template<size_t nargout = 1>
typename return_type_calculatearea<nargout>::type calculatearea(
    std::shared_ptr<MATLABControllerType> _matlabPtr, shapes::MyRectangle rect) {
    static_assert(nargout <= 1, "Too many outputs specified. Maximum outputs is 1.");
}
template<>
struct return_type_calculatearea<0> { typedef void type; };

template<>
struct return_type_calculatearea<1> { typedef matlab::data::Array type; };

template<>
void calculatearea<0>(std::shared_ptr<MATLABControllerType> _matlabPtr, 
    shapes::MyRectangle rect) {
    matlab::data::ArrayFactory _arrayFactory;
    std::vector<matlab::data::Array> _args = { rect };
    _matlabPtr->feval(u"calculatearea", 0, _args);
}

template<>
matlab::data::Array calculatearea<1>(std::shared_ptr<MATLABControllerType> _matlabPtr, 
    shapes::MyRectangle rect) {
    matlab::data::ArrayFactory _arrayFactory;
    std::vector<matlab::data::Array> _args = { rect };
    matlab::data::Array _result_mda = _matlabPtr->feval(u"calculatearea", _args);
    matlab::data::Array _result;
    _result = _result_mda;
    return _result;
}

标头映射摘录

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 包:MyPosition 和 MyRectangle。

MyPosition

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

MyRectangle

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

calculatearea

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

在 C++ 中映射

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

namespace shapes

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

shapes::MyPosition

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

shapes::MyRectangle

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

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

calculatearea

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

另请参阅

| |

主题