How To Call Functions In a MATLAB Generated .NET DLL From C#

Posted on Updated on

This Tutorial does not represent a practical use of  a MATLAB generated .NET DLL since it requires the installation and use of MATLAB and the MATLAB Compiler Runtime (MCR) for a scenario that could be easily handled by a more elegant algorithm such as the one discussed in my earlier post on RPN expression parsing. However, it does seek to demonstrate the principles behind developing an algorithm in MATLAB which could be then exported to a .NET component and reused in any managed language. MATLAB provides a fast prototyping environment which gives the programmer a vast array of tools which she can then use to design and test her algorithms. A subset of these functionalities are provided in the MCR which is basically an installable, free, headless version of MATLAB. Since the MCR is a MATLAB instance it requires the same amount of time for initial loading into memory and will also be as memory intensive as its GUI cousin.

That said,  MATLAB allows the packaging of its code into an encrypted Dll file which can then be executed by the MCR. In addition to .NET, MATLAB code can also be exported to C libraries , Java libraries or an executable file.

The first thing we will do is fire up MATLAB, we will access its computational engine through the use of  of  the eval  function which will return the result of a mathematical expression passed to it as a string.  There are the steps.

  1. Launch MATLAB
  2. Type edit in the MATLAB command window then press enter/return. 
  3. In the new window create the function as seen below then hit the save button.
    function result = calculator(input)
    result = eval(input);
  4. Click File->New->Deployment Project then type calculator in the name box.
  5. Select a location to save the project and .NET Assembly from the type drop down box.
  6. In the .NET Assembly window, under the build tab change the default name of the class to “demo” then drag and drop the calculator.m file unto the class to add it as a method of the class, then hit the compile button. This should create the calculator.dll which can be referenced from your c# application.
  7. Fire up Visual Studio and create a new C# Windows Forms Application, to this application add a reference to the calculator.dll file, you will also need to add a reference to the MWArray.Dll which is located at “C:\Program Files\MATLAB\MATLAB Compiler Runtime\v716\toolbox\dotnetbuilder\bin\win64\v2.0\MWArray.dll” (version dependent path) in order to facilitate the conversion of .NET types to MATLAB types.
  8. GUI for expression parser application.
  9. C# Application code.
    using System;
    using System.Windows.Forms;
    using MathWorks.MATLAB.NET.Arrays;
    using calculator;
    namespace DemoCalculator
        public partial class Form1 : Form
            public Form1()
            private void button1_Click(object sender, EventArgs e)
                var calc= new demo();            
  10. Run the application then enter a mathematical expression and hit the Calculate button, this will send the string to be interpreted by the MCR and return a result to the user, the end.

8 thoughts on “How To Call Functions In a MATLAB Generated .NET DLL From C#

    phuc said:
    March 18, 2014 at 12:35 pm

    I have done that your code, but after I run my program, have error . It threw an exception without more explain. I dont know why .

    Johnny Ancich said:
    April 1, 2014 at 2:38 pm

    Cannot get this to work. I get this error
    Warning 1 There was a mismatch between the processor architecture of the project being built “MSIL” and the processor architecture of the reference “MWArray, Version=, Culture=neutral, PublicKeyToken=e1d84a0da19db86f, processorArchitecture=AMD64″, “AMD64″. This mismatch may cause runtime failures. Please consider changing the targeted processor architecture of your project through the Configuration Manager so as to align the processor architectures between your project and references, or take a dependency on references with a processor architecture that matches the targeted processor architecture of your project. DemoCalculator

      Johnny Ancich said:
      April 1, 2014 at 3:54 pm

      figured it out myself. Had to go to configuration menu in vs2012 and change architecture from any CPU to x64

        Johnny Ancich said:
        April 2, 2014 at 1:30 pm

        Now, how do I do a similar program but with more than 1 input and more than 1 output??

      Romaine Carter responded:
      April 7, 2014 at 10:23 am

      Check the build settings\platform target of your .net app

        Romaine Carter responded:
        April 10, 2014 at 4:38 pm

        Functions do not usually return multiple outputs try using a container for the return object or out parameters.

    Ray said:
    January 4, 2015 at 9:06 pm

    Is it possible to calculate simple equation?
    For example, in a Matlab somthing like y=(x1)+2*(x2) equation.
    Then, possible to make a c# using msgbox?

    PEJA said:
    January 15, 2015 at 1:22 pm

    Hey there
    I’m trying to figure out how to send data from Matlab to programs that use C# in real time. This code in C# is fine but works for very simple .m files. I’m very new to TCP/IP so I first tried to use this method. Anyone has suggestions for me? 🙂

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s