| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB Builder EX |
| Contents | Index |
| Learn more about MATLAB Builder EX |
| On this page… |
|---|
Before calling a class method (compiled MATLAB function), you must create an instance of the class that contains the method. VBA provides two techniques for doing this:
CreateObject function
New operator
This method uses the Microsoft Visual Basic application programming interface (API) CreateObject function to create an instance of the class. To use this method, Dim a variable of type Object to hold a reference to the class instance and call CreateObject using the class programmatic identifier (ProgID) as an argument, as shown in the next example:
Function foo(x1 As Variant, x2 As Variant) As Variant
Dim aClass As Object
On Error Goto Handle_Error
Set aClass = CreateObject("mycomponent.myclass.1_0")
' (call some methods on aClass)
Exit Function
Handle_Error:
foo = Err.Description
End Function
This method uses the Visual Basic New operator on a variable explicitly dimensioned as the class to be created. Before using this method, you must reference the type library containing the class in the current VBA project. Do this by selecting the Tools menu from the Visual Basic Editor, and then selecting References to display the Available References list. From this list, select the necessary type library.
The following example illustrates using the New operator to create a class instance. It assumes that you have selected mycomponent 1.0 Type Library from the Available References list before calling this function.
Function foo(x1 As Variant, x2 As Variant) As Variant Dim aClass As mycomponent.myclass On Error Goto Handle_Error Set aClass = New mycomponent.myclass ' (call some methods on aClass) Exit Function Handle_Error: foo = Err.Description End Function
In this example, the class instance can be dimensioned as simply myclass. The full declaration in the form <component-name>.<class-name> guards against name collisions that can occur if other libraries in the current project contain types named myclass.
Both methods are equivalent in functionality. The first method does not require a reference to the type library in the VBA project, while the second results in faster code execution. The second method has the added advantage of enabling the Auto-List-Members and Auto-Quick-Info capabilities of the Microsoft Visual Basic editor to work with your classes. The default function wrappers created with each built component all use the first method for object creation.
In the previous two examples, the class instance used to make the method call was a local variable of the procedure. This creates and destroys a new class instance for each call. An alternative approach is to declare one single module-scoped class instance that is reused by all function calls, as in the initialization code of the previous example.
The following example illustrates this technique with the second method:
Dim aClass As mycomponent.myclass
Function foo(x1 As Variant, x2 As Variant) As Variant
On Error Goto Handle_Error
If aClass Is Nothing Then
Set aClass = New mycomponent.myclass
End If
' (call some methods on aClass)
Exit Function
Handle_Error:
foo = Err.Description
End Function
MATLAB Builder EX creates a single MATLAB Compiler Runtime (MCR) when the first Microsoft COM class is instantiated in an application. This MCR is reused and shared among all subsequent class instances within the component, resulting in more efficient memory usage and eliminating the MCR startup cost in each subsequent class instantiation.
All class instances share a single MATLAB workspace and share global variables in the M-files used to build the component. This makes properties of a COM class behave as static properties instead of instance-wise properties.
![]() | Initializing MATLAB Builder EX Libraries with Microsoft Excel | Calling the Methods of a Class Instance | ![]() |

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 |