Quantcast

Documentation Center

  • Trial Software
  • Product Updates

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). For another example, see Create Delegates from .NET Object Methods.

This example consists of the following tasks:

Declare a Delegate in a C# Assembly

The C# example NetDocDelegate.cs, in the matlabroot/extern/examples/NET/NetSample folder, defines delegates used in the following examples. To see the code, open the fileopen the file in MATLAB Editor. To run the examples, build the NetDocDelegate assembly as described in Build a .NET Application for MATLAB Examples.

Load the Assembly Containing the Delegate into MATLAB

If the NetDocDelegate assembly is in your c:\work folder, load the file with the command:

dllPath = fullfile('c:','work','NetDocDelegate.dll');
NET.addAssembly(dllPath);

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 (string), has a signature that matches the delInteger delegate. For example, the following 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, the following command displays the ! character:

myFunction(33)
Was this topic helpful?