Pass Arguments

Overview

When it encapsulates MATLAB® functions, the MATLAB Compiler SDK™ product adds the MATLAB function arguments to the argument list of the class methods it creates. Thus, if a MATLAB function uses varargin and/or varargout, the compiler adds these arguments to the argument list of the class method. They are added at the end of the argument list for input and output arguments.

You can pass multiple arguments as a varargin array by creating a Variant array, assigning each element of the array to the respective input argument.

See Producing a COM Class for more information about mapping of input and output arguments.

Create and Use a varargin Array in Microsoft Visual Basic Programs

The following example creates a varargin array to call a method encapsulating a MATLAB function of the form y=foo(varargin).

The MWUtil class included in the MWComUtil utility library provides the MWPack helper function to create varargin parameters.

Function foo(x1 As Variant, x2 As Variant, x3 As Variant, _
             x4 As Variant, x5 As Variant) As Variant
   Dim aClass As Object
   Dim v(1 To 5) As Variant
   Dim y As Variant 
   
   On Error Goto Handle_Error
   v(1) = x1
   v(2) = x2
   v(3) = x3
   v(4) = x4
   v(5) = x5
   aClass = CreateObject("mycomponent.myclass.1_0")
   Call aClass.foo(1,y,v)
   foo = y
   Exit Function
Handle_Error:
   foo = Err.Description
End Function

Create and Use varargout in Microsoft Visual Basic Programs

The next example processes a varargout argument as three separate arguments. This function uses the MWUnpack function in the utility library.

The MATLAB function used is varargout=foo(x1,x2).

Sub foo(Xout1 As Variant, Xout2 As Variant, Xout3 As Variant, _
        Xin1 As Variant, Xin2 As Variant)
   Dim aClass As Object
   Dim aUtil As Object
   Dim v As Variant
    
   On Error Goto Handle_Error
   aUtil = CreateObject("MWComUtil.MWUtil")
   aClass = CreateObject("mycomponent.myclass.1_0")
   Call aClass.foo(3,v,Xin1,Xin2)
   Call aUtil.MWUnpack(v,0,True,Xout1,Xout2,Xout3)
   Exit Sub
Handle_Error:
   MsgBox(Err.Description)
End Sub

Pass an Empty varargin From Microsoft Visual Basic Code

In MATLAB, varargin inputs to functions are optional, and may be present or omitted from the function call. However, from Microsoft® Visual Basic®, function signatures are more strict—if varargin is present among the MATLAB function inputs, the VBA call must include varargin, even if you want it to be empty. To pass in an empty varargin, pass the Null variant, which is converted to an empty MATLAB cell array when passed.

Passing an Empty varargin From VBA Code

The following example illustrates how to pass the null variant in order to pass an empty varargin:

Function foo(x1 As Variant, x2 As Variant, x3 As Variant, _ 
             x4 As Variant, x5 As Variant) As Variant 
   Dim aClass As Object 
   Dim v(1 To 5) As Variant 
   Dim y As Variant 
    
   On Error Goto Handle_Error 
   v(1) = x1 
   v(2) = x2 
   v(3) = x3 
   v(4) = x4 
   v(5) = x5 
   aClass = CreateObject("mycomponent.myclass.1_0") 

   'Call aClass.foo(1,y,v) 
   Call aClass.foo(1,y,Null) 

   foo = y 
   Exit Function 
Handle_Error: 
   foo = Err.Description 
End Function
Was this topic helpful?