Handle Data Returned from .NET Objects

.NET Type to MATLAB Type Mapping

The following table shows how MATLAB® converts data from a .NET object into MATLAB types. These values are displayed in a method signature.

C# .NET TypeMATLAB Type
System.Int16int16 scalar
System.UInt16uint16 scalar
System.Int32int32 scalar
System.UInt32uint32 scalar
System.Int64int64 scalar
System.UInt64uint64 scalar
System.Singlesingle scalar
System.Doubledouble scalar
System.Booleanlogical scalar
System.Byteuint8 scalar
System.Enumenum
System.Charchar
System.DecimalSystem.Decimal
System.ObjectSystem.Object
System.IntPtrSystem.IntPtr
System.UIntPtrSystem.UIntPtr
System.StringSystem.String
System.Nullable<ValueType>System.Nullable<ValueType>
System.Array

See Use Arrays with .NET Applications

System.__ComObject

See How MATLAB Handles System.__ComObject

class nameclass name
struct namestruct name

How MATLAB Handles System.String

To convert a System.String object to a MATLAB string, use the string function. To convert a System.String object to a MATLAB character array, use the char function. For example:

str = System.String('create a System.String');
mlstr = string(str)
mlchar = char(str)
mlstr = 

    "create a System.String"


mlchar =

    'create a System.String'           

MATLAB displays the string value of System.String objects, instead of the standard object display. For example, type:

a = System.String('test')
b = System.String.Concat(a,' hello',' world')
a = 
test
b = 
test hello world

The System.String class illustrates how MATLAB handles fields and properties, as described in Call .NET Properties That Take an Argument. To see reference information about the class, search for the term System.String in the .NET Framework Class Library, as described in To Learn More About the .NET Framework.

The string function converts String.String arrays (String.String[], String.String[,], etcetera) to MATLAB string arrays with the same dimensions and sizes. Conversion of jagged arrays, for example String.String[][], is not supported.

How MATLAB Handles System.__ComObject

The System.__ComObject type represents a Microsoft® COM object. It is a non-visible, public class in the mscorlib assembly with no public methods. Under certain circumstances, a .NET object returns an instance of System.__ComObject. MATLAB handles the System.__ComObject based on the return types defined in the metadata.

MATLAB Converts Object

If the return type of a method or property is strongly typed, and the result of the invocation is System.__ComObject, MATLAB automatically converts the returned object to the appropriate type.

For example, suppose that your assembly defines a type, TestType, and provides a method, GetTestType, with the following signature.

Return TypeNameArguments
NetDocTest.TestType RetValGetTestType(NetDocTest.MyClass this)

The return type of GetTestType is strongly typed and the .NET Framework returns an object of type System.__ComObject. MATLAB automatically converts the object to the appropriate type, NetDocTest.TestType, shown in the following pseudo-code:

cls = NetDocTest.MyClass;
var = GetTestType(cls)
var = 

  TestType handle with no properties.

Casting Object to Appropriate Type

If the return type of a method or property is System.Object, and the result of the invocation is System.__ComObject, MATLAB returns System.__ComObject. To use the returned object, cast it to a valid class or interface type. Use your product documentation to identify the valid types for this object.

To call a member of the new type, cast the object using the MATLAB conversion syntax:

objConverted = namespace.classname(obj)

where obj is a System.__ComObject type.

For example, an item in a Microsoft Excel® sheet collection can be a chart or a worksheet. The following command converts the System.__ComObject variable mySheet to a Chart or a Worksheet object newSheet:

newSheet = Microsoft.Office.Interop.Excel.interfacename(mySheet);

where interfacename is Chart or Worksheet. For an example, see Work with Microsoft Excel Spreadsheets Using .NET.

Pass a COM Object Between Processes

If you pass a COM object to or from a function, lock the object so that MATLAB does not automatically release it when the object goes out of scope. To lock the object, call the NET.disableAutoRelease function. Then unlock the object, using the NET.enableAutoRelease function, after you are through using it.

How MATLAB Handles System.Nullable

If .NET returns a System.Nullable type, MATLAB returns the corresponding System.Nullable type.

A System.Nullable type lets you assign null values to types, such as numeric types, that do not support null value. To use a System.Nullable object in MATLAB, first decide how to handle null values.

  • If you want to process null values differently from <ValueType> values, use the HasValue property.

  • If you want every value to be of the underlying <ValueType>, use the GetValueOrDefault method. This method assigns a default value of type <ValueType> to null values.

Use a variable of the object's underlying type where appropriate in any MATLAB expression. For examples, see Pass System.Nullable Arguments.

How MATLAB Handles dynamic Type

MATLAB handles dynamic types as System.Object. For example, the following C# method exampleMethod has a dynamic input argument d and returns a dynamic output value:

public dynamic exampleMethod(dynamic d)

The following table shows the corresponding MATLAB function signature.

Return TypeNameArguments
System.Object RetValexampleMethod(namespace.classname this,
System.Object d)

How MATLAB Handles Jagged Arrays

You must convert a .NET jagged array before using it in a MATLAB command. To convert:

  • If the shape of the array is rectangular, use the corresponding MATLAB numeric function.

  • If the array is not rectangular, use the cell function.

If the jagged array is multidimensional, you must individually convert the arrays in each dimension.