Main Content

Integrate Simple MATLAB Function into .NET Application

Note

The examples for the MATLAB® Compiler SDK™ product are in matlabroot\toolbox\dotnetbuilder\Examples\VSVersion\NET, where VSVersion specifies the version of Microsoft® Visual Studio® .NET you are using. You can load projects for all the examples by opening the following solution in Visual Studio:

matlabroot\toolbox\dotnetbuilder\Examples\VSVersion\NET\DotNetExamples.sln

The Simple Plot example shows you how to create a .NET assembly that calls a MATLAB function to display a plot. For an example that uses a MATLAB function to modify a structure array, see Phone Book.

In the following examples, you perform these steps to integrate a MATLAB function into a .NET application:

  • Use the MATLAB Compiler SDK product to convert a MATLAB function to a method of a .NET class and wrap the class in a .NET assembly.

  • Access the component in either a C# application or a Visual Basic® application by instantiating your .NET class and using the MWArray class library to handle data conversion.

  • Build and run the generated application using the Visual Studio .NET development environment.

Prerequisites

  • Verify that you have met all of the MATLAB Compiler SDK .NET target requirements. For details, see MATLAB Compiler SDK .NET Target Requirements.

  • Verify that you have Microsoft Visual Studio installed.

  • End users must have an installation of MATLAB Runtime to run the application. For details, see Download and Install MATLAB Runtime.

    For testing purposes, you can use an installation of MATLAB instead of MATLAB Runtime.

Create Simple Plot

Files

MATLAB Function Locationmatlabroot\toolbox\dotnetbuilder\Examples\VSVersion\NET\PlotExample\PlotComp\drawgraph.m
C# Code Locationmatlabroot\toolbox\dotnetbuilder\Examples\VSVersion\NET\PlotExample\PlotCSApp\PlotApp.cs
Visual Basic Code Locationmatlabroot\toolbox\dotnetbuilder\Examples\VSVersion\NET\PlotExample\PlotVBApp\PlotApp.vb

Procedure

  1. Copy the following folder that ships with the MATLAB product to your work folder:

    matlabroot\toolbox\dotnetbuilder\Examples\VSVersion\NET\PlotExample

    At the MATLAB command prompt, navigate to the PlotExample\PlotComp subfolder in your work folder.

  2. Examine the drawgraph function located in PlotExample\PlotComp.

    function drawgraph(coords)
    plot(coords(1,:), coords(2,:));
    pause(5)
    Test the function at the MATLAB command prompt.

    x = 0:0.01:10;
    y = sin(x);
    z = [x;y];
    drawgraph(z)

    The function outputs a figure that displays a sine wave.

  3. Build the .NET component with the Library Compiler app or compiler.build.dotNETAssembly using the following information:

    FieldValue
    Library NamePlotComp
    Class NamePlotter
    File to Compiledrawgraph.m

    For example, if you are using compiler.build.dotNETAssembly, type:

    buildResults = compiler.build.dotNETAssembly('drawgraph.m', ...
    'AssemblyName','PlotComp', ...
    'ClassName','Plotter');

    For more details, see the instructions in Generate .NET Assembly and Build .NET Application.

  4. Decide whether you are using C# or Visual Basic to access the component.

    • C#

      If you are using C#, write source code for a C# application that accesses the component.

      The sample application for this example is in PlotExample\PlotCSApp\PlotApp.cs.

       PlotApp.cs

      This statement creates an instance of the Plotter class:

      Plotter plotter= new Plotter(); 

      This statement explicitly casts the native plotValues to MWNumericArray and then calls the method drawgraph:

      plotter.drawgraph((MWNumericArray)plotValues);
      
    • Visual Basic

      If you are using Visual Basic, write source code for a Visual Basic application that accesses the component.

      The sample application for this example is in PlotExample\PlotVBApp\PlotApp.vb.

       PlotApp.vb

      This statement creates an instance of the Plotter class:

      Dim plotter As Plotter = New Plotter

      This statement calls the method drawgraph:

      plotter.drawgraph(coords)

    In either case, the PlotApp program does the following:

    • Creates two arrays of double values.

    • Creates a Plotter object.

    • Calls the drawgraph method to plot the equation using the MATLAB plot function.

    • Uses MWNumericArray to represent the data needed by the drawgraph method to plot the equation.

    • Uses a try-catch block to catch and handle any exceptions.

  5. Open the .NET project file that corresponds to your application language using Visual Studio.

    • C#

      If you are using C#, the PlotCSApp folder contains a Visual Studio .NET project file for this example. Open the project in Visual Studio .NET by double-clicking PlotCSApp.csproj in Windows® Explorer. You can also open it from the desktop by right-clicking PlotCSApp.csproj and selecting Open Outside MATLAB.

    • Visual Basic

      If you are using Visual Basic, the PlotVBApp folder contains a Visual Studio .NET project file for this example. Open the project in Visual Studio .NET by double-clicking PlotVBApp.vbproj in Windows Explorer. You can also open it from the desktop by right-clicking PlotVBApp.vbproj and selecting Open Outside MATLAB.

  6. Add a reference to your assembly file PlotComp.dll located in the folder where you generated or installed the assembly.

  7. Add a reference to the MWArray API.

    If MATLAB is installed on your systemmatlabroot\toolbox\dotnetbuilder\bin\win64\<version>\MWArray.dll
    If MATLAB Runtime is installed on your system<MATLAB_RUNTIME_INSTALL_DIR>\toolbox\dotnetbuilder\bin\win64\<version>\MWArray.dll

  8. Build and run the PlotApp application in Visual Studio .NET.

    The application displays the following plot:

    Plot of x squared.

  9. To follow up on this example:

    • Try running the generated application on a different computer.

    • Try building an installer for the package using compiler.package.installer.

    • Try integrating an assembly that consists of multiple functions.

