Skip to Main Content Skip to Search
Product Documentation

Using MATLAB Global Variables in Microsoft Visual Basic

Class properties allow an object to retain an internal state between method calls.

Global variables are variables that are declared in the MATLAB product with the global keyword. The builder automatically converts all global variables shared by the MATLAB files that make up a class to properties on that class.

Properties are particularly useful when you have a large array containing values that do not change often, but you need to operate on it frequently. In this case, you can set the array once as a class property and operate on it repeatedly without incurring the overhead of passing (and converting) the data for passing to each method every time it is called.

The following example shows how to use 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 (coded as A in MATLAB) as a class property so that it does not need to be passed to the factorization routines.

Consider these three MATLAB files.

Cholesky.m

function [L] = Cholesky()
    global A;
    if (isempty(A))
        L = [];
        return;
    end
    L = chol(A);

LUDecomp.m

function [L,U] = LUDecomp()
    global A;
    if (isempty(A))
        L = [];
        U = [];
        return;
    end
    [L,U] = lu(A);

QRDecomp.m

function [Q,R] = QRDecomp()
    global A;
    if (isempty(A))
        Q = [];
        R = [];
        return;
    end
    [Q,R] = qr(A);

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:

  1. Create a new MATLAB Builder NE project named mymatrix with a version of 1.0.

  2. Add a single class called myfactor to the component.

  3. Add the above three MATLAB files to the class.

  4. Build the component.

To test your application, make sure that you reference the library generated by the builder in the current Visual Basic project:

  1. Select Project > References in the Visual Basic main menu.

  2. Click mymatrix 1.0 Type Library.

Use the following Visual Basic subroutine to test the myfactor class:

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

Run the subroutine, which does the following:

  1. Creates an instance of the myfactor class

  2. Assigns a double matrix to the property A

  3. Calls the three factorization methods

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS