1.1. onversion of m (MATLA source code) file to .NET ... · PDF filey clicking the “Add...

19
1.1. Conversion of m (MATLAB source code) file to .NET component in the form of dll (Dynamic Link Library) file using MATLAB Following are the steps to convert m-file into dll-file using MATLAB deployment project tool: First write “deploytool” in the “Command Window” of MATLAB and press “Enter” as shown below: Figure 3-2: Opening “Deployment Project” window It will open the “Deployment Project” window. Enter the desired name e.g. I named it as “DotNetProject” and select the locaon where you want to save the .NET component. From the type drop down menu select the “.NET Assembly” opon as shown in the snapshot below: Figure 3-3: “Deployment Project” window and selecon of “.NET Assembly”

Transcript of 1.1. onversion of m (MATLA source code) file to .NET ... · PDF filey clicking the “Add...

Page 1: 1.1. onversion of m (MATLA source code) file to .NET ... · PDF filey clicking the “Add Reference ... created project is located to select the dll-file and then click “OK” finally

1.1. Conversion of m (MATLAB source code) file to .NET component in

the form of dll (Dynamic Link Library) file using MATLAB Following are the steps to convert m-file into dll-file using MATLAB deployment project tool:

First write “deploytool” in the “Command Window” of MATLAB and press “Enter” as shown

below:

Figure 3-2: Opening “Deployment Project” window

It will open the “Deployment Project” window. Enter the desired name e.g. I named it as

“DotNetProject” and select the location where you want to save the .NET component. From

the type drop down menu select the “.NET Assembly” option as shown in the snapshot below:

Figure 3-3: “Deployment Project” window and selection of “.NET Assembly”

Page 2: 1.1. onversion of m (MATLA source code) file to .NET ... · PDF filey clicking the “Add Reference ... created project is located to select the dll-file and then click “OK” finally

After making desired changes on the “Deployment Project” window press “Enter” or click

“OK” on the window. It will open the “.NET Assembly” window as shown below:

Figure 3-4: “.NET Assembly” window

Page 3: 1.1. onversion of m (MATLA source code) file to .NET ... · PDF filey clicking the “Add Reference ... created project is located to select the dll-file and then click “OK” finally

Now on the “.NET Assembly” window click on “Add class” to add a new class and rename it

according to your choice e.g. I named it as “DotNetClass”. Any number of classes can be added

according to the requirement. The snapshot shown below shows a class “DotNetClass” by

above mentioned procedure:

Figure 3-5: Adding a class in “.NET Assembly” window

Page 4: 1.1. onversion of m (MATLA source code) file to .NET ... · PDF filey clicking the “Add Reference ... created project is located to select the dll-file and then click “OK” finally

Now click on “Add files” below the class you just added and it will open “Add Files” window

which enables you to select the m-files you want to add to this class. Any number of m-files

can be added depending upon the user requirements. Browse the path for the files you want

to add to this class, select them and click open on the “Add Files” window. The “Add Files”

window is shown below:

Figure 3-6: Adding m-file to the project in MATLAB

Page 5: 1.1. onversion of m (MATLA source code) file to .NET ... · PDF filey clicking the “Add Reference ... created project is located to select the dll-file and then click “OK” finally

It will add the selected files below the class as shown in the following snapshot:

Figure 3-7: Class in “.NET Assembly” window along with added m-file

To see the files which will be created within the package after building the project click on

“Package” tab on the “.NET Assembly” window. For example following snapshot shows the

files which will be created in my “DotNetProject”:

Figure 3-8: Preview of files which will be built by MATLAB .NET Builder

Page 6: 1.1. onversion of m (MATLA source code) file to .NET ... · PDF filey clicking the “Add Reference ... created project is located to select the dll-file and then click “OK” finally

Next step is optional. It depends upon your choice and requirement. If you like to add MCR

(MATLAB Compiler Runtime) in your package then go to “Package” tab on “.NET Assembly”

window and click on “Add MCR”. The snapshot for this is shown below:

