This is machine translation

Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Translate This Page

MathWorks Machine Translation

The automated translation of this page is provided by a general purpose third party translator tool.

MathWorks does not warrant, and disclaims all liability for, the accuracy, suitability, or fitness for purpose of the translation.

Data Conversion with C# and MATLAB Types

When the .NET client invokes a MATLAB® function through
a request and receives a result in the response, data conversion takes
place between MATLAB types and C# types.

Working with MATLAB Data Types

There are many data types, or classes, that you can work with
in MATLAB. Each of these classes is in the form of a matrix or
array. You can build matrices and arrays of floating-point and integer
data, characters and strings, and logical true and false states. Structures
and cell arrays provide a way to store dissimilar types of data in
the same array.

Scalar Numeric Type Coercion

Scalar numeric MATLAB types can be assigned to multiple
.NET numeric types as long as there is no loss of data or precision.

The main exception to this rule is that MATLAB double scalar
data can be mapped into any .NET numeric type. Because double is
the default numeric type in MATLAB, this exception provides more
flexibility to the users of MATLAB
Production Server .NET client
API.

Dimension Coercion

In MATLAB, dimensionality is an attribute of the fundamental
types and does not add to the number of types as it does in .NET.

In C#, double, double[] and double[,] are
three different data types. In MATLAB, there is only a double data
type and possibly a scalar instance, a vector instance, or a multi-dimensional
instance.

C# Signature

Value
Returned from MATLAB

double[,,] foo()

ones(1,2,3)

How you define your MATLAB function and corresponding C#
method signature determines if your output data will be coerced, using
padding or truncation.

This coercion is performed automatically for you. This section
describes the rules followed for padding and truncation.

Note

Multidimensional arrays of C# types are supported. Jagged arrays
are not supported.

Padding

When a C# method's return type has a greater number of dimensions
than MATLAB’s, MATLAB's dimensions are padded with
ones (1s) to match the required number of output
dimensions in C#.

The following tables provide examples of how padding is performed
for you:

How Your C# Method Return Type is Padded

MATLAB Function

C# Method Signature

When Dimensions
in MATLAB are:

And Dimensions in C#
are:

function a = fooa = ones(2,3);

double[,,,] foo()

size(a) is [2,3]

Array will be returned as size 2,3,1,1

Truncation

When a C# method's return type has fewer dimensions than MATLAB’s, MATLAB’s
dimensions are truncated to match the required number of output dimensions
in C#. This is only possible when extra dimensions for MATLAB array
have values of ones (1s) only.

To compute appropriate number of dimensions in C#, excess ones
are truncated, in this order:

From the end of the array

From the array’s beginning

From the middle of the array (scanning front-to-back).

The following tables provide examples of how truncation is performed
for you:

How MATLAB Truncates Your C# Method Return Type

MATLAB Function

C# Method Signature

When Dimensions
in MATLAB are:

And Dimensions in C#
are:

function a = fooa = ones(1,2,1,1,3,1);

double[,] foo()

size(a) is [1,2,1,1,3,1]

Array will be returned as size 2,3

Following are some examples of dimension shortening using the double
numeric type:

Truncating Dimensions in MATLAB and C# Data Conversion

MATLAB Array Dimensions

Declared Output C# Type

Output
C# Dimensions

1 x 1

double

0 (scalar)

2 x 1

double[]

2

1 x 2

double[]

2

2 x 3 x 1

double[,]

2 x 3

1 x 3 x 4

double[,]

3 x 4

1 x 3 x 4 x 1 x 1

double[,,]

1 x 3 x 4

1 x 3 x 1 x 1 x 2 x 1 x 4 x 1

double[,,,]

3 x 2 x 1 x 4

Empty (Zero) Dimensions

When a null is passed from C# to MATLAB,
it will always be marshaled into [] in MATLAB as
a zero by zero (0 x 0)
double. This is independent of the declared input type used in C#.
For example, all the following methods can accept null as
an input value: