Contenido principal

MathWorks.MATLAB.Types.MATLABProvider

R2026b

.NET base class for invoking MATLAB functions

Since R2023b

Description

Dynamic class to represent instances of MathWorks.MATLAB.Engine.MATLABEngine or MathWorks.MATLAB.Runtime.MATLABRuntime (MATLAB Compiler SDK) classes in .NET applications. The MATLABProvider class enables data conversion between MATLAB and .NET.

The matlab.engine.typedinterface.generateCSharp function, called explicitly or implicitly, generates signatures for object MATLABProvider so that you can use the same source code to build either a .NET application or a deployed .NET application to call a MATLAB function.

Class Details

Namespace:

MathWorks.MATLAB.Types

Examples

Pass MATLABProvider to Position Constructor

Create .NET application code for this MATLAB® Position class.

classdef Position
    properties
        X (1,1) double {mustBeReal}
        Y (1,1) double {mustBeReal}
    end

    methods
        function P = show(P)
            arguments
                P (1,1) Position
            end

            fprintf('Position: (%d, %d)\n', P.X, P.Y);
        end
    end
end

Generate the C# wrapper file for the function.

matlab.engine.typedinterface.generateCSharp("PositionApp",Classes="Position")
/* File: Position.cs
*
* MATLAB Strongly Typed Interface Version: R2023b
* C# source code generated on: 08-Jun-2023
*/
using System;
using MathWorks.MATLAB.Types;
using MathWorks.MATLAB.Exceptions;

public  struct Position { 
    private void ThrowIfDefaultValue()
    {
        if (_matlab == null || _objrep == null)
        {
            throw new UnsupportedTypeException("Position object is not initialized.");
        }
    }
    private dynamic _objrep;
    private dynamic _matlab;
    public  Position(MATLABProvider _matlab){
        this._matlab = _matlab;
        _objrep = (MATLABArray)this._matlab.Position(new RunOptions(nargout:1));
    }
    public double X {
        get => _matlab.matlab.@internal.engine.getProperty(_objrep, "X");
        set => _objrep = _matlab.matlab.@internal.engine.setProperty(_objrep,"X",value);
    }
    public double Y {
        get => _matlab.matlab.@internal.engine.getProperty(_objrep, "Y");
        set => _objrep = _matlab.matlab.@internal.engine.setProperty(_objrep,"Y",value);
    }
    public void show(){
        _objrep.show(new RunOptions(nargout:0));
    }
    public void show( out dynamic _P){
        _P = (MATLABArray)_objrep.show(new RunOptions(nargout:1));
    }
    public static implicit operator MATLABObject(Position position){
        position.ThrowIfDefaultValue();
        return position._objrep;
    }
    public static implicit operator Position(MATLABObject _obj){
        Position position = default;
        position._objrep = _obj;
        position._matlab = typeof(MATLABObject).GetField("Matlab", 
            System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(_obj);
        return position;
    }
}

The input to the constructor is a MATLABProvider object.

public  Position(MATLABProvider _matlab)

Create a Position object in your .NET application.

MATLABEngine matlab = MATLABEngine.StartMATLAB();
Position p1 = new Position(matlab, 1, 1);

Create a Position object in your deployed .NET application.

MATLABRuntime runtime = MATLABRuntime.StartMATLAB();
Position p2 = new Position(runtime, 1, 2);

For information about building an application from a MATLAB class, see Create C# Code from Strongly Typed MATLAB Function.

Version History

Introduced in R2023b