Overview of Data Conversion Classes |
The data conversion classes contained in the MWArray assembly bridge the gap between the .NET and MATLAB programming environments. These classes provide the following functionality:
A MATLAB object and native .NET API that provide data conversion capabilities between .NET and MATLAB data types
Transparent garbage collection of encapsulated MATLAB arrays stored in unmanaged memory
.NET indexers that provide a subset of MATLAB array indexing
Output formatting for MATLAB array types
MATLAB code error reporting with stack tracing
Ability to port a subset of MATLAB graphics functionality to Web based ASP.NET applications
Memory Management
As MWArray and its derived classes encapsulate an unmanaged MATLAB array that may be quite large, it is natural to assume that one should dispose of instances of these types as soon as possible using the deterministic Dispose() method. This is not necessary, however, as these classes implement an unmanaged memory tracking facility that automatically and transparently disposes of these arrays for you. This mechanism is closely integrated with the CLR garbage collector and, in most circumstances, makes deterministic object destruction unnecessary.
Generated Component Classes
The component classes, created by MATLAB Compiler SDK and included in the generated assembly files, allow developers to incorporate MATLAB functions into their own .NET applications. The MATLAB functions map to methods contained in one or more .NET classes.
NOTE: MATLAB Compiler SDK is an add-on product to MATLAB and the MATLAB Compiler that converts MATLAB code into .NET class methods.
For more information on these products, please visit www.mathworks.com.
MATLAB Runtime
The MATLAB Runtime is a collection of native libraries required to execute the MATLAB functions encapsulated by a MATLAB Compiler SDK class method; consequently, the MATLAB Runtime must be installed on any machine that runs a MATLAB Compiler SDK generated component. If you install MATLAB, MATLAB Compiler or MATLAB Compiler SDK, you have access to the MATLAB Runtime automatically. Otherwise, you must install the MATLAB Runtime by running the MCRInstaller executable. The operating system uses the system path variable, which is automatically set up by the installer, to locate the native MATLAB Runtime libraries.
The MWArray assembly is part of the MATLAB Runtime and can be referenced from the $MCR\toolbox\dotnetbuilder\bin\win64\v4.0 directory, where $MCR is the root of either the MATLAB Runtime or the MATLAB installation.
For more information on the MATLAB Runtime, please search the knowledge base at The MathWorks support web site.
MATLAB Data Types
In MATLAB, a matrix is the basic building block for all of the common data types. These include scalars (1-by-1 matrices), vectors (matrices with only one row or column) and multidimensional matrices with two or more dimensions. MATLAB has other ways of storing numeric and nonnumeric data, but most data is stored as a matrix. The supported MATLAB data types include logical, char, numeric, structure, and cell arrays. The numeric data type has subtypes representing signed and unsigned values and integer and floating-point data. The structure and cell types are MATLAB specific data types that act as container classes.
For more information on MATLAB data types, see The MathWorks support site and refer to the section "Programming Fundamentals".
MATLAB Compiler SDK and MATLAB Data Types
For .NET developers, MATLAB Compiler SDK provides access to the MATLAB data types through the class hierarchy provided by this assembly. All of the classes in the hierarchy derive from the abstract MWArray class. Each of the derived classes, with the exception of MWIndexArray (which is also abstract), maps to a MATLAB data type and includes the .NET classes MWNumericArray, MWLogicalArray, MWCharArray, MWStructArray, and MWCellArray. Each class has properties and methods that can be used to query various attributes of the class such as the number of dimensions, dimension size, element size, and field names.
Data Type Mapping Rules
The following table lists the data type mapping rules for converting .NET data types to MATLAB types using the MWArray class hierarchy:
.NET Type | MWArray Type | MATLAB Type |
---|---|---|
System.Double | MWNumericArray | double |
System.Number | MWNumericArray | double |
System.Float | MWNumericArray | single |
System.Byte | MWNumericArray | int8 |
System.Short | MWNumericArray | int16 |
System.Int32 | MWNumericArray | int32 |
System.Int64 | MWNumericArray | int64 |
System.Char | MWCharArray | char |
System.String | MWCharArray | char |
System.Boolean | MWLogicalArray | logical |
N/A | MWStructArray | structure |
N/A | MWCellArray | cell |
Note: .NET CLS compliant languages are not required to support the unsigned types uint16, uint32, and uint64 supported by MATLAB. Therefore, MATLAB arrays of these types should not be used in MATLAB code that is to be accessed via a .NET component. Finally, .NET does not have any built-in data type that corresponds to the MATLAB specific structure and cell types.
.NET - MATLAB Data Conversion
When you invoke a method on a MATLAB Compiler SDK generated class instance, the input parameter type must be one of the derived MWArray classes that wrap an internal MATLAB array type. You can either explicitly create the instance or pass in a native .NET type that maps to the required MATLAB type.
Similarly, all data returned by a MATLAB Compiler SDK generated method must belong to one of the derived MWArray types. Return data is not automatically converted to a .NET type. Use the ToArray() method to convert the return type to the appropriate native .NET type.