Create a .NET Assembly with MATLAB

This example shows how to transform a MATLAB function into a .NET assembly and integrate it into an application.

Create a .NET Assembly

Start Matlab, create and save new function.

Write “deploytool” command to Matlab Command Window and press Enter.

At the Deployment Project window select “.NET Assembly” from Target combobox, give him new name and press OK.

At this moment need create new class(es) (press “Add class”) and put there matlam function(s).

After that press build button and please wait while operation complete.

Open current matlab folder, how you can see,deploytool operation create .net assembly. Now you can use this dll with C#. If you want to use this library on other PC (without Matlab), you need install only Matlab Compiler Runtime (MCR) Installer. You can find him at the Matlab folder or download.

Create C# project

Creat new project, add references on matlab dll and MWArray.dll. MWArray.dll you can find on your PC after installation MCR. After that add new namespaces and call matlab function:

using MathWorks.MATLAB.NET.Arrays;
using MatlabFunction;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            MatlabFunctionClass matlabFunvtion = new MatlabFunctionClass();
            var res = matlabFunvtion.SumFunction((MWArray)2, (MWArray)6);
        }
    }
}

Compile and run application.

Leave a Comment