Multiple MATLAB Functions in a Component Class

Purpose

The purpose of the example is to show you the following:

  • How to assign more than one MATLAB® function to a component class

  • How to access the component in a C# application (MatrixMathApp.cs) by instantiating Factor 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

  • How to build and run the MatrixMathApp application, using the Visual Studio® .NET development environment

This example builds a .NET component to perform matrix math. The example creates a program that performs Cholesky, LU, and QR factorizations on a simple tridiagonal matrix (finite difference matrix) with the following form:

A = [ 2 -1  0  0  0
     -1  2 -1  0  0
      0 -1  2 -1  0
      0  0 -1  2 -1
      0  0  0 -1  2 ]

You supply the size of the matrix on the command line, and the program constructs the matrix and performs the three factorizations. The original matrix and the results are printed to standard output. You may optionally perform the calculations using a sparse matrix by specifying the string "sparse" as the second parameter on the command line.

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\MatrixMathExample
      
    2. At the MATLAB command prompt, cd to the new MatrixMathExample subfolder in your work folder.

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

    The code for the cholesky, ludecomp, and qrdecomp functions is already in your work folder in MatrixMathExample\MatrixMathComp\.

  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 NameMatrixMathComp
    Class NameFactor
    Files to compilecholesky    ludecomp    qrdecomp

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

    The sample application for this example is in MatrixMathExample\MatrixMathCSApp\MatrixMathApp.cs.

    The program listing is shown here.

     MatrixMathApp.cs

    The statement

     Factor factor= new Factor(); 

    creates an instance of the class Factor.

    The following statements call the methods that encapsulate the MATLAB functions:

    argOut= factor.cholesky((MWArray)matrix);
    ...
    argsOut= factor.ludecomp(2, matrix);
    ...
    argsOut= factor.qrdecomp(2, matrix);
    ...

    Note

    See Understanding the MatrixMath Program for more details about the structure of this program.

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

    1. The MatrixMathCSApp folder contains a Visual Studio .NET project file for this example. Open the project in Visual Studio .NET by double-clicking MatrixMathCSApp.csproj in Windows® Explorer. You can also open it from the desktop by right-clicking MatrixMathCSApp.csproj > Open Outside MATLAB.

    2. Add a reference to the MWArray component, which is matlabroot\toolbox\dotnetbuilder\bin\architecture\framework_version
      \mwarray.dll.
      See Supported Microsoft .NET Framework Versions for a list of supported framework versions.

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

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

MATLAB Functions to Be Encapsulated

The following code defines the MATLAB functions used in the example.

 cholesky.m

 ludecomp.m

 qrdecomp.m

Understanding the MatrixMath Program

The MatrixMath program takes one or two arguments from the command line. The first argument is converted to the integer order of the test matrix. If the string sparse is passed as the second argument, a sparse matrix is created to contain the test array. The Cholesky, LU, and QR factorizations are then computed and the results are displayed.

The main method has three parts:

  • The first part sets up the input matrix, creates a new factor object, and calls the cholesky, ludecomp, and qrdecomp methods. This part is executed inside of a try block. This is done so that if an exception occurs during execution, the corresponding catch block will be executed.

  • The second part is the catch block. The code prints a message to standard output to let the user know about the error that has occurred.

  • The third part is a finally block to manually clean up native resources before exiting.

    Note

    This optional as the garbage collector will automatically clean-up resources for you.