Top Banner

of 19

MATLAB - C# Interfacing

Jun 02, 2018

Download

Documents

cavanzas
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 8/11/2019 MATLAB - C# Interfacing

    1/19

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

    the form of dll (Dynamic Link Library) le using MATLAB

    Following are the steps to convert m-le into dll-le 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

  • 8/11/2019 MATLAB - C# Interfacing

    2/19

    Aer 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

  • 8/11/2019 MATLAB - C# Interfacing

    3/19

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

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

  • 8/11/2019 MATLAB - C# Interfacing

    4/19

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

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

    can be added depending upon the user requirements. Browse the path for the les 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-le to the project in MATLAB

  • 8/11/2019 MATLAB - C# Interfacing

    5/19

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

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

    To see the les which will be created within the package aer building the project click on

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

    les which will be created in my DotNetProject:

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

  • 8/11/2019 MATLAB - C# Interfacing

    6/19

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

    (MATLAB Compiler Runme) 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 acon buon on the .NET

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

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

  • 8/11/2019 MATLAB - C# Interfacing

    7/19

    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 opon and click OK on the dialog. The following

    snapshot shows the dialog which appears:

    Figure 3-11: Dierent opons for adding MCR in a project in MATLAB

  • 8/11/2019 MATLAB - C# Interfacing

    8/19

    Aer nishing all the above steps the project is ready to be built to convert m-le into .NET

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

    buon on the .NET Assembly window as shown below:

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

  • 8/11/2019 MATLAB - C# Interfacing

    9/19

    The alternave way of building the project is to go to acon buon on the .NET Assembly

    window and click on build buon there. The snapshot for this is shown below:

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

  • 8/11/2019 MATLAB - C# Interfacing

    10/19

    When the build buon is clicked then start building and takes someme depending upon

    the computer in use. The following window appears when the build buon is clicked:

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

  • 8/11/2019 MATLAB - C# Interfacing

    11/19

    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 aer correcon re-building of project succeeds.

  • 8/11/2019 MATLAB - C# Interfacing

    12/19

    Now the les which are built by this process are ready to be used within a .NET soluon 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 funcon as a method in C#

    Following are the steps to use MATLAB generated dll-le 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 stu rather then I will

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

    with the help of snapshots that how to add references, dene namespace, creang 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 secon.

    I just opened a console applicaon named DotNetConsole. The snapshot below shows the

    inial screen which appears when a console project is created:

  • 8/11/2019 MATLAB - C# Interfacing

    13/19

    Figure 3-17: Inial window in Visual C# 2010 for a console applicaon

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

  • 8/11/2019 MATLAB - C# Interfacing

    14/19

    By clicking the Add Reference opon, Add Reference window appears. Select the

    Browse tab on this window then browse distrib folder within the locaon where MATLAB

    created project is located to select the dll-le and then click OK nally to add the selected

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

    MATLAB. Following snapshots shows this:

    Figure 3-19: Add Reference window for browsing and selecng dll-le need to be added

    to a C# project

  • 8/11/2019 MATLAB - C# Interfacing

    15/19

    When OK is clicked on the Add Reference window shown above then dll le is added to

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

    Soluon Explorer window in C# project as shown in the below snapshot:

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

  • 8/11/2019 MATLAB - C# Interfacing

    16/19

    In order to use MATLAB funcons which input data from C# and aer processing output the

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

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

    in the installaon folder of MATLAB. The version of MATLAB whos MWArray.dll is added

    must be the same as the MATLAB with which m-le was converted to a dll-le otherwise thiswill generate error. In case that you dont have MATLAB installed then you need to install MCR

    (MATLAB Compiler Runme) and MWArray.dll can be found then in installaon folder of

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

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

    found along with other les in the readme.txt le in the folder. The following snapshot

    shows the readme.txt le along with MATLAB created dll-les:

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

    MATLAB generated dll-les

  • 8/11/2019 MATLAB - C# Interfacing

    17/19

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

    DotNetProject.dll and then it can also be seen in the Soluon Explorer window below

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

    Figure 3-22: MWArray.dll which is key for communicaon between MATLAB and C# for

    input and output data both ways

  • 8/11/2019 MATLAB - C# Interfacing

    18/19

    Now aer adding the dll-les we need to add namespaces in the .cs (C# source code) le in

    which we want to use MATLAB funcon. Following is the snapshot aer adding namespaces

    in Program.cs:

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

    The upper two red marked namespaces are for MWArray.dll and the lower one red markednamespace is for my built DotNetProject.dll. Aer this the MATLAB class and funcon which

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

    Now I will dene 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

    funcon stability with input parameters of type MWNumericArray just as calling a nave

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

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

    of dening variables of type MWNumericArray, assigning them value, creaon of an object

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

    method of this class.

  • 8/11/2019 MATLAB - C# Interfacing

    19/19

    Figure 3-24: Dening variables of MATLAB supported data type, creaon of an object of

    MATLAB generated class and using its funcon as method in C#

    The upper marked block shows way of dening variables of type MWNumericArray, middle

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

    of an object of class DotNetClass and calling the MATLAB le stability.m as a method of

    DotNetClass.