To interface MATLAB and VB.NET a .dll file has to be created.
Steps to create a dll file
1) Open MATLAB R2009a software
2) Type deploytool in the MATLAB Command Window and press Enter key
3) A new Deployment Tool Window will be opened
4) Click on the New Deployment Project Icon (Circled in the figure below)
5) Select the MATLAB Builder NE option
6) Then Select Generic COM Component, enter a name of the project and press OK
7) A new deployment project will be created
8) Add the MATLAB .m files to the project by clicking on the icon circled below
9) Note that the .m files must be a function. For example, file Test2.m contains the function as follws:
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
10) More than one .m files can be added
11) Also other files such as C, C++ etc. can also be added
12) After adding all the desired files Click on Build Project icon (Circled in the image below)
13) A new dialog window Deployment Tool Output will open
14) For the first time only it will ask you to select a compiler for compiling the project
15) 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
16) I would suggest select Visual C++ 2008 Compiler
17) 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.
18) After the compilation Click on the package button, circled in the figure below
19) After packaging a folder with the project name will be created in the MATLAB folder
20) The dll file is present in the src and distrib folder
21) 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