Call the Methods of a Class Instance
Standard Mapping Technique
After you create a class instance, you can call the class methods to access the encapsulated MATLAB® functions. The MATLAB Compiler SDK™ product uses a standard technique to map the original MATLAB function syntax to the method's argument list. This standard mapping technique is as follows:
nargout
When a method has output arguments, the first argument is always
nargout
, which is of typeLong
. This input parameter passes the normal MATLABnargout
parameter to the encapsulated function and specifies how many outputs are requested. Methods that do not have output arguments do not pass anargout
argument.Output parameters
Following
nargout
are the output parameters listed in the same order as they appear on the left side of the original MATLAB function.Input parameters
Next come the input parameters listed in the same order as they appear on the right side of the original MATLAB function.
For example, the most generic MATLAB function is:
function [Y1, Y2, ..., varargout] = foo(X1, X2, ..., varargin)
This function maps directly to the following Microsoft® Visual Basic® signature:
Sub foo(nargout As Long, _ Y1 As Variant, _ Y2 As Variant, _ . . varargout As Variant, _ X1 As Variant, _ X2 As Variant, _ . . varargin As Variant)
See Calling Conventions for more details and examples of the standard mapping from MATLAB functions to COM class method calls.
Variant
All input and output arguments are typed as Variant
, the
default Visual Basic data type. The Variant
type can hold any of the
basic Visual Basic types, arrays of any type, and object references. See Data Conversion for details about the conversion of any basic
type to and from MATLAB data types.
In general, you can supply any Visual Basic type as an argument to a class method, with the exception of Visual Basic User Defined Types (UDTs).
When you pass a simple Variant
type as an output parameter, the
called method allocates the received data and frees the original contents of the
Variant
. In this case it is sufficient to dimension each
output argument as a single Variant
. When an object type (like an
Excel®
Range
) is passed as an output parameter, the object reference is
passed in both directions, and the object's Value
property
receives the data.
Pass Input and Output Parameters
The following examples show how to pass input and output parameters to COM component class methods in Visual Basic.
The first example is a function, foo
, that takes two arguments
and returns one output argument. The foo
function dispatches a
call to a class method that corresponds to a MATLAB function of the form function y =
foo(x1,x2)
.
Function foo(x1 As Variant, x2 As Variant) As Variant Dim aClass As Object Dim y As Variant On Error Goto Handle_Error Set aClass = CreateObject("mycomponent.myclass.1_0") Call aClass.foo(1,y,x1,x2) foo = y Exit Function Handle_Error: foo = Err.Description End Function
The second example rewrites the foo
function as a
subroutine:
Sub foo(Xout As Variant, X1 As Variant, X2 As Variant) Dim aClass As Object On Error Goto Handle_Error Set aClass = CreateObject("mycomponent.myclass.1_0") Call aClass.foo(1,Xout,X1,X2) Exit Sub Handle_Error: MsgBox(Err.Description) End Sub