Main Content

Data Marshaling with MWArray API

To support data conversion between managed .NET types and MATLAB® types, MATLAB Compiler SDK™ provides a set of data conversion classes derived from the abstract class MWArray. These classes allow you to pass most native .NET value types as parameters directly without using explicit data conversion. You reference the MWArray assembly in your managed application to convert native arrays to MATLAB arrays and vice versa. This process is called data marshaling.

When you invoke a method on a component, the input and output parameters are a derived type of MWArray. To pass parameters, you can either instantiate one of the MWArray subclasses explicitly, or, in many cases, pass the parameters as a managed data type and rely on the implicit data conversion feature of MATLAB Compiler SDK.

For examples that demonstrate guidelines for manual data conversion between various native data types and types compatible with MATLAB, see Convert Data Between .NET and MATLAB.

MWArray Data Conversion Classes

The MWArray data conversion classes are built as a class hierarchy that represents the major MATLAB array types.

  • MWArray

  • MWIndexArray

  • MWCellArray

  • MWCharacterArray

  • MWLogicalArray

  • MWNumericArray

  • MWStructArray

The root of the hierarchy is the MWArray abstract class. MWIndexArray is also an abstract class. The other subclasses represent the major MATLAB array types: MWNumericArray, MWLogicalArray, MWCharArray, MWCellArray, and MWStructArray.

MWArray and its derived classes provide the following functionality:

  • Constructors and destructors to instantiate and dispose of MATLAB arrays

  • Properties to get and set the underlying array data

  • Indexers to support a subset of MATLAB array indexing

  • Implicit and explicit data conversion operators

  • General methods

For information about these data conversion classes, see the MWArray Class Library Reference, which is also available in the matlabroot\help\dotnetbuilder\MWArrayAPI folder, where matlabroot represents your MATLAB installation folder.

Pass Data from .NET Code to MATLAB

In most instances, if you use a native .NET primitive or array as an input parameter in a C# program, MATLAB Compiler SDK automatically and transparently converts it to an instance of the appropriate MWArray class before passing it to the generated method. MATLAB Compiler SDK converts most CLS-compliant strings, numeric types, or multidimensional arrays of these types to an appropriate MWArray type. For a list of the unsupported types, see Unsupported MATLAB Array Types. This conversion is transparent in C# applications, but might require an explicit casting operator in other languages, for example, op_implicit in Visual Basic®.

For example, consider the .NET statement:

result = theFourier.plotfft(3, data, interval);

In this statement, the argument interval is of the .NET native type System.Double. MATLAB Compiler SDK casts this argument to a MATLAB 1-by-1 double MWNumericArray type, which is a wrapper class containing a MATLAB double array.

Pass Data from MATLAB to .NET Code

All data returned from a MATLAB function to a .NET method is represented as an instance of the appropriate MWArray subclass. For example, a MATLAB cell array is returned as an MWCellArray object.

Returned data is not automatically converted to a native array. If you need to get the corresponding native array type, call the ToArray method, which converts a MATLAB array to the appropriate native data type, with some exceptions. Cell arrays, structure arrays, and arrays of complex numbers are not available as native .NET types. To represent these data types, you must create an instance of MWCellArray, MWStructArray, or MWNumericArray, respectively.

For a list of the .NET native data types and their equivalents in MATLAB, see Rules for Data Conversion Between .NET and MATLAB.

Related Topics