Main Content

matlab.engine.typedinterface.generateCSharp

Generate C# code interface for MATLAB namespaces, classes, and functions

Since R2023b

    Description

    matlab.engine.typedinterface.generateCSharp(targetFolder,Name=Value) creates a C# interface from one or more MATLAB® namespaces, classes, and functions. Use the interface to build a C# application. You must specify at least one Namespaces, Classes, or Functions name-value argument.

    example

    Examples

    collapse all

    Create a MATLAB function in a file on the MATLAB path named CircleInfo.m with these statements.

    function [diameter,area] = CircleInfo(radius)
        arguments (Input)
            radius (1,1) double {mustBeReal}
        end
        arguments (Output)
            diameter (1,1) double {mustBeReal}
            area (1,1) double {mustBeReal}
        end
        area = pi.*radius.*radius;
        diameter = 2.*radius;
    end

    Generate a C# interface from the CircleInfo function, and save the C# file in a folder named CircleApp.

    matlab.engine.typedinterface.generateCSharp("CircleApp",Functions="CircleInfo")
    0 class(es) and 1 function(s) written to CircleApp

    The function creates CircleInfo.cs. You can include this file in a .NET application.

    /* File: CircleInfo.cs
    *
    * MATLAB Strongly Typed Interface Version: R2023b
    * C# source code generated on: 06-Jun-2023
    */
    using System;
    using MathWorks.MATLAB.Types;
    using MathWorks.MATLAB.Exceptions;
    
    public static partial class MATLABFunctions { 
        public static void CircleInfo(MATLABProvider _matlab, dynamic radius){
            dynamic _dynMatlab = _matlab;
            _dynMatlab.CircleInfo(new RunOptions(nargout:0),radius);
        }
        public static void CircleInfo(MATLABProvider _matlab, dynamic radius,  out dynamic diameter){
            dynamic _dynMatlab = _matlab;
            diameter = (MATLABArray)_dynMatlab.CircleInfo(new RunOptions(nargout:1),radius);
        }
        public static void CircleInfo(MATLABProvider _matlab, dynamic radius,  out dynamic diameter,  out dynamic area){
            dynamic _dynMatlab = _matlab;
            (diameter,area) = ((MATLABArray,MATLABArray))_dynMatlab.CircleInfo(new RunOptions(nargout:2),radius);
        }
    }
    

    Input Arguments

    collapse all

    Name of the folder for the generated C# interface, specified as a string scalar or character vector. The name can include a relative or absolute path. A best practice is to place C# code files in separate folders from MATLAB files.

    Example: "MyApp" specifies a subfolder of the current folder.

    Example: "C:\work\MyApp" specifies a folder with an absolute path.

    Name-Value Arguments

    Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

    Example: matlab.engine.typedinterface.generateCSharp("MyApp",Classes=["Position","Rectangle"],DisplayReport=true,SaveReport="report.log") generates a C# interface in the MyApp folder using the Position and Rectangle classes, displays generation messages, and saves the log as report.log.

    Note

    You must specify at least one Namespaces, Classes, or Functions name-value argument.

    Since R2024a

    MATLAB namespaces to include in the C# interface, specified as a string array, character vector, or cell array of character vectors. The namespace value becomes a C# namespace and supersedes any specified namespace. This value becomes the default namespace.

    Example: Namespaces="shapes"

    MATLAB value and handle classes to include in the C# interface, specified as a string array, character vector, or cell array of character vectors. The function generates one file with a .cs extension for each C# class, struct, and enumeration.

    • The function supports user-authored value and handle classes, classes inherited from other user-authored classes, built-in MATLAB classes, and classes inherited from MATLAB classes.

    • The function represents a MATLAB value class as a C# struct.

    • The function represents a MATLAB enumeration as a member of the namespace specified by the OuterCSharpNamespace name-value argument. The default namespace is MATLABEnums. Properties and methods of a MATLAB enumeration are not generated in C#.

    C# class methods return new objects as output variables. Like MATLAB methods, C# class methods do not update the existing object.

    Example: Classes=["Position","Rectangle"]

    MATLAB functions to include in the C# interface, specified as a string array, character vector, or cell array of character vectors. The function supports user-authored functions and built-in MATLAB functions. Because C# requires that every function belong to a class, matlab.engine.typedinterface.generateCSharp generates a C# class named MATLABFunctions for these functions. To specify the class name, use the FunctionHolderClass name-value argument.

    Example: Functions=["show","enlarge"]

    C# namespace containing the generated interface, specified as a string scalar or character vector. Specify this argument as the outermost namespace.

    Example: OuterCSharpNamespace="shapes"

    C# class for MATLAB functions that do not belong to a C# class, specified as a string scalar or character vector.

    Example: FunctionHolderClass="shapeInformation"

    Option to display generation messages, specified as a numeric or logical 0 (false) or 1 (true).

    Example: DisplayReport=true

    Log filename, specified as a string scalar or character vector. The name can include a relative or absolute path. Specify this name-value argument to create a text file, and optionally include a file extension. The file contains detailed information about the matlab.engine.typedinterface.generateCSharp function call and the generated code.

    Example: SaveReport="log.txt"

    Version History

    Introduced in R2023b

    expand all