Figure 3-9: Adding MCR to project in MATLAB

The alternate way procedure for adding MCR is going to “action” button on the “.NET

Assembly” window and click on “Add MCR”. The snapshot for this step is shown below:

Figure 3-10: Alternative way of adding MCR to a project in MATLAB

Page 7: 1.1. onversion of m (MATLA source code) file to .NET ... · PDF filey clicking the “Add Reference ... created project is located to select the dll-file and then click “OK” finally

It will open prompt you to select the way you want to add the MCR in your package. It’s up to

you which way you prefer then select that option and click “OK” on the dialog. The following

snapshot shows the dialog which appears:

Figure 3-11: Different options for adding MCR in a project in MATLAB

Page 8: 1.1. onversion of m (MATLA source code) file to .NET ... · PDF filey clicking the “Add Reference ... created project is located to select the dll-file and then click “OK” finally

After finishing all the above steps the project is ready to be built to convert m-file into .NET

assembly which can be used in .NET solutions. In order to build the project click on “build”

button on the “.NET Assembly” window as shown below:

Figure 3-12: Building project in MATLAB for m-file to dll-file conversion

Page 9: 1.1. onversion of m (MATLA source code) file to .NET ... · PDF filey clicking the “Add Reference ... created project is located to select the dll-file and then click “OK” finally

The alternative way of building the project is to go to “action” button on the “.NET Assembly”

window and click on “build” button there. The snapshot for this is shown below:

Figure 3-13: Alternative way of building project in MATLAB

Page 10: 1.1. onversion of m (MATLA source code) file to .NET ... · PDF filey clicking the “Add Reference ... created project is located to select the dll-file and then click “OK” finally

When the “build” button is clicked then start building and takes sometime depending upon

the computer in use. The following window appears when the “build” button is clicked:

Figure 3-14: Window showing project building in MATLAB in progress

Page 11: 1.1. onversion of m (MATLA source code) file to .NET ... · PDF filey clicking the “Add Reference ... created project is located to select the dll-file and then click “OK” finally

The following window appears on successful building of project:

Figure 3-15: Successful built of project

In case of any error in building, the project building fails and it indicates the problem which if

needs to be corrected and after correction re-building of project succeeds.

Page 12: 1.1. onversion of m (MATLA source code) file to .NET ... · PDF filey clicking the “Add Reference ... created project is located to select the dll-file and then click “OK” finally

Now the files which are built by this process are ready to be used within a .NET solution can

be seen in the following snapshot:

Figure 3-16: Files created by project building in MATLAB

1.2. Adding MATLAB generated “dll” into C# program as a reference and

using its function as a method in C# Following are the steps to use MATLAB generated dll-file in a C# program:

First of all open a new project in Visual Studio C#. I am using Visual C# 2010 express for this

purpose. There are many good help pages available for staring .NET programming using Visual

Studio on the. So, I will not much concentrate on .NET programming stuff rather then I will

put my concentration on using MATLAB built dll-files in your C# project and will demonstrate

with the help of snapshots that how to add references, define namespace, creating object of

class built by MATLAB and using method of this class in C# program. For this purpose I will use

the dll which I just built in the previous section.

I just opened a console application named “DotNetConsole”. The snapshot below shows the

initial screen which appears when a console project is created:

Page 13: 1.1. onversion of m (MATLA source code) file to .NET ... · PDF filey clicking the “Add Reference ... created project is located to select the dll-file and then click “OK” finally

Figure 3-17: Initial window in Visual C# 2010 for a console application

Right click on “References” and choose “Add Reference…” from the menu which appears. The

following snapshot shows this:

Figure 3-18: Adding MATLAB generated dll-file in C# project

Page 14: 1.1. onversion of m (MATLA source code) file to .NET ... · PDF filey clicking the “Add Reference ... created project is located to select the dll-file and then click “OK” finally

By clicking the “Add Reference…” option, “Add Reference” window appears. Select the

“Browse” tab on this window then browse “distrib” folder within the location where MATLAB

