Sie sind auf Seite 1von 19

1.1. Conversion of m (MATLAB source code) fle to .

NET component in
the form of dll (Dynamic Link Library) fle using MATLAB
Following are the steps to convert m-fle into dll-fle 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 locaton where you want to save the .NET component. From
the type drop down menu select the .NET Assembly opton as shown in the snapshot below:













Figure 3-3: Deployment Project window and selecton of .NET Assembly

Afer 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











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 mentoned procedure:

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










Now click on Add fles below the class you just added and it will open Add Files window
which enables you to select the m-fles you want to add to this class. Any number of m-fles
can be added depending upon the user requirements. Browse the path for the fles 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-fle to the project in MATLAB












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

Figure 3-7: Class in .NET Assembly window along with added m-fle

To see the fles which will be created within the package afer building the project click on
Package tab on the .NET Assembly window. For example following snapshot shows the
fles which will be created in my DotNetProject:

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

Next step is optonal. It depends upon your choice and requirement. If you like to add MCR
(MATLAB Compiler Runtme) 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 acton buton on the .NET
Assembly window and click on Add MCR. The snapshot for this step is shown below:

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

It will open prompt you to select the way you want to add the MCR in your package. Its up to
you which way you prefer then select that opton and click OK on the dialog. The following
snapshot shows the dialog which appears:

Figure 3-11: Diferent optons for adding MCR in a project in MATLAB













Afer fnishing all the above steps the project is ready to be built to convert m-fle into .NET
assembly which can be used in .NET solutons. In order to build the project click on build
buton on the .NET Assembly window as shown below:

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











The alternatve way of building the project is to go to acton buton on the .NET Assembly
window and click on build buton there. The snapshot for this is shown below:

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












When the build buton is clicked then start building and takes sometme depending upon
the computer in use. The following window appears when the build buton is clicked:


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












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 afer correcton re-building of project succeeds.










Now the fles which are built by this process are ready to be used within a .NET soluton 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 functon as a method in C#
Following are the steps to use MATLAB generated dll-fle 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 stuf rather then I will
put my concentraton on using MATLAB built dll-fles in your C# project and will demonstrate
with the help of snapshots that how to add references, defne namespace, creatng 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 secton.
I just opened a console applicaton named DotNetConsole. The snapshot below shows the
inital screen which appears when a console project is created:


Figure 3-17: Inital window in Visual C# 2010 for a console applicaton

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-fle in C# project





By clicking the Add Reference opton, Add Reference window appears. Select the
Browse tab on this window then browse distrib folder within the locaton where MATLAB
created project is located to select the dll-fle and then click OK fnally to add the selected
dll-fle into the C# project. In my case I will choose DotNetProject.dll fle which I created in
MATLAB. Following snapshots shows this:

Figure 3-19: Add Reference window for browsing and selectng dll-fle need to be added
to a C# project

When OK is clicked on the Add Reference window shown above then dll fle is added to
the references of the project which can be seen below the References label within the
Soluton Explorer window in C# project as shown in the below snapshot:

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














In order to use MATLAB functons which input data from C# and afer processing output the
result to C# require adding another dll-fle called MWArray.dll which allows using MATLAB
data types variables in C# just as natve data types in presence of this dll. This dll-fle is located
in the installaton folder of MATLAB. The version of MATLAB whos MWArray.dll is added
must be the same as the MATLAB with which m-fle was converted to a dll-fle otherwise this
will generate error. In case that you dont have MATLAB installed then you need to install MCR
(MATLAB Compiler Runtme) and MWArray.dll can be found then in installaton folder of
MCR. The version of MCR must also correspond to the version of MATLAB with which m-fle
was converted to a dll-fle. The informaton about the version of MCR which is needed can be
found along with other fles in the readme.txt fle in the folder. The following snapshot
shows the readme.txt fle along with MATLAB created dll-fles:

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





Now use the same procedure to add MWArray.dll as described above for
DotNetProject.dll and then it can also be seen in the Soluton Explorer window below
References label in the C# project as shown in the following snapshot:

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












Now afer adding the dll-fles we need to add namespaces in the .cs (C# source code) fle in
which we want to use MATLAB functon. Following is the snapshot afer adding namespaces
in Program.cs:

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

The upper two red marked namespaces are for MWArray.dll and the lower one red marked
namespace is for my built DotNetProject.dll. Afer this the MATLAB class and functon which
we built are ready to be used in C# program.
Now I will defne 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
functon stability with input parameters of type MWNumericArray just as calling a natve
method of a class in C#. I will not go in much depth of C# classes, objects and methods because
very good informaton is available about this on internet. Following snapshot shows the way
of defning variables of type MWNumericArray, assigning them value, creaton of an object
test of class DotNetClass which we created in MATLAB and then calling the stability
method of this class.




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

The upper marked block shows way of defning variables of type MWNumericArray, middle
marked block shows assigning values to these variables and the lower block shows creaton
of an object of class DotNetClass and calling the MATLAB fle stability.m as a method of
DotNetClass.

Das könnte Ihnen auch gefallen