Interfacing between MATLAB and VB.NET

To interface MATLAB and VB.NET a .dll file has to be created.

Steps to create a dll file

  • Open MATLAB  R2009a  software
  • Type deploytool in the MATLAB Command Window and press Enter key
  • A new Deployment Tool Window will be opened
  • Click on the New Deployment Project Icon (Circled in the figure below)
  • Select the MATLAB Builder NE option
  • Then Select Generic COM Component, enter a name of the project and press OK
  • A new deployment project will be created
  • Add the MATLAB .m files to the project by clicking on the icon circled below
  • Note that the .m files must be a function. For example, file Test2.m contains the function as follows:

Test2.m

function y = Test2(p)

y = imread(p); % reads image file

j = rgb2gray(y); % converts a colored image to grayscale image

imwrite(j,'C:\matlabtempimage.jpg','jpg'); % stores the image to a location in hard disk

  • More than one .m files can be added
  • Also other files such as C, C++ etc. can also be added
  • After adding all the desired files Click on Build Project icon (Circled in the image below)
  • A new dialog window Deployment Tool Output will open
  • For the first time only it will ask you to select a compiler for compiling the project
  • Click on the link in the new window opened and select a compiler form the list provided by entering the number next to the compiler and press Y to confirm
  •  I would suggest select Visual C++ 2008 Compiler
  • After selecting the compiler the MATLAB program will start compiling the project. It will display Compilation completed successfully if there are no errors else it will display the errors. It takes some time so be patient.
  • After the compilation Click on the package button, circled in the figure below:
  • After packaging a folder with the project name will be created in the MATLAB folder
  • The dll file is present in the src and distrib folder
  • The dll file has to be registered in order to use it in VB.NET

Two ways for registering the dll file:

Method 1

  • Open DOS Command prompt
  • Give the location of the dll file present in the src folder. For example:

C:\>cd C:\Documents and Settings\My Documents\MATLAB\Test\src

Then enter the command regsvr32 space (dll filename) For example:

C:\Documents and Settings\My Documents\MATLAB\Test\src> regsvr32 Test_1_0.dll

The following message will be displayed:

Method 2

Double Click on the _install.bat file in the distrib folder. The dll file will be registered automatically

After registering the dll file, Open Microsoft Visual Basic 2008

  • Open the your project and select Project –> Add Reference
  • Select COM –> Select your dll file –> Click on OK
  • The dll file will then be added as reference

Note that each time changes are made to the .m file the above process of building, packaging, registering and adding as reference must be repeated.

Example on How to call the dll file

Form1.vb

Public Class Form1

Private cal As Test.TestClass

....

....

Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

Dim d, b As Object

cal = New Test.TestClass

d = Me.OpenFileDialog1.FileName

Call cal.Test2(1, b, d)

End Sub

....

End Class

Comments are closed.