Integrating a Simple MATLAB Function

The purpose of these examples is to highlight main steps required for integrating a MATLAB® function.

Simple Plot

Purpose

The drawgraph function displays a plot of input parameters x and y. The purpose of the example is to show you how to:

  • Use the MATLAB Compiler SDK™ product to convert a MATLAB function (drawgraph) to a method of a .NET class (Plotter) and wrap the class in a .NET assembly (PlotComp).

  • Access the component in a C# application (PlotApp.cs) by instantiating the Plotter class and using the MWArray class library to handle data conversion.

    Note

    For information about these data conversion classes, see the MATLAB MWArray Class Library Reference, available in the matlabroot\help\dotnetbuilder\MWArrayAPI folder, where matlabroot represents your MATLAB installation folder

  • Build and run the PlotCSApp application, using the Visual Studio® .NET development environment.

Procedure

  1. If you have not already done so, copy the files for this example as follows:

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

      matlabroot\toolbox\dotnetbuilder\Examples\VSVersion\NET\PlotExample
    2. At the MATLAB command prompt, change folder to the new PlotExample\PlotComp subfolder in your work folder.

  2. Write the drawgraph function as you would any MATLAB function.

    This code is already in your work folder in PlotExample\PlotComp\drawgraph.m.

  3. From the MATLAB apps gallery, open the Library Compiler app.

  4. Build the .NET component. See the instructions in Generate a .NET Assembly and Build a .NET Application for more details. Use the following information:

    Project NamePlotComp
    Class NamePlotter
    File to compiledrawgraph.m

  5. Write source code for a C# application that accesses the component.

    The sample application for this example is in matlabroot\toolbox\dotnetbuilder\Examples\VSVersion\PlotExample
    \PlotCSApp\PlotApp.cs
    .

    The program listing is shown here.

     PlotApp.cs

    The 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.

    The statement

     Plotter plotter= new Plotter(); 

    creates an instance of the Plotter class, and the statement

    plotter.drawgraph((MWNumericArray)plotValues);
    

    explicitly casts the native plotValues to MWNumericArray and then calls the method drawgraph.

  6. Build the PlotCSApp application using Visual Studio .NET.

    1. 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 > Open Outside MATLAB.

    2. Add a reference to the MWArray component, which is matlabroot\toolbox\dotnetbuilder\bin\architecture\framework_version\mwarray.dll.

    3. Add or, if necessary, fix the location of a reference to the PlotComp component which you built in a previous step. (The component, PlotComp.dll, is in the \PlotExample\PlotComp\x86\V2.0\Debug\distrib subfolder of your work area.)

  7. Build and run the application in Visual Studio .NET.

Phone Book

Purpose

Purpose

The makephone function takes a structure array as an input, modifies it, and supplies the modified array as an output.

Note

For information about these data conversion classes, see the MATLAB MWArray Class Library Reference, available in the matlabroot\help\dotnetbuilder\MWArrayAPI folder, where matlabroot represents your MATLAB installation folder

Procedure

  1. If you have not already done so, copy the files for this example as follows:

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

      matlabroot\toolbox\dotnetbuilder\Examples\VSVersion\NET\PhoneBookExample
    2. At the MATLAB command prompt, cd to the new PhoneBookExample subfolder in your work folder.

  2. Write the makephone function as you would any MATLAB function.

    The following code defines the makephone function:

    function book = makephone(friends)
    %MAKEPHONE Add a structure to a phonebook structure
    %   BOOK = MAKEPHONE(FRIENDS) adds a field to its input structure.
    %   The new field EXTERNAL is based on the PHONE field of the original.
    %   Copyright 2006-2012 The MathWorks, Inc.
    
    book = friends;
    for i = 1:numel(friends)
        numberStr = num2str(book(i).phone);
        book(i).external = ['(508) 555-' numberStr];
    end

    This code is already in your work folder in PhoneBookExample\PhoneBookComp\makephone.m.

  3. From the MATLAB apps gallery, open the Library Compiler app.

  4. Build the .NET component. See the instructions in Generate a .NET Assembly and Build a .NET Application for more details. Use the following information:

    Project NamePhoneBookComp
    Class NamePhoneBook
    File to compilemakephone

  5. Write source code for an application that accesses the component.

    The sample application for this example is in matlabroot\toolbox\dotnetbuilder\Examples\VSVersion\NET\
    PhoneBookExample\PhoneBookCSApp\PhoneBookApp.cs
    .

    The program defines a structure array containing names and phone numbers, modifies it using a MATLAB function, and displays the resulting structure array.

    The program listing is shown here.

     PhoneBookApp.cs

    The program does the following:

    • Creates a structure array, using MWStructArray to represent the example phonebook data.

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

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

  6. Build thePhoneBookCSApp application using Visual Studio .NET.

    1. 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 > Open Outside MATLAB.

    2. Add a reference to the MWArray component, which is matlabroot\toolbox\dotnetbuilder\bin\architecture\framework_version
      \mwarray.dll.

    3. If necessary, add (or fix the location of) a reference to the PhoneBookComp component which you built in a previous step. (The component, PhoneBookComp.dll, is in the \PhoneBookExample\PhoneBookComp\x86\V2.0\Debug\distrib subfolder of your work area.)

  7. Build and run the application in Visual Studio .NET.

    The PhoneBookApp program should display the 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