Main Content

MathWorks.MATLAB.Types.MATLABArray

.NET class to represent pointers to MATLAB arrays

Since R2022b

Description

Use MathWorks.MATLAB.Types.MATLABArray to represent a MATLAB® type in a .NET application. Using this type reduces the number of array allocations and data copies. For an example, see Reduce Array Allocation Using MATLABArray.

There is no direct mapping from a MATLAB type to a single .NET type. MATLAB returns arguments as dynamic .NET types. Convert the MATLABArray object to a specific .NET type for your application. For an example, see Pass Variables from MATLAB to .NET.

Class Details

Namespace:

MathWorks.MATLAB.Types
Superclass:System.Dynamic.DynamicObject
Superclass:System.IConvertible

Examples

expand all

This example describes how the .NET application allocates memory for variables of type double compared with variables of type MATLABArray.

In this C# code, the application creates a MATLAB array for each .NET scalar value (0 and 1) to pass to the linspace function. It copies the value returned from linspace to a new .NET array (x1). Next, it copies the .NET array x1 to a new MATLAB array to pass to the sqrt function and copies the value returned from sqrt to a new .NET array (y1).

using (dynamic matlab = MATLABEngine.StartMATLAB())
{
	double[] x1 = matlab.linspace(0, 1);
	double[] y1 = matlab.sqrt(x1);
}

In this code, the application creates a MATLAB array for each .NET scalar value (0 and 1) to pass to the linspace function. However, by using MATLABArray objects, the application does not create extra .NET and MATLAB arrays for variables x2 and y2.

using (dynamic matlab = MATLABEngine.StartMATLAB())
{
	MATLABArray x2 = matlab.linspace(0, 1);
	MATLABArray y2 = matlab.sqrt(x2);
}

Version History

Introduced in R2022b