Accessing MATLAB functions from C#.NET

MATLAB is a very useful high-level language and interactive environment that enables us to do computationally intensive tasks. Since it is an interpreted language, there are performance issues. However, we are blessed with huge libraries and toolboxes which will ease our complex mathematical computations. We can also make GUI applications in MATLAB using its GUI builder. The GUI toolbox in MATLAB only provides a few set of controls in it.

Most of us refrain from using languages like C or C++ as compared to MATLAB because of the lack of libraries which is available in MATLAB. In this tutorial, I will show you how to call the MATLAB functions from C#.NET code with useful examples. A similar tutorial is available at

but that tutorial do not explain any simple technique for handling images.
This tutorial explains the following

  • Calling a simple MATLAB function and fetching the result in .NET.
  • Processing Images using MATLAB function.

Calling a simple MATLAB function and fetching the result in .NET.

Let us do a simple matrix addition for a start.

1. Create a MATLAB function with the code for matrix addition

function [ c ] = addMatrices( a,b )
c=a+b; %add the matrices.

2. Type “deploytool” in the Command Window of MATLAB which will display

3. Type a project name in the Name field and select the Target as “.NET Assembly” and press the OK button.
4. Now the deployment tool will be shown and in the Deployment Tool, select the Build tab and add a class “MyMatClass”. Add our MATLAB function addMatrices to the class. The created class with the function will look like

5. Click on the build icon in the Deployment Tool to start deploying the .NET component.
6. Upon successful deployment, you will get the dll files generated and you can find them under the Package tab. We only need the MatCode.dll file in .NET

7. You should have MCR (MATLAB Compiler Runtime) installed in your machine and there is no need to install MATLAB. Next Step is to add the required References in .NET. The following are the two dll files required by the C#.NET project to invoke the MATLAB functions.
(b)MWArray.dll (this file can be located in the MCR installed directory , something like ….\MATLAB Compiler Runtime\v713\toolbox\dotnetbuilder\bin\win32\v2.0)
After adding these two references to the C#.NET project, you are ready to code.
The following is a code in C# that declares two matrices and passes them to a MATLAB function for adding.

int[,] a ={ { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; //Matrix 1
int[,] b ={ { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; //Matrix 2
MWNumericArray arr1 = a;
MWNumericArray arr2 = b;
MyMatClass obj = new MyMatClass();
MWArray result = obj.addMatrices((MWArray)arr1, (MWArray)arr2);
Debug.WriteLine(result); //Result is displayed

Following are the extra namespaces that are to be added

using MathWorks.MATLAB.NET.Arrays;
using MatCode;

The MCR will be handling the execution of MATLAB code and the results are returned to our C# program. MWArray.dll encapsulates the different types of data that enable communication between .NET and MATLAB.

Processing Images using MATLAB function.
Processing images is not simple as the matrix addition. Sometimes we may fall short of functions in C# to process images. In C#, we have other libraries like Aforge to enable image processing. However, we can say that Aforge library do not cover as much functions as MATLAB does. For example, in MATLAB we can apply different type of noise to image like sale & pepper, Gaussian, speckle noise etc. Aforge provides a method for adding salt & pepper noise to the image. But there is no method in Aforge that apply a speckle noise to the image. In such cases we still want to rely upon MATLAB.
The following code snippets that I wrote will help in processing of images in C# with the help of MATLAB. I will explain how to read an image in C# and pass it to MATLAB for processing. After processing the image, the MATLAB code will return the processed image back to C#.
Here we go…
Following a code in MATLAB function that apply salt & pepper noise to an image.
function [output_image] = applyNoise(input_image)
output_image=imnoise(input_image,'salt & pepper',0.5);

Now the function is packaged in a dll file as explained before.
The following are the steps in short that is to be done in C#.NET
1. Read an image into a Bitmap object.
2. Read the image pixels using getPixel method and place the pixel values in a 3D array of UInt16 (Unsigned integer).
3. Call MATLAB function and fetch the results in MWArray object.
4. Read the output array and set the output pixels accordingly.
int i, j;
Bitmap image = (Bitmap)pictureBox1.Image;
int width = image.Width;
int height = image.Height;
Bitmap processed_image = new Bitmap(width, height);
byte[, ,] rgb = new byte[3, height, width];
for (i = 0; i < height; i++)
for (j = 0; j < width; j++)
rgb[0,i,j] = image.GetPixel(j, i).R;
rgb[1,i,j] = image.GetPixel(j, i).G;
rgb[2,i,j] = image.GetPixel(j, i).B;
MWNumericArray narr = rgb;
MyMatClass obj = new MyMatClass();
MWArray u =obj.applyFixedNoise((MWArray)narr);
for (i = 0; i < height; i++)
for (j = 0; j < width; j++)
processed_image.SetPixel(j,i, Color.FromArgb(Convert.ToInt32(u[i + 1, j + 1, 1].ToString()), Convert.ToInt32(u[i + 1, j + 1, 2].ToString()), Convert.ToInt32(u[i + 1, j+ 1, 3].ToString())));
pictureBox2.Image = processed_image;
catch (Exception ex)

Following is a sample result obtained

It is to be noted that the code I shown above is specially written for RGB images. Doing the same for grayscale images is very easier than this.
Happy Coding…


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