主要内容

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

将 MATLAB 类和函数映射到 C#

R2026b

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

从高层次来看:

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

  • MATLAB 包映射到同名的 C# 子命名空间。根命名空间对应于程序集的名称。

  • MATLAB 类映射到同名的 C# 结构。C# 代码文件使用以下命名模式生成:<MatlabPackageName>_<MatlabClassName>.cs。

  • MATLAB 类的公共方法映射到同名 C# 结构的公共方法。

  • MATLAB 类的属性映射到 C# 结构中同名的属性。

+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 (Input)
                R (1,1) shapes.MyRectangle
                n (1,1) double {mustBeReal}
            end
            arguments (Output)
                R (1,1) shapes.MyRectangle
            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(['Point 1 = ' ...
                '(' num2str(R.UpperLeft.X), ', ' num2str(R.UpperLeft.Y) ')' newline])
            disp(['Point 2 = ' ...
                '(' num2str(R.LowerRight.X), ', ' num2str(R.LowerRight.Y) ')' newline])
            disp(['Rectangle (', ...
                num2str(R.UpperLeft.X), ', ', ...
                num2str(R.UpperLeft.Y), ') -> (', ...
                num2str(R.LowerRight.X), ', ', ...
                num2str(R.LowerRight.Y), ')' newline])
        end
    end
end
function r = calculatearea(rect)

arguments (Input)
    rect (1,1) shapes.MyRectangle
end

arguments (Output)
    r (1,1) double {mustBeReal}
end

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

end

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# 映射摘录

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 CalculateArea.shapes{
public  struct MyRectangle { 
    ...
    ...
    private dynamic _objrep;
    private dynamic _matlab;
    public  MyRectangle(MATLABProvider _matlab){
        this._matlab = _matlab;
        _objrep = (MATLABArray)this._matlab.shapes.MyRectangle(new RunOptions(nargout:1));
    }
    public CalculateArea.shapes.MyPosition UpperLeft {
        get => (MATLABObject) _matlab.matlab.@internal.engine.getProperty(_objrep, "UpperLeft");
        set => _objrep = _matlab.matlab.@internal.engine.setProperty(_objrep,"UpperLeft",(MATLABObject)value);
    }
    public CalculateArea.shapes.MyPosition LowerRight {
        get => (MATLABObject) _matlab.matlab.@internal.engine.getProperty(_objrep, "LowerRight");
        set => _objrep = _matlab.matlab.@internal.engine.setProperty(_objrep,"LowerRight",(MATLABObject)value);
    }
    public void show(){
        _objrep.show(new RunOptions(nargout:0));
    }
    public void show( out dynamic _R){
        _R = (MATLABArray)_objrep.show(new RunOptions(nargout:1));
    }
    public void enlarge(double n){
        _objrep.enlarge(new RunOptions(nargout:0),n);
    }
    public void enlarge(double n, out MyRectangle R)
        {
        R = (CalculateArea.shapes.MyRectangle)_objrep.enlarge(new RunOptions(nargout:1),n);
    }
   ...

}

在 C# 中映射

namespace CalculateArea.shapes

MATLAB 包 shapes 映射到 C# 代码中的命名空间 CalculateArea.shapes。此处的根命名空间 CalculateArea 源自执行 compiler.build.dotnetAssembly 时指定的程序集名称。

struct MyPosition

  • MATLAB 类 MyPosition 映射到同名的 C# 结构。

  • MATLAB 类 X 中的 Y 和 MyPosition 属性直接映射到 C# 结构中同名的属性。数据类型延续。

  • C# 结构定义了一个构造函数,该构造函数接受 MATLABProvider 对象并使用 MATLAB Runtime 初始化一个新的 MyPosition 实例。

  • C# 结构包含在 MyPosition 和 MATLABObject 之间进行转换的隐式运算符。这样就可以在任何需要 MyPosition 对象的地方使用 MATLABObject 对象,反之亦然。

struct MyRectangle

  • MATLAB 类 MyRectangle 映射到同名的 C# 结构。

  • MATLAB 类中的属性 UpperLeft 和 LowerRight 属于 shapes.MyPosition 类型,直接映射到 C# 结构中同名的属性,这些属性属于 CalculateArea.shapes.MyPosition 类型。数据类型延续。

  • C# 结构定义了一个构造函数,该构造函数接受 MATLABProvider 对象并使用 MATLAB Runtime 初始化一个新的 MyRectangle 实例。

  • 方法:

    • a.为了 enlarge,MATLAB 方法采用 double n 作为参量并修改 UpperLeft 和 LowerRight 属性。C# 结构体具有一个 enlarge 方法,该方法也以 double n 作为参量,并修改 C# 结构的 UpperLeft 和 LowerRight 属性。

    • b.对于 show,MATLAB 方法显示 UpperLeft 和 LowerRight 坐标。C# 结构也有一个 show 方法来执行相同的操作。

  • C# 结构包含在 MyRectangle 和 MATLABObject 之间进行转换的隐式运算符。这样就可以在任何需要 MyRectangle 对象的地方使用 MATLABObject 对象,反之亦然。

calculatearea

在 C# 中,提供了两个名为 calculatearea 的方法,作为 MATLAB 函数的包装器。这些方法旨在通过 MATLABProvider 的实例调用 MATLAB 函数。

第一种方法 calculatearea(MATLABProvider _matlab, CalculateArea.shapes.MyRectangle rect) 不期望从 MATLAB 函数返回值。

第二种方法 calculatearea(MATLABProvider _matlab, CalculateArea.shapes.MyRectangle rect, out double r) 期望从 MATLAB 函数返回一个值,这与 MATLAB 函数一致。使用 out 参数 r 将计算出的面积传递出该方法。

在这两种 C# 方法中,shapes.MyRectangle 参数 rect 在传递给 MATLAB 函数之前被强制转换为 MATLABObject。这表明 shapes.MyRectangle 可以转换为兼容的 MATLAB 数据类型。

另请参阅

主题