Contenido principal

Use .NET Methods in MATLAB

From within MATLAB®, you can view .NET method signatures and call .NET methods. MATLAB decides which .NET method to execute by examining the method name, argument count, and argument type.

Display .NET Method Information

MATLAB provides several functions to display the methods of a .NET class. You can use these functions without creating an instance of the class. These functions do not list generic methods; use the .NET documentation to get information on generic methods.

  • methods — Display method names.

  • methods with "-full" option — Display method signatures.

  • methodsview — Display method signatures in a table.

The methodsview table is often easier to use as a reference, because the table appears in a separate window, rather than in the Command Window. For example, display a methodsview table for the System.String class. A separate window displays the method names, return types, and argument lists in a table format.

methodsview("System.String")

MATLAB uses these naming conventions when displaying method signatures:

  • this — The object argument.

  • obj — Output from the constructor.

  • RetVal — The return value of a method.

  • All other arguments use .NET metadata names.

For example, this table shows one row of the methodsview table for the Equals method, where this refers to the System.String object, and obj is the comparison argument. The method returns RetVal.

NameReturn TypeArguments
Equalslogical scalar RetVal(System.String this, System.Object obj)

How MATLAB Chooses the .NET Method to Run

When you call a .NET method from MATLAB, MATLAB follows these steps to select the method to run:

  1. Method existence — MATLAB checks if the object (or class, for static methods) has a method by that name.

  2. Argument count — MATLAB checks if the number of arguments matches at least one method with that name.

  3. Type compatibility — MATLAB checks if each argument can be converted to the type defined for the method.

If all the checks pass, then MATLAB calls the matching method.

For overloaded methods, MATLAB follows these additional rules:

  • MATLAB rejects methods with incompatible argument types.

  • MATLAB selects the method whose arguments best fit the calling arguments, based on fitness values (how closely MATLAB types match .NET types).

  • If two methods have the same fitness, MATLAB chooses the first one defined in the class.

  • For class types, MATLAB chooses the method based on the "distance" between the incoming class type and the expected .NET class type.

For rules about overloaded methods with optional arguments, see Call Overloaded Method with Optional Arguments.

Work with .NET Methods Having Multiple Signatures

To run this example, build and load the assembly created from this SampleMethodSignature C# Source Code using the directions in Build and Load .NET Assembly for MATLAB.

Display Function Signature Example

Create a SampleMethodSignature object.

obj = netdoc.SampleMethodSignature;

Display the method signatures.

methods(obj,"-full")

Look for these signatures in the MATLAB output.

netdoc.SampleMethodSignature obj SampleMethodSignature
netdoc.SampleMethodSignature obj SampleMethodSignature(double scalar d)
netdoc.SampleMethodSignature obj SampleMethodSignature(System.String s)

For more information about argument types, see Handle Data Returned from .NET Objects.

SampleMethodSignature C# Source Code

The C# example SampleMethodSignature.cs defines three constructors.

using System;
namespace netdoc
{
    public class SampleMethodSignature
    {
        public SampleMethodSignature ()
        {}
        
        public SampleMethodSignature (double d)
        { myDoubleField = d; }
        
        public SampleMethodSignature (string s)
        { myStringField = s; }

        public int myMethod(string strIn, ref double dbRef, 
					out double dbOut)
        {
            dbRef += dbRef;
            dbOut = 65;
            return 42;
        }

        private Double myDoubleField = 5.5;
        private String myStringField = "hello";
    }
}

Call .NET Methods in MATLAB

You can call .NET methods from within MATLAB. For example, concatenate two .NET strings using a .NET method. Start by creating two .NET System.String objects.

str1 = System.String("hello");
str2 = System.String("world");

To find a concatenation method, display the method signatures of the System.String class.

methodsview("System.String")

Concatenate the two .NET strings by using the .NET Concat method.

System.String.Concat(str1,str2)

Based on the method name and argument count, MATLAB finds these relevant signatures:

NameReturn TypeArgumentsQualifiers
ConcatSystem.String RetVal(System.Object arg0,
System.Object arg1)
Static
ConcatSystem.String RetVal(System.String str0,
System.String str1)
Static

Because both str1 and str2 are of type System.String, MATLAB calls the second method and returns:

ans = 
helloworld

If you specify System.Object arguments, MATLAB calls the method with System.Object arguments, applies ToString to each argument, and concatenates their string representations.

dt = System.DateTime.Today;
myDate = System.DateTime(dt.Year,3,1,11,32,5);
System.String.Concat(dt,myDate)
ans = 
9/5/2025 12:00:00 AM3/1/2025 11:32:05 AM

Use .NET Operators in MATLAB

MATLAB supports overloaded .NET operators, mapping them to these MATLAB methods:

C++ Operator Symbol.NET OperatorMATLAB Method
+ (binary) op_Addition plus, +
- (binary) op_Subtraction minus, -
* (binary) op_Multiplymtimes, *
/op_Divisionmrdivide, /
&&op_LogicalAndand, &
||op_LogicalOror, |
==op_Equalityeq, ==
>op_GreaterThangt, >
<op_LessThanlt, <
!=op_Inequalityne, ~=
>=op_GreaterThanOrEqualge, >=
<=op_LessThanOrEqualle, <=
- (unary)op_UnaryNegationuminus, -a
+ (unary)op_UnaryPlusuplus, +a

MATLAB implements other overloaded operators (such as % and +=) using their static method names (for example, op_Modulus and op_AdditionAssignment). For details, see the Microsoft® .NET documentation.

For example, compare two dates of type System.DateTime using the == operator.

date1 = System.DateTime(2025,1,1);
date2 = System.DateTime(2025,1,1);
date1 == date2
ans =

  logical

   1

Alternatively, use the static method.

System.DateTime.op_Equality(date1,date2)
ans =

  logical

   1

Compare .NET Objects

To compare two .NET objects, use the isequal and isequaln functions which is equivalent to calling the .NET Equals method. (since R2026a)

Call .NET Extension Methods

You can use extension methods to add functionality to existing types. In MATLAB, extension methods are called as static methods of the class that defines them (unlike in C#). For information on extension methods and how to implement a custom extension method, see the Microsoft C# documentation.

Limitations for .NET Methods in MATLAB

MATLAB enables you to call .NET methods. However, MATLAB does not support this functionality:

  • Displaying Generic Methods — The methods and methodsview functions do not list generic methods. As a result, you might not see all available .NET methods when using these MATLAB functions.

  • Overloading MATLAB Functions — If your .NET application implements a method with the same name as a MATLAB function, the method must have the same signature as the MATLAB function. If the signatures do not match, MATLAB throws an error. For information about how MATLAB handles overloaded functions, see Overload Functions in Class Definitions.

  • Calling Overloaded Static Methods — If an instance method has the same name as a static method in the same class, MATLAB throws an error if you attempt to call the instance method.

See Also

Functions

Topics