You have just finished testing your Matlab script and now you would like to be able to import it into your C# project.

How can you do this?

In this article we will take a step-by-step look at how to compile a Matlab program and import it into our C# applications.

Firstly we will start by developing a simple Matlab script and see how to use the Matlab Coder to generate dynamic libraries. We will then go on to present the C# code needed to import the resulting library into our .NET project.

Write Matlab Script

Let’s start by writing a simple Matlab script that will serve as a test bench: open Matlab and create a new .m file called h_rand.m.

In this example we will concentrate on the use of the rand function, which allows us to generate two-dimensional matrices containing uniformly distributed random numbers.

The code in question, which has to be a function, will be as follows:

Copy to Clipboard

As you can see our function is called h_rand and takes 2 input arguments:

  1. row: the number of rows for the output matrix.
  2. column: the number of columns for the output matrix.

The output is going to be placed in the output variable which is used in the row 2 to store the result of the function call.

Generate Dll

Ok, now on the top menu bar, click on Apps -> Matlab Coder.

As a result this will open the Matlab Coder Window through which we will compile our function.

On the main page, type the function name you want to compile and press enter.

At this point we can specify the input parameter type, therefore click on Let me enter input or global types directly.

Specify the input types, in our example we are talking about 2 unsigned integer (int32) scalars (1 x 1).

Press Next for 2 times so that you reach the Generate Page. Now, select Dynamic Library, and then click on Generate.

This will generate the C compiled code and the dll library, in this case we must have a look on the generated code, in particular open the h_rand.c:

Copy to Clipboard

C function h_rand takes 3 arguments:

  1. unsigned int row: the number of row.
  2. unsigned int col: the number of columns.
  3. emxArray_real_T *output: an output matrix.

These parameters are necessary to understand how to structure the next steps when we’ll create our Matlab C# project.

Now, click Next so that you can see the Generated Output Folders and note down the binaries path where to find the dll.

Matlab Coder supports a huge amount of primitive Matlab functions but there are some of them that have limitations.

This means that if you try to compile the Matlab code into C, you may encounter some problems.

Take as reference this official page in which you can find a complete list of all possible functions.

Matlab C#: Import to .NET

We are ready to create our C# project, so open your IDE, Visual Studio in my case, and create a new .NET Console project. It can be a .NET Core or a .NET Framework solution.

Once created, the first step is to copy-paste the dll created on the previous step in the project folder.

That being said, right-click on the dll in the solution explorer -> Properties and select Copy if newer for the Copy to Output Directory item.

The script to be used is composed by 2 main parts:

  1. Data structured necessary to pass information from C# to the C dll.
  2. C# program that calls the external function and gets the result.

1. emxArray_real_T Struct

The first part comes from the Matlab structure used for wrapping the result output and is called emxArray_real_T.

This container, according to the generated file h_rand_types.h, has the following structure:

Copy to Clipboard

We need to port this struct into our C# project. This is possible thanks to the following code:

Copy to Clipboard

From lines 1 to 8 we have replicated the same C structure as before. In the rest of the code, we use the GCHandle object to protect the data shared with our dll from being collected by the Garbage Collector.

Since this data aren’t handled by the GC, they have to be manually deleted. This is what is done in the Dispose method (rows 42-47).

2. Matlab C# Program

The second part is composed by a code in which we specify (rows 8-10) the external function h_rand together with its input parameters.

Note that the function name reported in row 10 has to be the same as the Matlab function.

In addition to this is also important to follow the same order shown in the h_rand.c code (rows, columns and output).

As we can see in the code reported below, the output parameter is represented by a reference pointer (row 10) to the struct previously defined.

Copy to Clipboard

In the rows 18-22 we use the wrapper to make the function call and save the result. Finally in the rows 24-31 we print the result: a 5 x 5 matrix filled with random numbers.

If you try to run both applications, you will see that the results are the same.

Conclusions

In this article we have seen how to convert a Matlab script into a dynamic library in order to use it into a .NET application.

This is possible thanks to the Matlab Coder which allows to convert almost every Matlab function into C code. Once done that, importing such a dll into a C# program is straightforward.

If you want, you can find the entire project at this link, and before you leave the page, have a look on the other articles of the Tips & Tricks section or leave a comment below for any question/request!