matlab.engine.typedinterface.generateCSharp
Generate C# code interface for MATLAB namespaces, classes, and functions
Since R2023b
Description
matlab.engine.typedinterface.generateCSharp(
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 targetFolder
,Name=Value
)Namespaces
,
Classes
, or Functions
name-value argument.
Examples
Create C# Interface from MATLAB Function
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
targetFolder
— Name of folder for C# interface
string scalar | character vector
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.
Namespaces
— MATLAB namespaces
string array | character vector | cell array of character vectors
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"
Classes
— MATLAB classes
string array | character vector | cell array of character vectors
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 isMATLABEnums
. 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"]
Functions
— MATLAB functions
string array | character vector | cell array of character vectors
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"]
OuterCSharpNamespace
— C# namespace containing generated interface
string scalar | character vector
C# namespace containing the generated interface, specified as a string scalar or character vector. Specify this argument as the outermost namespace.
Example: OuterCSharpNamespace="shapes"
FunctionHolderClass
— C# class for MATLAB functions
"MATLABFunctions"
(default) | string scalar | character vector
C# class for MATLAB functions that do not belong to a C# class, specified as a string scalar or character vector.
Example: FunctionHolderClass="shapeInformation"
DisplayReport
— Option to display generation messages
false
or 0
(default) | true
or 1
Option to display generation messages, specified as a numeric or logical
0
(false
) or 1
(true
).
Example: DisplayReport=true
SaveReport
— Log filename
string scalar | character vector
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 R2023bR2024a: Support for handle classes
You can include handle classes in a strongly typed C# interface. The function creates code with handle copy behavior. That is, functions and methods can modify the input data in the calling workspace without making copies.
R2024a: Support for comparison operators
The function generates C# comparison methods and the isvalid
function
as well as comparison operators for both handle and value classes which implement the
eq
, ne
, lt
,
gt
, le
, and ge
methods in
MATLAB.
R2024a: Packages
name-value argument renamed to Namespaces
The name of the Packages
name-value argument is now Namespaces
. The
behavior of this name-value argument remains the same. There are no plans to remove support
for existing references to the argument.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)