Data Conversion
Conversion Rules
This section describes the data conversion rules for COM components created with the MATLAB® Compiler SDK™ product. These components are dual interface COM objects that support data types compatible with Automation.
Note
Automation (formerly called OLE Automation) is a technology that allows software packages to expose their unique features to scripting tools and other applications. Automation uses the Component Object Model (COM), but may be implemented independently from other OLE features, such as in-place activation.
Caution
Be aware that IIS (Internet Information Service) usually
prevents most COM automation on the basis that it may pose a
security risk. Therefore, XLSREAD
and
other Automation services may fail when served by IIS,
leading to errors such as object reference not
set
.
When a method is invoked on a MATLAB Compiler SDK component, the input parameters are converted to MATLAB internal array format and passed to the compiled MATLAB function. When the function exits, the output parameters are converted from MATLAB internal array format to COM Automation types.
The COM client passes all input and output arguments in the compiled
MATLAB functions as type VARIANT
. The COM
VARIANT
type is a union of several simple
data types. A type VARIANT
variable can store a
variable of any of the simple types, as well as arrays of any of
these values.
The Win32 API provides many functions for creating and manipulating
VARIANT
s in C/C++, and Microsoft®
Visual Basic® provides native language support for this type. See
the Microsoft
Visual Studio® documentation for definitions and API support for COM
VARIANT
s. VARIANT
variables are self describing and store their type code as an
internal field of the structure.
Note
This discussion of data refers to both
VARIANT
and
Variant
data types.
VARIANT
is the C++ name and
Variant
is the corresponding data
type in Visual Basic.
See VARIANT Type Codes Supported for a list of the VARIANT
type codes supported
by compiler components.
See MATLAB to COM VARIANT Conversion Rules and COM VARIANT to MATLAB Conversion Rules for conversion rules between COM VARIANT
s and
MATLAB arrays.
VARIANT Type Codes Supported
VARIANT Type
Code (C/C++) | C/C++ Type | Variant Type Code (Visual Basic) | Visual Basic Type | Definition |
---|---|---|---|---|
VT_EMPTY | - | vbEmpty | - | Uninitialized
|
VT_I1 | char | - | - | Signed one-byte character |
VT_UI1 | unsigned
| vbByte | Byte | Unsigned one-byte character |
VT_I2 | short | vbInteger | Integer | Signed two-byte integer |
VT_UI2 | unsigned short | - | - | Unsigned two-byte integer |
VT_I4 | long | vbLong | Long | Signed four-byte integer |
VT_UI4 | unsigned long | - | - | Unsigned four-byte integer |
VT_R4 | float | vbSingle | Single | IEEE® four-byte floating-point value |
VT_R8 | double | vbDouble | Double | IEEE eight-byte floating-point value |
VT_CY | CY + | vbCurrency | Currency | Currency value (64-bit integer, scaled by 10,000) |
VT_BSTR | BSTR + | vbString | String | String value |
VT_ERROR | SCODE + | vbError | - |
|
VT_DATE | DATE + | vbDate | Date | Eight-byte floating-point value representing date and time |
VT_INT | int | - | - | Signed integer; equivalent to type
|
VT_UINT | unsigned int | - | - | Unsigned integer; equivalent to type
|
VT_DECIMAL | DECIMAL + | vbDecimal | - | 96-bit (12-byte) unsigned integer, scaled by a variable power of 10 |
VT_BOOL | VARIANT_BOOL + | vbBoolean | Boolean | Two-byte Boolean value (0xFFFF = True; 0x0000 = False) |
VT_DISPATCH | IDispatch* | vbObject | Object |
|
VT_VARIANT | VARIANT + | vbVariant | Variant |
|
<anything>|VT_ARRAY | Bitwise combine
| |||
<anything>|VT_BYREF | Bitwise combine
| |||
+ Denotes Windows® specific type. Not part of standard C/C++. |
MATLAB to COM VARIANT Conversion Rules
MATLAB Data Type | VARIANT Type for Scalar Data | VARIANT Type for Array Data | Comments |
---|---|---|---|
cell | A 1-by-1 cell array converts to a
single | A multidimensional cell array
converts to a | |
structure |
|
| A MATLAB
|
char | A 1-by-1 | A 1-by-L | Arrays of strings are not supported
as |
sparse |
|
| A MATLAB sparse array is converted to an
|
double | A real 1-by-1 double matrix converts
to a | A real multidimensional double matrix
converts to a | Complex arrays are passed to and from
compiled MATLAB functions using the
|
single | A real 1-by-1 single matrix converts
to a | A real multidimensional single matrix
converts to a | Complex arrays are passed to and from
compiled MATLAB functions using the
|
int8 | A real 1-by-1 | A real multidimensional
| Complex arrays are passed to and from
compiled MATLAB functions using the
|
uint8 | A real 1-by-1
| A real multidimensional
| Complex arrays are passed to and from
compiled MATLAB functions using the
|
int16 | A real 1-by-1
| A real multidimensional
| Complex arrays are passed to and from
compiled MATLAB functions using the
|
uint16 | A real 1-by-1
| A real multidimensional
| Complex arrays are passed to and from
compiled MATLAB functions using the
|
int32 | A 1-by-1 | A multidimensional
| Complex arrays are passed to and from
compiled MATLAB functions using the
|
uint32 | A 1-by-1 | A multidimensional
| Complex arrays are passed to and from
compiled MATLAB functions using the
|
Function handle |
|
| Not supported |
Java® class |
|
| Not supported |
User class |
|
| Not supported |
logical |
|
|
COM VARIANT to MATLAB Conversion Rules
VARIANT Type | MATLAB Data Type (Scalar or Array Data) | Comments |
---|---|---|
VT_EMPTY | N/A | Empty array created. |
VT_I1 | int8 |
|
VT_UI1 | uint8 |
|
VT_I2 | int16 |
|
VT_UI2 | uint16 |
|
VT_I4 | int32 |
|
VT_UI4 | uint32 |
|
VT_R4 | single |
|
VT_R8 | double |
|
VT_CY | double |
|
VT_BSTR | char | A |
VT_ERROR | int32 |
|
VT_DATE | double |
|
VT_INT | int32 |
|
VT_UINT | uint32 |
|
VT_DECIMAL | double |
|
VT_BOOL | logical |
|
VT_DISPATCH | Varies |
Currently, support exists
for Excel®
|
anything |VT_BYREF | Varies | Pointers to any of the basic types are processed according to the rules for what they point to. The resulting MATLAB array contains a deep copy of the values. |
anything |VT_ARRAY | Varies | Multidimensional
|
Array Formatting Flags
The components have flags that control how array data is formatted in both directions. Generally, you should develop client code that matches the intended inputs and outputs of the MATLAB functions with the corresponding methods on the compiled COM objects, in accordance with the rules listed in MATLAB to COM VARIANT Conversion Rules and COM VARIANT to MATLAB Conversion Rules. In some cases this is not possible, for example, when existing MATLAB code is used in conjunction with a third-party product like Excel.
The following table shows the array formatting flags.
Array Formatting Flags
Flag | Description |
---|---|
| Defines the array formatting rule
used on input arrays.
|
| Sets the input array indirection
level used with the
|
| Defines the array formatting rule
used on output arrays. An output array is a
MATLAB array, created by the compiled COM
object, sent as an output parameter from a method
call to the client. The values for this flag,
|
| (Applies to nested cell arrays only.)
Output array indirection level used with the
|
| (Applies to Excel ranges only.) When the target output
from a method call is a range of cells in an
Excel worksheet and the output array size
and shape is not known at the time of the call,
set this flag to |
| Set this flag to
|
Data Conversion Flags
MATLAB
Compiler SDK components contain flags to control the conversion of
certain VARIANT
types to MATLAB types. These flags are as follows:
CoerceNumericToType
This flag tells the data converter to convert all numeric
VARIANT
data to one specific
MATLAB type. VARIANT
type codes
affected by this flag are VT_I1
,
VT_UI1
, VT_I2
,
VT_UI2
, VT_I4
,
VT_UI4
, VT_R4
,
VT_R8
, VT_CY
,
VT_DECIMAL
,
VT_INT
,
VT_UINT
,
VT_ERROR
,
VT_BOOL
, and
VT_DATE
. Valid values for this
flag are mwTypeDefault
,
mwTypeChar
,
mwTypeDouble
,
mwTypeSingle
,
mwTypeLogical
,
mwTypeInt8
,
mwTypeUint8
,
mwTypeInt16
,
mwTypeUint16
,
mwTypeInt32
, and
mwTypeUint32
.
The default for this flag, mwTypeDefault
,
converts numeric data according to the rules listed in Data Conversion.
InputDateFormat
This flag tells the data converter how to convert
VARIANT
dates to MATLAB dates. Valid values for this flag are
mwDateFormatNumeric
(default) and
mwDateFormatString
. The default
converts VARIANT
dates according to the
rule listed in VARIANT Type Codes Supported. The
mwDateFormatString
flag converts
a VARIANT
date to its string
representation. This flag only affects
VARIANT
type code
VT_DATE
.
OutputAsDate As Boolean
This flag instructs the data converter to process an output
argument as a date. By default, numeric dates that are
output parameters from compiled MATLAB functions are passed as
Double
s that need to be
decremented by the COM date bias (693960) as well as coerced
to COM dates. Set this flag to True
to
convert all output values of type
Double
.
DateBias As Long
This flag sets the date bias for performing COM to MATLAB numeric date conversions. The default value of
this property is 693960, which represents the difference
between the COM Date
type and MATLAB numeric dates. This flag allows existing
MATLAB code that already performs the increment of
numeric dates by 693960 to be used unchanged with the
components. To process dates with such code, set this
property to 0
.