Main Content

Call .NET Delegates in MATLAB

This example shows you how to use a delegate in MATLAB®. It creates a delegate using a MATLAB function (char).

Declare a Delegate in a C# Assembly

The C# example NetDocDelegate.cs defines delegates used in these examples. To run the examples, build and load the NetDocDelegate assembly as described in Build and Load .NET Assembly for MATLAB. The source code is:

using System;
namespace NetDocDelegate
{
    public delegate string delInteger(int x);
    public delegate Int32 del2Integer(Int32 arg1, Int32 arg2);
    public delegate string delString(string message);
}

Select a MATLAB Function

The delInteger delegate encapsulates any method that takes an integer input and returns a string. The MATLAB char function, which converts a nonnegative integer into a character array, has a signature that matches the delInteger delegate. For example, this command displays the ! character:

char(33)

Create an Instance of the Delegate in MATLAB

To create an instance of the delInteger delegate, pass the function handle of the char function:

myFunction = NetDocDelegate.delInteger(@char);

Invoke the Delegate Instance in MATLAB

Use myFunction the same as you would char. For example, this command displays the ! character:

myFunction(33)

See Also

Topics