| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB Builder NE |
| Contents | Index |
| Learn more about MATLAB Builder NE |
| On this page… |
|---|
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 complete reference information about the MWArray class hierarchy, see the MWArray Class Library Reference (available online only). |
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.
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:
matlabroot\toolbox\dotnetbuilder\Examples\VS8\NET\MatrixMathExample
At the MATLAB command prompt, cd to the new MatrixMathExample subfolder in your work folder.
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\.
While in MATLAB, issue the following command to open the Deployment Tool:
deploytool
Build the .NET component. See the instructions in Building Your Component for more details. Use the following information:
| Project Name | MatrixMathComp |
| Class Name | Factor |
| Files to compile | cholesky.mludecomp.mqrdecomp.m |
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.
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. |
Build the MatrixMathApp application using Visual Studio .NET.
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 MATLAB desktop by right-clicking MatrixMathCSApp.csproj > Open Outside MATLAB.
Add a reference to the MWArray component,
which is matlabroot\toolbox\dotnetbuilder\bin\architecture\framework_version
\mwarray.dll.
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.)
Build and run the application in Visual Studio .NET.
The following code defines the MATLAB functions used in the example.
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.
![]() | Spectral Analysis Example | Phonebook Example | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |