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 Variants 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.

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 Variants with each element containing a 1-by-1 Double, while var2 is a 1-by-1 Variant containing a 2-by-2 array of Doubles. 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 Variants (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