Main Content

Mapping MATLAB Classes and Functions to C#

This guide details how MATLAB® classes and functions map to their corresponding C# classes and functions. Through the lens of an example involving MATLAB classes named MyPosition and MyRectangle that are part of package +shapes, and a MATLAB function called calculatearea, we explore the C# code files generated by the compiler.build.dotNETAssembly function that form the interface between MATLAB and C#.

At a high level:

  • MATLAB data types are mapped to C# data types based on type specification in arguments and properties blocks.

  • MATLAB packages are mapped to C# sub-namespaces of the same name. The root namespace corresponds to the name of the assembly.

  • MATLAB classes are mapped to C# structs of the same name. The C# code file is generated with the following naming pattern: <MatlabPackageName>_<MatlabClassName>.cs.

  • Public methods of MATLAB classes map to public methods of the C# struct of the same name.

  • Properties of a MATLAB class are mapped to properties of the same name in the C# struct.

 +shapes (Package)

 MyPosition.m (Class)

 MyRectangle.m (Class)

 calculatearea.m (Function)

MATLAB Package, Classes, and Function

+shapes

This is a MATLAB package containing two classes: MyPosition and MyRectangle.

MyPosition

This class is part of the shapes package and represents a position in two-dimensional space. It contains two properties: X and Y, both are real double values.

MyRectangle

This class is also part of the shapes package and it represents a rectangle. It contains two properties: UpperLeft and LowerRight which are instances of the shapes.MyPosition class. The class also has two methods: enlarge and show. The enlarge method enlarges the rectangle by a factor n, adjusting the UpperLeft and LowerRight positions. The show method displays the current values of UpperLeft and LowerRight positions.

calculatearea

The function calculatearea takes an instance of shapes.MyRectangle as an argument and calculates the area of the rectangle.

C# Mapping Excerpt

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);
    }
   ...

}

Mapping in C#

namespace CalculateArea.shapes

The MATLAB package shapes maps to the namespace CalculateArea.shapes in the C# code. Here the root namespace CalculateArea is derived from the assembly name specified when executing compiler.build.dotnetAssembly.

struct MyPosition

  • MATLAB class MyPosition is mapped to a C# struct of the same name.

  • The X and Y properties in the MATLAB class MyPosition map directly to properties of the same name in the C# struct. The data type carries over.

  • The C# struct defines a constructor that accepts a MATLABProvider object and initializes a new MyPosition instance using MATLAB Runtime.

  • The C# struct includes implicit operators to convert between MyPosition and MATLABObject. This allows MyPosition objects to be used wherever MATLABObject objects are expected, and vice versa.

struct MyRectangle

  • MATLAB class MyRectangle is mapped to a C# struct of the same name.

  • The properties UpperLeft and LowerRight in the MATLAB class, which are of type shapes.MyPosition, map directly to properties of the same name in the C# struct, which are of type CalculateArea.shapes.MyPosition. The data types carry over.

  • The C# struct defines a constructor that accepts a MATLABProvider object and initializes a new MyRectangle instance using MATLAB Runtime.

  • Methods:

    • a. For enlarge, the MATLAB method takes a double n as an argument and modifies the UpperLeft and LowerRight properties. The C# struct has an enlarge method that also takes a double n as an argument and modifies the UpperLeft and LowerRight properties of the C# struct.

    • b. For show, the MATLAB method displays the UpperLeft and LowerRight coordinates. The C# struct also has a show method to perform the same operation.

  • The C# struct includes implicit operators to convert between MyRectangle and MATLABObject. This allows MyRectangle objects to be used wherever MATLABObject objects are expected, and vice versa.

calculatearea

In C#, two methods named calculatearea are provided, which serve as wrappers for the MATLAB function. These methods are designed to call the MATLAB function via an instance of MATLABProvider.

The first method, calculatearea(MATLABProvider _matlab, CalculateArea.shapes.MyRectangle rect), does not expect a return value from the MATLAB function.

The second method, calculatearea(MATLABProvider _matlab, CalculateArea.shapes.MyRectangle rect, out double r), expects one return value from the MATLAB function, which is consistent with the MATLAB function. The calculated area is passed out of the method using the out parameter r.

In both C# methods, the shapes.MyRectangle parameter rect is cast to a MATLABObject before it is passed to the MATLAB function. This indicates that shapes.MyRectangle is convertible to a compatible MATLAB data type.

See Also

Related Topics