Documentation Center

  • Trial Software
  • Product Updates

Passing Data to .NET Objects

When you make a call in MATLAB® to a .NET method or function, MATLAB automatically converts arguments into .NET types. MATLAB performs this conversion on each passed argument, except for arguments that are already .NET objects.

The following topics provide information about passing specific data to .NET:

Pass Primitive .NET Types

The following table shows the MATLAB base types for passed arguments and the corresponding .NET types defined for input arguments. Each row shows a MATLAB type followed by the possible .NET argument matches, from left to right in order of closeness of the match.

MATLAB Primitive Type Conversion Table

MATLAB
Type
Closest Type <————— Other Matching .NET Types —————> Least Close Type
Preface Each .NET Type with System.
logicalBooleanByteSByteInt16UInt16Int32UInt32Int64UInt64SingleDoubleObject
doubleDoubleSingleDecimalInt64UInt64Int32UInt32Int16UInt16SByteByteObject
singleSingleDoubleDecimalObject
int8SByteInt16Int32Int64SingleDoubleObject
uint8ByteUInt16UInt32UInt64SingleDoubleObject
int16Int16Int32Int64SingleDoubleObject
uint16UInt16UInt32UInt64SingleDoubleObject
int32Int32Int64SingleDoubleObject
uint32UInt32UInt64SingleDoubleObject
int64Int64DoubleObject
uint64UInt64DoubleObject
charCharStringObject

The following primitive .NET argument types do not have direct MATLAB equivalent types. MATLAB passes these types as is:

  • System.IntPtr

  • System.UIntPtr

  • System.Decimal

  • enumerated types

Pass Cell Arrays

You can pass a cell array to a .NET property or method expecting an array of System.Object or System.String arguments, as shown in the following table.

MATLAB Cell Array Conversion Table

MATLAB TypeClosest Type <——— Other Matching .NET Types ———> Least Close Type

Cell array of strings

System.String[ ]System.Object[ ]System.Object

Cell array (not all strings)

System.Object[ ]System.Object

Elements of a cell can be any of the following supported types:

Pass Nonprimitive .NET Objects

When calling a method that has an argument of a particular .NET class, you must pass an object that is an instance of that class or its derived classes. You can create such an object using the class constructor, or use an object returned by a member of the class. When a class member returns a .NET object, MATLAB leaves it as a .NET object so you can continue to use it to interact with other class members.

Pass MATLAB Strings

MATLAB automatically converts a string or char array to a .NET System.String object. To pass an array of strings, create a cell array.

Pass System.Nullable Type

You can pass any of the following to a .NET method with System.Nullable<ValueType> input arguments:

  • Variable of the underlying <ValueType>

  • null value, []

  • System.Nullable<ValueType> object

When you pass a MATLAB variable of type ValueType, MATLAB reads the signature and automatically converts your variable to a System.Nullable<ValueType> object. For a complete list of possible ValueType values accepted for System.Nullable<ValueType>, refer to the MATLAB Primitive Type Conversion Table.

For examples, see Pass System.Nullable Arguments.

Pass NULL Values

MATLAB uses empty double ([]) values for reference type arguments.

Unsupported MATLAB Types

You cannot pass the following MATLAB types to .NET methods:

  • Structure arrays

  • Sparse arrays

  • Complex numbers

Choosing Method Signatures

MATLAB chooses the correct .NET method signature (including constructor, static and nonstatic methods) based on the following criteria.

When your MATLAB function calls a .NET method, MATLAB:

  1. Checks to make sure that the object (or class, for a static method) has a method by that name.

  2. Determines whether the invocation passes the same number of arguments of at least one method with that name.

  3. Makes sure that each passed argument can be converted to the type defined for the method.

If all the preceding conditions are satisfied, MATLAB calls the method.

In a call to an overloaded method, if there is more than one candidate, MATLAB selects the one with arguments that best fit the calling arguments, based on the MATLAB Primitive Type Conversion Table. First, MATLAB rejects all methods that have any argument types that are incompatible with the passed arguments. Among the remaining methods, MATLAB selects the one with the highest fitness value, which is the sum of the fitness values of all its arguments. The fitness value for each argument is how close the MATLAB type is to the .NET type. If two methods have the same fitness, MATLAB chooses the first one defined in the class.

For class types, MATLAB chooses the method signature based on the distance of the incoming class type to the expected .NET class type. The closer the incoming type is to the expected type, the better the match.

The rules for overloaded methods with optional arguments are described in Determining Which Overloaded Method Is Invoked.

Example — Choosing a Method Signature

Open a methodsview window for the System.String class and look at the entries for the Concat method:

import System.*
methodsview('System.String')

The Concat method takes one or more arguments. If the arguments are of type System.String, the method concatenates the values. For example, create two strings:

str1 = String('hello');
str2 = String('world');

When you type:

String.Concat(str1,str2)

MATLAB verifies the method Concat exists and looks for a signature with two input arguments. The following table shows the two signatures.

QualifiersReturn TypeNameArguments
StaticSystem.String RetValConcat(System.Object arg0,
System.Object arg1)
StaticSystem.String RetValConcat(System.String str0,
System.String str1)

Since str1 and str2 are of class System.String, MATLAB chooses the second signature and displays:

ans = 
helloworld

If the arguments are of type System.Object, the method displays the string representations of the values. For example, create two System.DateTime objects:

objDate = DateTime.Today;
myDate = System.DateTime(objDate.Year,3,1,11,32,5);

When you type:

String.Concat(objDate,myDate)

MATLAB chooses the following signature, since System.DateTime objects are derived from the System.Object class.

QualifiersReturn TypeNameArguments
StaticSystem.String RetValConcat(System.Object arg0,
System.Object arg1)

This Concat method first applies the ToString method to the objects, then concatenates the strings. MATLAB displays information like:

ans = 
12/23/2008 12:00:00 AM3/1/2008 11:32:05 AM

Pass Arrays

For information about passing MATLAB arrays to .NET methods, see Using Arrays with .NET Applications and Pass MATLAB Arrays as Jagged Arrays.

How Array Dimensions Affect Conversion

The dimension of a .NET array is the number of subscripts required to access an element of the array. To get the number of dimensions, use the Rank property of the .NET System.Array type. The dimensionality of a MATLAB array is the number of non-singleton dimensions in the array.

MATLAB matches the array dimensionality with the .NET method signature, as long as the dimensionality of the MATLAB array is lower than or equal to the expected dimensionality. For example, you can pass a scalar input to a method that expects a 2-D array.

For a MATLAB array with number of dimensions, N, if the .NET array has fewer than N dimensions, the MATLAB conversion drops singleton dimensions, starting with the first one, until the number of remaining dimensions matches the number of dimensions in the .NET array.

Converting a MATLAB Array to System.Object

You can pass a MATLAB array to a method that expects a System.Object.

Pass MATLAB Arrays as Jagged Arrays

A MATLAB array is a rectangular array. The .NET Framework supports a jagged array, which is an array of arrays. This means the elements of a jagged array can be of different dimensions and sizes.

Although .NET languages support jagged arrays, the term jagged is not a language keyword. C# function signatures use multiple pairs of square brackets ([][]) to represent a jagged array. In addition, a jagged array can be nested ([][][]), multidimensional ([,]), or nested with multidimensional elements (for example, [,,][,][]).

MATLAB automatically converts MATLAB arrays of numeric types to the corresponding jagged array type. If the input argument is a non-numeric type or multidimensional, use the NET.createArray function to create an array to pass as a jagged array. For examples using NET.createArray, see Pass Jagged Arrays.

Was this topic helpful?