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 (
Factor and using the
library to handle data conversion
For information about these data conversion classes, see the MATLAB
MWArray Class Library Reference, available in the
matlabroot represents your MATLAB installation
How to build and run the
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.
If you have not already done so, copy the files for this example as follows:
Copy the following folder that ships with the MATLAB product to your work folder:
At the MATLAB command prompt,
MatrixMathExample subfolder in your work
Write the MATLAB functions as you would any MATLAB function.
The code for the
qrdecomp functions is already in your work
From the MATLAB apps gallery, open the Library Compiler app.
Build the .NET component. See the instructions in Generate a .NET Assembly and Build a .NET Application for more details. Use the following information:
|Files to compile|
Write source code for an application that accesses the component.
The sample application for this example is in
The program listing is shown here.
Factor factor= new Factor();
creates an instance of the class
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); ...
See Understanding the MatrixMath Program for more details about the structure of this program.
MatrixMathCSApp folder contains
Studio .NET project file for this example. Open the project
Studio .NET by double-clicking
MatrixMathCSApp.csproj in Windows® Explorer.
You can also open it from the desktop by right-clicking MatrixMathCSApp.csproj > Open
Add a reference to the
See Supported Microsoft .NET Framework Versions for
a list of supported framework versions.
If necessary, add (or fix the location of) a reference
MatrixMathComp component which you built
in a previous step. (The component,
is in the
of your work area.)
Build and run the application in Visual Studio .NET.
The following code defines the MATLAB functions used in the example.
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
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
qrdecomp methods. This part is executed inside
try block. This is done so that if an exception
occurs during execution, the corresponding
will be executed.
The second part is the
The code prints a message to standard output to let the user know
about the error that has occurred.
The third part is a
to manually clean up native resources before exiting.
This optional as the garbage collector will automatically clean-up resources for you.