Assign Multiple MATLAB Functions in Component Class
This example shows you how to create a .NET matrix math program using multiple MATLAB® functions.
In this example, you perform the following steps:
Assign more than one MATLAB function to a component class.
Access the component in a C# application (
MatrixMathApp.cs
) or a Visual Basic® application (MatrixMathApp.vb
) by instantiatingFactor
and using theMWArray
class library to handle data conversion.Build and run the
MatrixMathApp
application using the Visual Studio® .NET development environment.
MatrixMathApp Application
The MatrixMathApp
application 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.
Files
MATLAB Functions | cholesky.m
ludecomp.m qrdecomp.m |
MATLAB Function Location |
|
C# Code Location |
|
Visual Basic Code Location |
|
Procedure
Copy the following folder that ships with the MATLAB product to your work folder:
matlabroot\toolbox\dotnetbuilder\Examples\VSVersion\NET\MatrixMathExample
At the MATLAB command prompt, navigate to the new
MatrixMathExample\MatrixMathComp
subfolder in your work folder.Examine the MATLAB functions
cholesky.m
,ludecomp.m
, andqrdecomp.m
.function [L] = Cholesky(A) L = chol(A);
function [L,U] = LUDecomp(A) [L,U] = lu(A);
function [Q,R] = QRDecomp(A) [Q,R] = qr(A);
Build the .NET component with the Library Compiler app or
compiler.build.dotNETAssembly
using the following information:Field Value Library Name MatrixMathComp
Class Name Factor
Files to compile cholesky
ludecomp
qrdecomp
For example, if you are using
compiler.build.dotNETAssembly
, type:buildResults = compiler.build.dotNETAssembly(["cholesky.m","ludecomp.m","qrdecomp.m"], ... 'AssemblyName','MatrixMathComp', ... 'ClassName','Factor');
For more details, see the instructions in Generate .NET Assembly and Build .NET Application.
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
MatrixMathExample\MatrixMathCSApp\MatrixMathApp.cs
.This statement creates an instance of the class
Factor
:Factor factor= new 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); ...
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
MatrixMathExample\MatrixMathVBApp\MatrixMathApp.vb
.This statement creates an instance of the class
Factor
:Dim factor As Factor = New Factor
The following statements call the methods that encapsulate the MATLAB functions:
argOut = factor.cholesky(matrix) argsOut = factor.ludecomp(2, matrix) argsOut = factor.qrdecomp(2, matrix)
Open the .NET project file that corresponds to your application language using Visual Studio.
C#
If you are using C#, the
MatrixMathCSApp
folder contains a Visual Studio .NET project file for this example. Open the project in Visual Studio .NET by double-clickingMatrixMathCSApp.csproj
in Windows® Explorer. You can also open it from the desktop by right-clicking MatrixMathCSApp.csproj and selecting Open Outside MATLAB.Visual Basic
If you are using Visual Basic, the
MatrixMathVBApp
folder contains a Visual Studio .NET project file for this example. Open the project in Visual Studio .NET by double-clickingMatrixMathVBApp.vbproj
in Windows Explorer. You can also open it from the desktop by right-clicking MatrixMathVBApp.vbproj and selecting Open Outside MATLAB.
Add a reference to your assembly file
MatrixMathComp.dll
located in the folder where you generated the assembly.Add a reference to the
MWArray
API.If MATLAB is installed on your system matlabroot
\toolbox\dotnetbuilder\bin\win64\<framework_version>
\MWArray.dllIf MATLAB Runtime is installed on your system <MATLAB_RUNTIME_INSTALL_DIR>
\toolbox\dotnetbuilder\bin\win64\<framework_version>
\MWArray.dllBuild and run the
MatrixMathApp
application in Visual Studio.The program displays the following output:
Test Matrix: (1,1) 2 (2,1) -1 (1,2) -1 (2,2) 2 (3,2) -1 (2,3) -1 (3,3) 2 (4,3) -1 (3,4) -1 (4,4) 2 Cholesky Factorization: (1,1) 1.4142 (1,2) -0.7071 (2,2) 1.2247 (2,3) -0.8165 (3,3) 1.1547 (3,4) -0.8660 (4,4) 1.1180 LU Factorization: L Matrix: (1,1) 1.0000 (2,1) -0.5000 (2,2) 1.0000 (3,2) -0.6667 (3,3) 1.0000 (4,3) -0.7500 (4,4) 1.0000 U Matrix: (1,1) 2.0000 (1,2) -1.0000 (2,2) 1.5000 (2,3) -1.0000 (3,3) 1.3333 (3,4) -1.0000 (4,4) 1.2500 QR Factorization: Q Matrix: (1,1) -0.8944 (2,1) 0.4472 (1,2) -0.3586 (2,2) -0.7171 (3,2) 0.5976 (1,3) -0.1952 (2,3) -0.3904 (3,3) -0.5855 (4,3) 0.6831 (1,4) 0.1826 (2,4) 0.3651 (3,4) 0.5477 (4,4) 0.7303 R Matrix: (1,1) -2.2361 (1,2) 1.7889 (2,2) -1.6733 (1,3) -0.4472 (2,3) 1.9124 (3,3) -1.4639 (2,4) -0.5976 (3,4) 1.9518 (4,4) 0.9129
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
, andqrdecomp
methods. This part is executed inside of atry
block. This is done so that if an exception occurs during execution, the correspondingcatch
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. This is optional, as the garbage collector will automatically clean up resources for you.
See Also
Library Compiler | compiler.build.dotNETAssembly
| deploytool