MATLAB Excel Builder | ![]() ![]() |
Processing varargin and varargout Arguments
When varargin
and/or varargout
are present in the original MATLAB function, these parameters are added to the argument list of the class method as the last input/output parameters in the list. 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.
The following example creates a varargin
array to call a method resulting from a MATLAB function of the form y = foo(varargin)
.
Function foo(x1 As Variant, x2 As Variant, x3 As Varaint, _ 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
The MWUtil
class included in the MWComUtil
utility library provides the MWPack
helper function to create varargin
parameters. See Utility Library for more details.
The next example processes a varargout
parameter into three separate Excel Range
s. This function makes use of the MWUnpack
function in the utility library. The MATLAB function used is varargout = foo(x1,x2)
.
Sub foo(Rout1 As Range, Rout2 As Range, Rout3 As Range, _ Rin1 As Range, Rin2 As Range) 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,Rin1,Rin2) Call aUtil.MWUnpack(v,0,True,Rout1,Rout2,Rout3) Exit Sub Handle_Error: MsgBox(Err.Description) End Sub
![]() | Calling the Methods of a Class Instance | Handling Errors During a Method Call | ![]() |