MATLAB COM Builder | ![]() ![]() |
Adding Class Properties to COM Builder Objects
Class properties allow an object to retain an internal state between method calls. MATLAB COM Builder automatically converts all global variables shared by the M-files that make up a class to properties on that class. Global variables are variables that are declared with the global keyword. Properties are particularly useful when you have a large array that doesn't change often, but you need to operate on it frequently. In this case, the array can be set once as a class property and operated on repeatedly without incurring the overhead of passing (and converting) the data into each method every time it is called.
The next example illustrates using a class property in a matrix factorization class. The example develops a class that performs Cholesky, LU, and QR factorizations on the same matrix. It stores the input matrix as a class property so that it won't need to be passed to our factorization routines.
Cholesky.m
LUDecomp.m
QRDecomp.m
These three files share a common global variable A
. Each function performs a matrix factorization on A
and returns the results.
To build the class, create a new COM Builder project named mymatrix
with a version of 1.0 and add a single class called myfactor
to the component. Add the above three M-files to the class and build.
Use the following Visual Basic subroutine to test the myfactor
class. First set a project reference to mymatrix 1.0 Type Library under Project->References in the Visual Basic main menu. Then run this subroutine, which creates an instance of the myfactor
class and assigns a double matrix to the property A
. Finally, it calls the three factorization methods.
Sub TestFactor() Dim x(1 To 2, 1 To 2) As Double Dim C As Variant, L As Variant, U As Variant, _ Q As Variant, R As Variant Dim factor As myfactor On Error GoTo Handle_Error Set factor = New myfactor x(1, 1) = 2# x(1, 2) = -1# x(2, 1) = -1# x(2, 2) = 2# factor.A = x Call factor.cholesky(1, C) Call factor.ludecomp(2, L, U) Call factor.qrdecomp(2, Q, R) Exit Sub Handle_Error: MsgBox (Err.Description) End Sub
![]() | Overview | Adding Events to COM Builder Objects | ![]() |