created project is located to select the dll-file and then click “OK” finally to add the selected

dll-file into the C# project. In my case I will choose “DotNetProject.dll” file which I created in

MATLAB. Following snapshots shows this:

Figure 3-19: “Add Reference” window for browsing and selecting dll-file need to be added to a C# project

Page 15: 1.1. onversion of m (MATLA source code) file to .NET ... · PDF filey clicking the “Add Reference ... created project is located to select the dll-file and then click “OK” finally

When “OK” is clicked on the “Add Reference” window shown above then dll file is added to

the references of the project which can be seen below the “References” label within the

“Solution Explorer” window in C# project as shown in the below snapshot:

Figure 3-20: MATLAB generated dll-file visible below “References” label in C# project

Page 16: 1.1. onversion of m (MATLA source code) file to .NET ... · PDF filey clicking the “Add Reference ... created project is located to select the dll-file and then click “OK” finally

In order to use MATLAB functions which input data from C# and after processing output the

result to C# require adding another dll-file called “MWArray.dll” which allows using MATLAB

data types variables in C# just as native data types in presence of this dll. This dll-file is located

in the installation folder of MATLAB. The version of MATLAB who’s “MWArray.dll” is added

must be the same as the MATLAB with which m-file was converted to a dll-file otherwise this

will generate error. In case that you don’t have MATLAB installed then you need to install MCR

(MATLAB Compiler Runtime) and “MWArray.dll” can be found then in installation folder of

MCR. The version of MCR must also correspond to the version of MATLAB with which m-file

was converted to a dll-file. The information about the version of MCR which is needed can be

found along with other files in the “readme.txt” file in the folder. The following snapshot

shows the “readme.txt” file along with MATLAB created dll-files:

Figure 3-21: “readme.txt” in MATLAB built project which tells requirement for using MATLAB generated dll-files

Page 17: 1.1. onversion of m (MATLA source code) file to .NET ... · PDF filey clicking the “Add Reference ... created project is located to select the dll-file and then click “OK” finally

Now use the same procedure to add “MWArray.dll” as described above for

“DotNetProject.dll” and then it can also be seen in the “Solution Explorer” window below

“References” label in the C# project as shown in the following snapshot:

Figure 3-22: “MWArray.dll” which is key for communication between MATLAB and C# for input and output data both ways

Page 18: 1.1. onversion of m (MATLA source code) file to .NET ... · PDF filey clicking the “Add Reference ... created project is located to select the dll-file and then click “OK” finally

Now after adding the dll-files we need to add namespaces in the .cs (C# source code) file in

which we want to use MATLAB function. Following is the snapshot after adding namespaces

in “Program.cs”:

Figure 3-23: Using namespaces in C# for “MWArray.dll” and MATLAB built dll-file

The upper two red marked namespaces are for “MWArray.dll” and the lower one red marked

namespace is for my built “DotNetProject.dll”. After this the MATLAB class and function which

we built are ready to be used in C# program.

Now I will define variables of type “MWNumericArray” which is a subclass of abstract class

MWArray, assign them user entered values, create an object of “DotNetClass” and will call its

function “stability” with input parameters of type “MWNumericArray” just as calling a native

method of a class in C#. I will not go in much depth of C# classes, objects and methods because

very good information is available about this on internet. Following snapshot shows the way

of defining variables of type “MWNumericArray”, assigning them value, creation of an object

“test” of class “DotNetClass” which we created in MATLAB and then calling the “stability”

method of this class.

Page 19: 1.1. onversion of m (MATLA source code) file to .NET ... · PDF filey clicking the “Add Reference ... created project is located to select the dll-file and then click “OK” finally

Figure 3-24: Defining variables of MATLAB supported data type, creation of an object of MATLAB generated class and using its function as method in C#

The upper marked block shows way of defining variables of type “MWNumericArray”, middle

marked block shows assigning values to these variables and the lower block shows creation

of an object of class “DotNetClass” and calling the MATLAB file “stability.m” as a method of

“DotNetClass”.