Use MATLAB Handle Classes in C#
Overview
The MATLAB® engine API for .NET supports MATLAB handle classes when using the strongly typed interface. This feature translates the behavior of MATLAB classes that inherit from handle classes into equivalent C# code, preserving specific features and behaviors. (since R2024a)
Key Features
When you generate a C# file from a MATLAB class that inherits from a handle class using the matlab.engine.typedinterface.generateCSharp
function, you get this
functionality:
Copy behavior: The generated C# code replicates MATLAB handle class copy behavior. In MATLAB, handle objects are reference types, meaning that when you copy these objects, both the original and the new variable refer to the same object.
Comparison operators: The C# representation of MATLAB handle classes supports comparison operations. You can compare C# objects, derived from MATLAB handle classes, using the standard operators
==
,!=
,<
,>
,<=
, and>=
.isvalid
function support: The C# interface supports theisvalid
function, which checks if handle objects are valid or have been deleted.delete
function support: The C# interface supports thedelete
function.
Inherent Capabilities of MATLAB Handle Classes
Deriving from the MATLAB handle class enables a subclass to:
Inherit methods.
Define events and listeners.
Define dynamic properties.
Implement
set
andget
methods.Customize copy behavior.
Example Files
These example files demonstrate the use and integration of MATLAB handle classes with C# applications using the engine API for .NET:
BankAccount.m
: This MATLAB class file inherits from the handle class and provides basic banking functionality. For more information, see MATLAB BankAccount Class.generateBankAccount.m
: A MATLAB script used to generate the corresponding C# file from theBankAccount
class, illustrating the integration with the engine API for .NET.matlab.engine.typedinterface.generateCSharp( ... "BankAccount", ... Classes="BankAccount")
run_BankAccount.m
: An example using theBankAccount
class.Program.cs
: This C# console application demonstrates the use of the generated C# file from the MATLABBankAccount
class. It replicates the functionality of the MATLAB scriptrun_BankAccount.m
, but within a .NET environment. This application shows key operations such as account creation, deposits, withdrawals, and balance inquiries, mirroring the actions performed in the MATLAB script.
MATLAB BankAccount
Class
Class definition: The MATLAB class BankAccount
is defined as a subclass of the handle
class, which allows it to exhibit reference behavior. This means instances of this class can
be passed by reference.
Private properties: The class has one private property, Balance
,
which is a double.
Methods: The class includes methods for depositing, withdrawing, and checking the
balance. These methods ensure controlled access and modification of the
Balance
property.
Generated C# BankAccount
File
Class inheritance: The C# BankAccount
class does not explicitly
inherit from a standard C# class that mimics a MATLAB handle class. Instead, the characteristics of a MATLAB handle class are flattened into the C# representation. This approach involves
selectively integrating a subset of supported methods into the generated C# code. By
incorporating these methods, the C# BankAccount
class emulates the
behavior of a MATLAB handle class.
Constructors and overloaded operators: These operators provide similar functionalities to a MATLAB handle class, which supports comparison operations.
public static bool operator >= (BankAccount obj1, BankAccount obj2) public static bool operator <= (BankAccount obj1, BankAccount obj2) public static bool operator != (BankAccount obj1, BankAccount obj2) public static bool operator == (BankAccount obj1, BankAccount obj2) public static bool operator > (BankAccount obj1, BankAccount obj2) public static bool operator < (BankAccount obj1, BankAccount obj2)
Method mapping: The C# methods (deposit
, withdraw
,
and checkBalance
) correspond to the MATLAB class methods.
MATLAB Signature | C# Signature |
---|---|
function deposit(obj, amount) |
public void deposit(double amount) |
function withdraw(obj, amount) |
public void withdraw(double amount) |
function bal = checkBalance(obj) |
public void checkBalance() |