Create Phone Book

In this example, you create a .NET assembly that calls a MATLAB function to modify a structure array that contains phone numbers.

Files

MATLAB Function Locationmatlabroot\toolbox\dotnetbuilder\Examples\VSVersion\NET\PhoneBookExample\PhoneBookComp\makephone.m
C# Code Locationmatlabroot\toolbox\dotnetbuilder\Examples\VSVersion\NET\PhoneBookExample\PhoneBookCSApp\PhoneBookApp.cs
Visual Basic Code Locationmatlabroot\toolbox\dotnetbuilder\Examples\VSVersion\NET\PhoneBookExample\PhoneBookVBApp\PhoneBookApp.vb

Procedure

  1. Copy the following folder that ships with MATLAB to your work folder:

    matlabroot\toolbox\dotnetbuilder\Examples\VSVersion\NET\PhoneBookExample

    At the MATLAB command prompt, navigate to the PhoneBookExample\PhoneBookComp subfolder in your work folder.

  2. Examine the makephone function located in PhoneBookExample\PhoneBookComp.

    function book = makephone(friends)
    book = friends;
    for i = 1:numel(friends)
        numberStr = num2str(book(i).phone);
        book(i).external = ['(508) 555-' numberStr];
    end

    Test the function at the MATLAB command prompt.

    friends(1).name = "Jordan Robert";
    friends(1).phone = 3386;
    friends(2).name = "Mary Smith";
    friends(2).phone = 3912;
    struct2table(makephone(friends))
    ans =
    
      2×3 table
    
             name          phone         external     
        _______________    _____    __________________
    
        "Jordan Robert"    3386     {'(508) 555-3386'}
        "Mary Smith"       3912     {'(508) 555-3912'}
    
  3. Build the .NET component with the Library Compiler app or compiler.build.dotNETAssembly using the following information:

    FieldValue
    Library NamePhoneBookComp
    Class NamePhoneBook
    File to Compilemakephone

    For example, if you are using compiler.build.dotNETAssembly, type:

    buildResults = compiler.build.dotNETAssembly('makephone.m', ...
    'AssemblyName','PhoneBookComp', ...
    'ClassName','PhoneBook');

    For more details, see the instructions in Generate .NET Assembly and Build .NET Application.

  4. Decide whether you are using C# or Visual Basic to access the component.

    • C#

      If you are using C#, write source code for a C# application that accesses the component.

      The sample application for this example is in
      PhoneBookExample\PhoneBookCSApp\PhoneBookApp.cs.

       PhoneBookApp.cs

    • Visual Basic

      If you are using Visual Basic, write source code for a Visual Basic application that accesses the component.

      The sample application for this example is in
      \PhoneBookExample\PhoneBookVBApp\PhoneBookApp.vb.

       PhoneBookApp.vb

    In either case, the PhoneBookApp program does the following:

    • Creates a structure array using MWStructArray to represent the example phonebook data containing names and phone numbers.

    • Instantiates the Phonebook class as thePhonebook object, as shown:
      thePhonebook = new phonebook();

    • Calls the MATLAB function makephone to create a modified copy of the structure by adding an additional field, as shown:
      result = thePhonebook.makephone(1, friends);

    • Displays the resulting structure array.

  5. Open the .NET project file that corresponds to your application language using Visual Studio.

    • C#

      If you are using C#, the PhoneBookCSApp folder contains a Visual Studio .NET project file for this example. Open the project in Visual Studio .NET by double-clicking PhoneBookCSApp.csproj in Windows Explorer. You can also open it from the desktop by right-clicking PhoneBookCSApp.csproj and selecting Open Outside MATLAB.

    • Visual Basic

      If you are using Visual Basic, the PhoneBookVBApp folder contains a Visual Studio .NET project file for this example. Open the project in Visual Studio .NET by double-clicking PhoneBookVBApp.vbproj in Windows Explorer. You can also open it from the desktop by right-clicking PhoneBookVBApp.vbproj and selecting Open Outside MATLAB.

  6. Create a reference to your assembly file PhoneBookComp.dll located in the folder where you generated the assembly.

  7. Create a reference to the MWArray API, which is located in:

    MATLABmatlabroot\toolbox\dotnetbuilder\bin\win64\<version>\MWArray.dll
    MATLAB Runtime<MATLAB_RUNTIME_INSTALL_DIR>\toolbox\dotnetbuilder\bin\win64\<version>\MWArray.dll

  8. Build and run the PhoneBookComp application in Visual Studio .NET.

    The application displays the following output:

    Friends: 
    2x2 struct array with fields:
        name
        phone
    Result: 
    2x2 struct array with fields:
        name
        phone
        external
    Result record 2:
    Mary Smith
    3912
    (508) 555-3912
    
    Entire structure:
    Number of Elements: 4
    Dimensions: 2-by-2
    Number of Fields: 3
    Standard MATLAB view:
    2x2 struct array with fields:
        name
        phone
        external
    Walking structure:
    Element 1
       name: Jordan Robert
       phone: 3386
       external: (508) 555-3386
    Element 2
       name: Mary Smith
       phone: 3912
       external: (508) 555-3912
    Element 3
       name: Stacy Flora
       phone: 3238
       external: (508) 555-3238
    Element 4
       name: Harry Alpert
       phone: 3077
       external: (508) 555-3077

See Also

| |

Related Topics