| Contents | Index |
| On this page… |
|---|
The MWUtil class contains a set of static utility methods used in array processing and application initialization. This class is implemented internally as a singleton (only one global instance of this class per instance of Microsoft Excel). It is most efficient to declare one variable of this type in global scope within each module that uses it. The methods of MWUtil are:
The function prototypes use Visual Basic syntax.
Initializes the library with the current instance of Microsoft Excel.
| Argument | Type | Description |
|---|---|---|
pApp | Object | A valid reference to the current Excel application |
Remarks. This function must be called once for each session of Excel that uses COM components created by MATLAB Builder NE. An error is generated if a method call is made to a member class of any MATLAB Builder NE COM component, and the library has not been initialized.
Example. This Visual Basic sample initializes the MWComUtil library with the current instance of Excel. A global variable of type Object named MCLUtil holds an instance of the MWUtil class, and another global variable of type Boolean named bModuleInitialized stores the status of the initialization process. The private subroutine InitModule() creates an instance of the MWComUtil class and calls the MWInitApplication method with an argument of Application. Once this function succeeds, all subsequent calls exit without recreating the object.
Dim MCLUtil As Object
Dim bModuleInitialized As Boolean
Private Sub InitModule()
If Not bModuleInitialized Then
On Error GoTo Handle_Error
If MCLUtil Is Nothing Then
Set MCLUtil = CreateObject("MWComUtil.MWUtil")
End If
Call MCLUtil.MWInitApplication(Application)
bModuleInitialized = True
Exit Sub
Handle_Error:
bModuleInitialized = False
End If
End Sub
Note If you are developing concurrently with multiple versions of MATLAB and MWComUtil.dll, for example, using this syntax: Set MCLUtil = CreateObject("MWComUtil.MWUtil")requires you to recompile your COM modules every time you upgrade. To avoid this, make your call to the MWUtil module version-specific, for example: Set MCLUtil = CreateObject("MWComUtil.MWUtilx.x")where x.x is the specific version number. |
Start MCR with MCR options. Similar to mclInitializeApplication used in C/C++ shared libraries.
| Argument | Type | Description |
|---|---|---|
pApp | Object | A valid reference only when called from an Excel application Non Excel COM clients pass in Empty. |
Remarks. Call this function to pass in MCR options (nojvm, logfile, etc.). Call this function once per process (since the MCR can only be initialized once).
Example. This Visual Basic sample initializes the MWComUtil library with the current instance of Excel. A global variable of type Object named MCLUtil holds an instance of the MWUtil class, and another global variable of type Boolean named bModuleInitialized stores the status of the initialization process. The private subroutine InitModule() creates an instance of the MWComUtil class and calls the MWInitApplicationWithMCROptions method with an argument of Application and a string array that contains the options. Once this function succeeds, all subsequent calls exit without recreating the object. When this function successfully executes, the MCR starts up with no JVM and a logfile named logfile.txt.
Dim MCLUtil As Object
Dim bModuleInitialized As Boolean
Private Sub InitModule()
If Not bModuleInitialized Then
On Error GoTo Handle_Error
If MCLUtil Is Nothing Then
Set MCLUtil = CreateObject("MWComUtil.MWUtil")
End If
Dim mcrOptions(1 To 3) as String
mcrOptions(1) = "-nojvm"
mcrOptions(2) = "-logfile"
mcrOptions(3) = "logfile.txt"
Call MCLUtil.MWInitApplicationWithMCROptions(Application, mcrOptions)
bModuleInitialized = True
Exit Sub
Handle_Error:
bModuleInitialized = False
End If
End Sub
Note If you are not using Excel, pass in Empty instead of Application to MWInitApplicationWithMCROptions. |
Returns true if MCR is launched with JVM; otherwise returns false.
Boolean
Returns true if MCR is initialized; otherwise returns true
Boolean
Packs a variable length list of Variant arguments into a single Variant array. This function is typically used for creating a varargin cell from a list of separate inputs. Each input in the list is added to the array only if it is not empty or missing. (In Visual Basic, a missing parameter is denoted by a Variant type of vbError with a value of &H80020004.)
| Argument | Type | Description |
|---|---|---|
pVarArg | Variant | Receives the resulting array |
| [Var0], [Var1], ... | Variant | Optional list of Variants to pack into the array. From 0 to 32 arguments can be passed. |
Remarks. This function always frees the contents of pVarArg before processing the list.
Example. This example uses MWPack in a formula function to produce a varargin cell to pass as an input parameter to a method compiled from a MATLAB function with the signature
function y = mysum(varargin)
y = sum([varargin{:}]);
The function returns the sum of the elements in varargin. Assume that this function is a method of a class named myclass that is included in a component named mycomponent with a version of 1.0. The Visual Basic function allows up to 10 inputs, and returns the result y. If an error occurs, the function returns the error string. This function assumes that MWInitApplication has been previously called.
Function mysum(Optional V0 As Variant, _
Optional V1 As Variant, _
Optional V2 As Variant, _
Optional V3 As Variant, _
Optional V4 As Variant, _
Optional V5 As Variant, _
Optional V6 As Variant, _
Optional V7 As Variant, _
Optional V8 As Variant, _
Optional V9 As Variant) As Variant
Dim y As Variant
Dim varargin As Variant
Dim aClass As Object
Dim aUtil As Object
On Error Goto Handle_Error
Set aClass = CreateObject("mycomponent.myclass.1_0")
Set aUtil = CreateObject("MWComUtil.MWUtil")
Call aUtil.MWPack(varargin,V0,V1,V2,V3,V4,V5,V6,V7,V8,V9)
Call aClass.mysum(1, y, varargin)
mysum = y
Exit Function
Handle_Error:
mysum = Err.Description
End Function
Unpacks an array of Variants into individual Variant arguments. This function provides the reverse functionality of MWPack and is typically used to process a varargout cell into individual Variants.
| Argument | Type | Description |
|---|---|---|
VarArg | Variant | Input array of Variants to be processed |
nStartAt | Long | Optional starting index (zero-based) in the array to begin processing. Default = 0. |
bAutoResize | Boolean | Optional auto-resize flag. If this flag is True, any Excel range output arguments are resized to fit the dimensions of the Variant to be copied. The resizing process is applied relative to the upper left corner of the supplied range. Default = False. |
[pVar0],[pVar1], ... | Variant | Optional list of Variants to receive the array items contained in VarArg. From 0 to 32 arguments can be passed. |
Remarks. This function can process a Variant array in one single call or through multiple calls using the nStartAt parameter.
Example. This example uses MWUnpack to process a varargout cell into several Excel ranges, while auto-resizing each range. The varargout parameter is supplied from a method that has been compiled from the MATLAB function.
function varargout = randvectors
for i=1:nargout
varargout{i} = rand(i,1);
end
This function produces a sequence of nargout random column vectors, with the length of the ith vector equal to i. Assume that this function is included in a class named myclass that is included in a component named mycomponent with a version of 1.0. The Visual Basic subroutine takes no arguments and places the results into Excel columns starting at A1, B1, C1, and D1. If an error occurs, a message box displays the error text. This function assumes that MWInitApplication has been previously called.
Sub GenVectors()
Dim aClass As Object
Dim aUtil As Object
Dim v As Variant
Dim R1 As Range
Dim R2 As Range
Dim R3 As Range
Dim R4 As Range
On Error GoTo Handle_Error
Set aClass = CreateObject("mycomponent.myclass.1_0")
Set aUtil = CreateObject("MWComUtil.MWUtil")
Set R1 = Range("A1")
Set R2 = Range("B1")
Set R3 = Range("C1")
Set R4 = Range("D1")
Call aClass.randvectors(4, v)
Call aUtil.MWUnpack(v,0,True,R1,R2,R3,R4)
Exit Sub
Handle_Error:
MsgBox (Err.Description)
End Sub
Converts output dates from MATLAB to Variant dates.
| Argument | Type | Description |
|---|---|---|
pVar | Variant | Variant to be converted |
Remarks. MATLAB handles dates as double-precision floating-point numbers with 0.0 representing 0/0/00 00:00:00. By default, numeric dates that are output parameters from compiled MATLAB functions are passed as Doubles that need to be decremented by the COM date bias as well as coerced to COM dates. The MWDate2VariantDate method performs this transformation and additionally converts dates in string form to COM date types.
Example. This example uses MWDate2VariantDate to process numeric dates returned from a method compiled from the following MATLAB function.
function x = getdates(n, inc)
y = now;
for i=1:n
x(i,1) = y + (i-1)*inc;
end
This function produces an n-length column vector of numeric values representing dates starting from the current date and time with each element incremented by inc days. Assume that this function is included in a class named myclass that is included in a component named mycomponent with a version of 1.0. The subroutine takes an Excel range and a Double as inputs and places the generated dates into the supplied range. If an error occurs, a message box displays the error text. This function assumes that MWInitApplication has been previously called.
Sub GenDates(R As Range, inc As Double)
Dim aClass As Object
Dim aUtil As Object
On Error GoTo Handle_Error
Set aClass = CreateObject("mycomponent.myclass.1_0")
Set aUtil = CreateObject("MWComUtil.MWUtil")
Call aClass.getdates(1, R, R.Rows.Count, inc)
Call aUtil.MWDate2VariantDate(R)
Exit Sub
Handle_Error:
MsgBox (Err.Description)
End Sub
The MWFlags class contains a set of array formatting and data conversion flags (See Data Conversion Rules for more information on conversion between MATLAB and COM Automation types.) All MATLAB Builder NE COM components contain a reference to an MWFlags object that can modify data conversion rules at the object level. This class contains these properties and method:
The ArrayFormatFlags property controls array formatting (as a matrix or a cell array) and the application of these rules to nested arrays. The MWArrayFormatFlags class is a noncreatable class accessed through an MWFlags class instance. This class contains six properties:
Property InputArrayFormat As mwArrayFormat. This property of type mwArrayFormat controls the formatting of arrays passed as input parameters to .NET Builder class methods. The default value is mwArrayFormatMatrix. The behaviors indicated by this flag are listed in the next table.
Array Formatting Rules for Input Arrays
| Value | Behavior |
|---|---|
mwArrayFormatAsIs | Converts arrays according to the default conversion rules listed in Data Conversion Rules. |
mwArrayFormatCell | Coerces all arrays into cell arrays. Input scalar or numeric array arguments are converted to cell arrays with each cell containing a scalar value for the respective index. |
mwArrayFormatMatrix | Coerces all arrays into matrices. When an input argument is encountered that is an array of Variants (the default behavior is to convert it to a cell array), the data converter converts this array to a matrix if each Variant is single valued, and all elements are homogeneous and of a numeric type. If this conversion is not possible, creates a cell array. |
Property InputArrayIndFlag As Long. This property governs the level at which to apply the rule set by the InputArrayFormat property for nested arrays (an array of Variants is passed and each element of the array is an array itself). It is not necessary to modify this flag for varargin parameters. The data conversion code automatically increments the value of this flag by 1 for varargin cells, thus applying the InputArrayFormat flag to each cell of a varargin parameter. The default value is 0.
Property OutputArrayFormat As mwArrayFormat. This property of type mwArrayFormat controls the formatting of arrays passed as output parameters to MATLAB Builder NE class methods. The default value is mwArrayFormatAsIs. The behaviors indicated by this flag are listed in the next table.
Array Formatting Rules for Output Arrays
| Value | Behavior |
|---|---|
mwArrayFormatAsIs | Converts arrays according to the default conversion rules listed in Data Conversion Rules. |
mwArrayFormatMatrix | Coerces all arrays into matrices. When an output cell array argument is encountered (the default behavior converts it to an array of Variants), the data converter converts this array to a Variant that contains a simple numeric array if each cell is single valued, and all elements are homogeneous and of a numeric type. If this conversion is not possible, an array of Variants is created. |
mwArrayFormatCell | Coerces all output arrays into arrays of Variants. Output scalar or numeric array arguments are converted to arrays of Variants, each Variant containing a scalar value for the respective index. |
Property OutputArrayIndFlag As Long. This property is similar to the InputArrayIndFalg property, as it governs the level at which to apply the rule set by the OutputArrayFormat property for nested arrays. As with the input case, this flag is automatically incremented by 1 for a varargout parameter. The default value of this flag is 0.
Property AutoResizeOutput As Boolean. This flag 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, setting this flag to True instructs the data conversion code to resize each Excel range to fit the output array. Resizing is applied relative to the upper left corner of each supplied range. The default value for this flag is False.
Property TransposeOutput As Boolean. Setting this flag to True transposes the output arguments. This flag is useful when processing an output parameter from a method call on a COM component, where the MATLAB function returns outputs as row vectors, and you desire to place the data into columns. The default value for this flag is False.
The DataConversionFlags property controls how input variables are processed when type coercion is needed. The MWDataConversionFlags class is a noncreatable class accessed through an MWFlags class instance. This class contains these properties:
Property CoerceNumericToType As mwDataType. This property converts all numeric input arguments to one specific MATLAB type. This flag is useful is when variables maintained within the Visual Basic code are different types, e.g., Long, Integer, etc., and all variables passed to the compiled MATLAB code must be doubles. The default value for this property is mwTypeDefault, which uses the default rules in Data Conversion Rules.
PropertyDateBias As Long. This property sets the date bias for performing COM to MATLAB numeric date conversions. The default value of this property is 693960, representing 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 COM components created by MATLAB Builder NE. To process dates with such code, set this property to 0.
This example uses data conversion flags to reshape the output from a method compiled from a MATLAB function that produces an output vector of unknown length.
function p = myprimes(n)
if length(n)~=1, error('N must be a scalar'); end
if n < 2, p = zeros(1,0); return, end
p = 1:2:n;
q = length(p);
p(1) = 2;
for k = 3:2:sqrt(n)
if p((k+1)/2)
p(((k*k+1)/2):k:q) = 0;
end
end
p = (p(p>0));
This function produces a row vector of all the prime numbers between 0 and n. Assume that this function is included in a class named myclass that is included in a component named mycomponent with a version of 1.0. The subroutine takes an Excel range and a Double as inputs, and places the generated prime numbers into the supplied range. The MATLAB function produces a row vector, although you want the output in column format. It also produces an unknown number of outputs, and you do not want to truncate any output. To handle these issues, set the TransposeOutput flag and the AutoResizeOutput flag to True. In previous examples, the Visual Basic CreateObject function creates the necessary classes. This example uses an explicit type declaration for the aClass variable. As with previous examples, this function assumes that MWInitApplication has been previously called.
Sub GenPrimes(R As Range, n As Double)
Dim aClass As mycomponent.myclass
On Error GoTo Handle_Error
Set aClass = New mycomponent.myclass
aClass.MWFlags.ArrayFormatFlags.AutoResizeOutput = True
aClass.MWFlags.ArrayFormatFlags.TransposeOutput = True
Call aClass.myprimes(1, R, n)
Exit Sub
Handle_Error:
MsgBox (Err.Description)
End Sub
Property InputDateFormat As mwDateFormat. This property converts dates passed as input parameters to method calls on .NET Builder classes. The default value is mwDateFormatNumeric. The behaviors indicated by this flag are shown in the following table.
Conversion Rules for Input Dates
| Value | Behavior |
|---|---|
mwDateFormatNumeric | Convert dates to numeric values as indicated by the rule listed in Data Conversion Rules. |
mwDateFormatString | Convert input dates to strings. |
PropertyOutputAsDate As Boolean. This property processes an output argument as a date. By default, numeric dates that are output parameters from compiled MATLAB functions are passed as Doubles 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.
ReplaceMissing As mwReplaceMissingData. This property is an enumeration and can have two possible values: mwReplaceNaN and mwReplaceZero.
To treat empty cells referenced by input parameters as zeros, set the value to mwReplaceZero. To treat empty cells referenced by input parameters as NaNs (Not a Number), set the value to mwReplaceNaN.
By default, the value is mwReplaceZero.
Creates a copy of an MWFlags object.
| Argument | Type | Description |
|---|---|---|
ppFlags | MWFlags | Reference to an uninitialized MWFlags object that receives the copy |
Remarks. Clone allocates a new MWFlags 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.
The MWStruct class passes or receives a Struct type to or from a compiled class method. This class contains seven properties/methods:
This method allocates a structure array with a specified number and size of dimensions and a specified list of field names.
| Argument | Type | Description |
|---|---|---|
varDims | Variant | Optional array of dimensions |
varFieldNames | Variant | Optional array of field names |
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.
Sub foo ()
Dim x As MWStruct
Dim y As MWStruct
On Error Goto Handle_Error
'Create 1X1 struct arrays with no fields for x, and y
Set x = new MWStruct
Set y = new MWStruct
'Initialize x to be 2X2 with fields "red", "green",
' and "blue"
Call x.Initialize(Array(2,2), Array("red", "green", "blue"))
'Initialize y to be 1X5 with fields "name" and "age"
Call y.Initialize(5, Array("name", "age"))
'Re-dimension x to be 3X3 with the same field names
Call x.Initialize(Array(3,3))
'Add a new field to y
Call y.Initialize(, Array("name", "age", "salary"))
Exit Sub
Handle_Error:
MsgBox(Err.Description)
End Sub
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.
| 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:
Field name only
This format may be used only in the case of a 1-by-1 structure array and returns the named field's value. For example:
x("red") = 0.2
x("green") = 0.4
x("blue") = 0.6
In this example, the name of the Item property was neglected. This is possible since the Item property is the default property of the MWStruct class. In this case the two statements are equivalent:
x.Item("red") = 0.2
x("red") = 0.2
Single index and field name
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:
y = x(2, "red") y = x(2, 1, "red")
All indices and field name
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:
For I From 1 To 2
For J From 1 To 2
r(I, J) = x(I, J, "red")
g(I, J) = x(I, J, "green")
b(I, J) = x(I, J, "blue")
Next
Next
Array of indices and field name
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:
Dim Index(1 To 2) As Integer
For I From 1 To 2
Index(1) = I
For J From 1 To 2
Index(2) = J
r(I, J) = x(Index, "red")
g(I, J) = x(Index, "green")
b(I, J) = x(Index, "blue")
Next
Next
With these four formats, the Item property provides a very flexible indexing mechanism for structure arrays. Also note:
You can combine the last two indexing formats. Several index arguments supplied in either scalar or array format are concatenated to form one index set. The combining stops when the number of dimensions has been reached. For example:
Dim Index1(1 To 2) As Integer Dim Index2(1 To 2) As Integer Index1(1) = 1 Index1(2) = 1 Index2(1) = 3 Index2(2) = 2 x(Index1, Index2, 2, "red") = 0.5
The last statement resolves to
x(1, 1, 3, 2, 2, "red") = 0.5
The field name must be the last index in the list. The following statement produces an error:
y = x("blue", 1, 2)
Field names are case sensitive.
The read-only NumberOfFields property returns the number of fields in the structure array.
The read-only NumberOfDims property returns the number of dimensions in the struct array.
The read-only Dims property returns an array of length NumberOfDims that contains the size of each dimension of the struct array.
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 foo ()
Dim x As MWStruct
Dim Dims as Variant
Dim FieldNames As Variant
On Error Goto Handle_Error
'
'... Call a method that returns an MWStruct in x
'
Dims = x.Dims
FieldNames = x.FieldNames
For I From 1 To Dims(1)
For J From 1 To Dims(2)
For K From 1 To x.NumberOfFields
y = x(I,J,FieldNames(K))
' ... Do something with y
Next
Next
Next
Exit Sub
Handle_Error:
MsgBox(Err.Description)
End Sub
Creates a copy of an MWStruct object.
| Argument | Type | Description |
|---|---|---|
ppStruct | MWStruct | Reference to an uninitialized MWStruct object to receive the copy |
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.
Sub foo ()
Dim x1 As MWStruct
Dim x2 As MWStruct
Dim x3 As MWStruct
On Error Goto Handle_Error
Set x1 = new MWStruct
x1("name") = "John Smith"
x1("age") = 35
'Set reference of x1 to x2
Set x2 = x1
'Create new object for x3 and copy contents of x1 into it
Call x1.Clone(x3)
'x2's "age" field is
'also modified 'x3's "age" field unchanged
x1("age") = 50
.
.
.
Exit Sub
Handle_Error:
MsgBox(Err.Description)
End Sub
The MWField class holds a single field reference in an MWStruct object. This class is noncreatable and contains four properties/methods:
The name of the field (read only).
Stores the field's value (read/write). The Value property is the default property of the MWField class. The value of a field can be any type that is coercible to a Variant, as well as object types.
Stores a reference to an MWFlags object. This property sets or gets the array formatting and data conversion flags for a particular field. Each field in a structure has its own MWFlags property. This property overrides the value of any flags set on the object whose methods are called.
Creates a copy of an MWField object.
| Argument | Type | Description |
|---|---|---|
ppField | MWField | Reference to an uninitialized MWField object to receive the copy |
Remarks. Clone allocates a new MWField 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.
The MWComplex class passes or receives a complex numeric array into or from a compiled class method. This class contains four properties/methods:
Stores the real part of a complex array (read/write). The Real property is the default property of the MWComplex class. The value of this property can be any type coercible to a Variant, as well as object types, with the restriction that the underlying array must resolve to a numeric matrix (no cell data allowed). Valid Visual Basic numeric types for complex arrays include Byte, Integer, Long, Single, Double, Currency, and Variant/vbDecimal.
Stores the imaginary part of a complex array (read/write). The Imag property is optional and can be Empty for a pure real array. If the Imag property is not empty and the size and type of the underlying array do not match the size and type of the Real property's array, an error results when the object is used in a method call.
Example. The following Visual Basic code creates a complex array with the following entries:
x = [ 1+i 1+2i
2+i 2+2i ]
Sub foo()
Dim x As MWComplex
Dim rval(1 To 2, 1 To 2) As Double
Dim ival(1 To 2, 1 To 2) As Double
On Error Goto Handle_Error
For I = 1 To 2
For J = 1 To 2
rval(I,J) = I
ival(I,J) = J
Next
Next
Set x = new MWComplex
x.Real = rval
x.Imag = ival
.
.
.
Exit Sub
Handle_Error:
MsgBox(Err.Description)
End Sub
Stores a reference to an MWFlags object. This property sets or gets the array formatting and data conversion flags for a particular complex array. Each MWComplex object has its own MWFlags property. This property overrides the value of any flags set on the object whose methods are called.
Creates a copy of an MWComplex object.
| Argument | Type | Description |
|---|---|---|
ppComplex | MWComplex | Reference to an uninitialized MWComplex object to receive the copy |
Remarks. Clone allocates a new MWComplex 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.
The MWSparse class passes or receives a two-dimensional sparse numeric array into or from a compiled class method. This class has seven properties/methods:
Stores the row dimension for the array. The value of NumRows must be nonnegative. If the value is zero, the row index is taken from the maximum of the values in the RowIndex array.
Stores the column dimension for the array. The value of NumColumns must be nonnegative. If the value is zero, the row index is taken from the maximum of the values in the ColumnIndex array.
Stores the array of row indices of the nonzero elements of the array. The value of this property can be any type coercible to a Variant, as well as object types, with the restriction that the underlying array must resolve to or be coercible to a numeric matrix of type Long. If the value of NumRows is nonzero and any row index is greater than NumRows, a bad-index error occurs. An error also results if the number of elements in the RowIndex array does not match the number of elements in the Array property's underlying array.
Stores the array of column indices of the nonzero elements of the array. The value of this property can be any type coercible to a Variant, as well as object types, with the restriction that the underlying array must resolve to or be coercible to a numeric matrix of type Long. If the value of NumColumns is nonzero and any column index is greater than NumColumns, a bad-index error occurs. An error also results if the number of elements in the ColumnIndex array does not match the number of elements in the Array property's underlying array.
Stores the nonzero array values of the sparse array. The value of this property can be any type coercible to a Variant, as well as object types, with the restriction that the underlying array must resolve to or be coercible to a numeric matrix of type Double or Boolean.
Stores a reference to an MWFlags object. This property sets or gets the array formatting and data conversion flags for a particular sparse array. Each MWSparse object has its own MWFlags property. This property overrides the value of any flags set on the object whose methods are called.
Creates a copy of an MWSparse object.
| Argument | Type | Description |
|---|---|---|
ppSparse | MWSparse | Reference to an uninitialized MWSparse object to receive the copy |
Remarks. Clone allocates a new MWSparse 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 sample creates a 5-by-5 tridiagonal sparse array with the following entries:
X = [ 2 -1 0 0 0
-1 2 -1 0 0
0 -1 2 -1 0
0 0 -1 2 -1
0 0 0 -1 2 ]
Sub foo()
Dim x As MWSparse
Dim rows(1 To 13) As Long
Dim cols(1 To 13) As Long
Dim vals(1 To 13) As Double
Dim I As Long, K As Long
On Error GoTo Handle_Error
K = 1
For I = 1 To 4
rows(K) = I
cols(K) = I + 1
vals(K) = -1
K = K + 1
rows(K) = I
cols(K) = I
vals(K) = 2
K = K + 1
rows(K) = I + 1
cols(K) = I
vals(K) = -1
K = K + 1
Next
rows(K) = 5
cols(K) = 5
vals(K) = 2
Set x = New MWSparse
x.NumRows = 5
x.NumColumns = 5
x.RowIndex = rows
x.ColumnIndex = cols
x.Array = vals
.
.
.
Exit Sub
Handle_Error:
MsgBox (Err.Description)
End Sub
The MWArg class passes a generic argument into a compiled class method. This class passes an argument for which the data conversion flags are changed for that one argument. This class has three properties/methods:
The Value property stores the actual argument to pass. Any type that can be passed to a compiled method is valid for this property.
Stores a reference to an MWFlags object. This property sets or gets the array formatting and data conversion flags for a particular argument. Each MWArg object has its own MWFlags property. This property overrides the value of any flags set on the object whose methods are called.
Creates a copy of an MWArg object.
| Argument | Type | Description |
|---|---|---|
ppArg | MWArg | Reference to an uninitialized MWArg object to receive the copy |
Remarks. Clone allocates a new MWArg 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.
![]() | Referencing Utility Classes | Enumerations | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |