MATLAB COM Builder | ![]() ![]() |
Array Formatting Flags
Array formatting flags guide the data conversion to produce either a MATLAB cell array or matrix from general Variant
data on input or to produce an array of Variant
s or a single Variant
containing an array of a basic type on output.
The following examples assume that you have referenced the MWComUtil
library in the current project by selecting Tools -> References... and selecting MWComUtil 1.0 Type Library from the list.
Sub foo( ) Dim aClass As mycomponent.myclass Dim var1(1 To 2, 1 To 2), var2 As Variant Dim x(1 To 2, 1 To 2) As Double Dim y1,y2 As Variant On Error Goto Handle_Error var1(1,1) = 11# var1(1,2) = 12# var1(2,1) = 21# var1(2,2) = 22# x(1,1) = 11 x(1,2) = 12 x(2,1) = 21 x(2,2) = 22 var2 = x Set aClass = New mycomponent.myclass Call aClass.foo(1,y1,var1) Call aClass.foo(1,y2,var2) Exit Sub Handle_Error: MsgBox(Err.Description) End Sub
Here, two Variant
variables, var1
and var2
are constructed with the same numerical data, but internally they are structured differently. var1
is a 2-by-2 array of Variant
s with each element containing a 1-by-1 Double
, while var2
is a 1-by-1 Variant
containing a 2-by-2 array of Double
s. According to the default data conversion rules listed in Table B-3, COM VARIANT to MATLAB Conversion Rules,, var1
converts to a 2-by-2 cell array with each cell occupied by a 1-by-1 double
, and var2
converts directly to a 2-by-2 double matrix. The InputArrayFormat
flag controls how arrays of these two types are handled. As it turns out, the two arrays in the previous example both convert to double matrices because the default value for the InputArrayFormat
flag is mwArrayFormatMatrix
. This default is used because, as it turns out, array data originating from Excel ranges is always in the form of an array of Variant
s (like var1
of the previous example), and MATLAB functions most often deal with matrix arguments. But what if you really want a cell array? In this case, you set the InputArrayFormat
flag to mwArrayFormatCell
. Do this by adding the following line after creating the class and before the method call.
Setting this flag presents all array input to the compiled MATLAB function as cell arrays.
Similarly, you can manipulate the format of output arguments using the OutputArrayFormat
flag. You can also modify array output with the AutoResizeOutput
and TransposeOutput
flags.
AutoResizeOutput
is used for Excel Range
objects passed directly as output parameters. When this flag is set, the target range automatically resizes to fit the resulting array. If this flag is not set, the target range must be at least as large as the output array or the data is truncated.
The TransposeOutput
flag transposes all array output. This flag is useful when dealing with MATLAB functions that output one-dimensional arrays. By default, MATLAB realizes one-dimensional arrays as 1-by-n matrices (row vectors), and you may prefer column output.
![]() | Handling Errors During a Method Call | Data Conversion Flags | ![]() |