MATLAB COM Builder    

Class MWStruct

The MWStruct class passes or receives a Struct type to or from a compiled class method. This class contains seven properties/methods:

Sub Initialize([varDims], [varFieldNames])

This method allocates a structure array with a specified number and size of dimensions and a specified list of field names.

Parameters.   
Argument
Type
Description
varDims
Variant
Optional array of dimensions
varFieldNames
Variant
Optional array of field names

Return Value. .   None.

Remarks.   When created, an MWStruct object has a dimensionality of 1-by-1 and no fields. The Initialize method dimensions the array and adds a set of named fields to each element. Each time you call Initialize on the same object, it is redimensioned. If you do not supply the varDims argument, the existing number and size of the array's dimensions unchanged. If you do not supply the varFieldNames argument, the existing list of fields is not changed. Calling Initialize with no arguments leaves the array unchanged.

Example.   The following Visual Basic code illustrates use of the Initialize method to dimension struct arrays.

Property Item([i0], [i1], ..., [i31]) As MWField

The Item property is the default property of the MWStruct class. This property is used to set/get the value of a field at a particular index in the structure array.

Parameters.   
Argument
Type
Description

i0,i1, ..., i31

Variant
Optional index arguments. Between 0 and 32 index arguments can be entered. To reference an element of the array, specify all indexes as well as the field name.

Remarks.   When accessing a named field through this property, you must supply all dimensions of the requested field as well as the field name. This property always returns a single field value, and generates a bad index error if you provide an invalid or incomplete index list. Index arguments have four basic formats:

This format accesses array elements through a single subscripting notation. A single numeric index n followed by the field name returns the named field on the nth array element, navigating the array linearly in column-major order. For example, consider a 2-by-2 array of structures with fields "red", "green", and "blue" stored in a variable x. These two statements are equivalent:

This format accesses an array element of an multidimensional array by specifying n indices. These statements access all four of the elements of the array in the previous example:

This format accesses an array element by passing an array of indices and a field name. The next example rewrites the previous example using an index array:

With these four formats, the Item property provides a very flexible indexing mechanism for structure arrays. Also note:

The last statement resolves to:

Property NumberOfFields As Long

The read-only NumberOfFields property returns the number of fields in the structure array.

Property NumberOfDims As Long

The read-only NumberOfDims property returns the number of dimensions in the struct array.

Property Dims As Variant

The read-only Dims property returns an array of length NumberOfDims that contains the size of each dimension of the struct array.

Property FieldNames As Variant

The read-only FieldNames property returns an array of length NumberOfFields that contains the field names of the elements of the structure array.

Example.   The next Visual Basic code sample illustrates how to access a two-dimensional structure array's fields when the field names and dimension sizes are not known in advance.

Sub Clone(ppStruct As MWStruct)

Creates a copy of an MWStruct object.

Parameters.   
Argument
Type
Description
ppStruct
MWStruct

Reference to an uninitialized MWStruct object to receive the copy.

Return Value.   None

Remarks.   Clone allocates a new MWStruct object and creates a deep copy of the object's contents. Call this function when a separate object is required instead of a shared copy of an existing object reference.

Example.   The following Visual Basic example illustrates the difference between assignment and Clone for MWStruct objects.


  Class MWFlags Class MWField