| Products & Services | Industries | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB |
| Contents | Index |
| Learn more about MATLAB |
| On this page… |
|---|
Creating .NET Arrays in MATLAB Example — Passing Data To a .NET Assembly Accessing .NET Array Elements in MATLAB |
.NET arrays are objects of the System.Array class. To create a .NET array, use the NET.createArray function. This function creates an empty array of a given type and dimension. If you have an existing MATLAB array, use the NET.convertArray function to convert it to a .NET array.
For information about the Array class, search the Microsoft Developer Network for System.Array.
Suppose you want to pass an array to a method. The following examples create .NET arrays.
To create a one-dimensional .NET array of strings, type:
strArray = NET.createArray('System.String', 3);
class(strArray)The object strArray is of class System.String[], an array of strings.
To create a two-dimensional array, type:
strArray = NET.createArray('System.String', [2,3]);
class(strArray)The object strArray is of class System.String[,], a two-dimensional array of strings.
Alternatively, type:
strArray2 = NET.createArray('System.String', 2,3);
class(strArray2)The object strArray2 is also of class System.String[,].
To create a .NET array from an existing MATLAB array, use the NET.convertArray command:
a =[1 2 3 4]; arr = NET.convertArray(a); class(arr)
The object arr is of class System.Double[], an .NET array of type Double.
To create a two-dimensional array from the MATLAB vector a, type:
arr2 = NET.convertArray(a, 'System.Double', [4,1]); class(arr2)
The object arr2 is a two-dimensional array of class System.Double.
You can also create a 1-by-4 array from a:
arr2 = NET.convertArray(a, 'System.Double', [1,4]);
To create a .NET array from a multidimensional MATLAB array, type:
a=[1 2 3 4;5 6 7 8]; arr =NET.convertArray(a); class(arr)
The object arr is of class System.Double[,].
You can create arrays of other .NET types. For example, the following commands:
a =[1 2 3 4]; arr =NET.convertArray(a, 'System.Int32', 4); class(arr)
create an object arr of class System.Int32[].
Suppose you want to call a class method in an assembly. The class documentation defines the following data.
| Variable Name | Description | Data Type |
|---|---|---|
| courseName | Description of a course | System.String |
| courseTime | Date and time of the course | System.DateTime |
| courseData | Array of data values | System.Double |
To create the name of the course in MATLAB, type:
import System.*
courseName = String('Getting Started with .NET');The course occurs on January 15, 2009, at 9:30 AM. Type:
courseTime = DateTime(2009,1,15,9,30,0);
Create a 2-by-4 array of integers:
myData = randi(100,2,4);
To convert this data to a .NET array, type:
courseData = NET.convertArray(myData);
Type:
whos course*
MATLAB displays:
Name Size Bytes Class courseData 1x1 60 System.Double[,] courseName 1x1 60 System.String courseTime 1x1 64 System.DateTime
Now you can pass these variables to methods in the class.
When a method call returns .NET arrays, MATLAB leaves them in their original form. They remain as .NET arrays so you can continue to use them to interact with other .NET methods. If you want to use a .NET array in a MATLAB command, convert it from the .NET type to a MATLAB type.
Suppose a method returns netArray, a .NET array of class System.Double[]. To work with this example, create and initialize the array by typing:
netArray = NET.createArray('System.Double', 3);
netArray(1) = 100;
netArray(2) = 101;
netArray(3) = 102;
To verify this is a .NET array, type:
class(netArray)
MATLAB displays:
ans = System.Double[]
To use netArray in a MATLAB command, convert it to a MATLAB array. For example, to use the sum command, call the double function:
sum(double(netArray))
MATLAB displays:
ans = 303
You convert a multidimensional array the same way you convert a one-dimensional array. For this example, first create and initialize a 3-by-2 .NET array, which would be performed in a method you invoke:
netArray2 = NET.createArray('System.Double', [3, 2]);
for i=1:netArray2.GetLength(0)
for j=1:netArray2.GetLength(1)
netArray2(i,j) = i+j;
end
endVerify this is a multidimensional .NET array:
class(netArray2)
MATLAB displays:
ans = System.Double[,]
To convert netArray2 to a MATLAB array, type:
mlArray = double(netArray2);
To get information about mlArray, type:
type = class(mlArray) [m,n] = size(mlArray) mlArray
MATLAB displays:
type =
double
m =
3
n =
2
mlArray =
2 3
3 4
4 5To sum the columns of mlArray, type:
M = sum(mlArray)
MATLAB displays:
M =
9 12Array indexing in the C# programming language is different from MATLAB array indexing. C# array indices are zero-based, MATLAB array indices are one-based. In C#, you access the elements of array y of length N using y[0] through y[N-1]. When working with this array in MATLAB, you access these same elements using the MATLAB software indexing style of y(1) through y(N). Thus, if you have a C# array of ten elements, the seventh element is obtained using y(7), and not y[6] as you use when writing a C# language program.
A .NET array is an object. The size function regards it as a 1-by-1 array. For more information , see the System.Array class.
Create and initialize a .NET string array:
strArray = NET.createArray('System.String', 3);
strArray(1) = 'one';
strArray(2) = 'two';
strArray(3) = 'three';To display the first element of the array, type:
strArray(1)
MATLAB displays:
ans = one
To work with multidimensional arrays, create dblArray as follows, setting values for two elements of the array:
dblArray = NET.createArray('System.Double', [2,3]);
dblArray(1,1) = 1;
dblArray(1,2) = 2;
To display the value in row 1, column 2, type:
dblArray(1,2)
MATLAB displays:
ans =
2
An alternative method to access elements of a .NET array, is to use the Get and Set methods. These are instance methods of the object. You can also use the System.Array class methods GetValue and SetValue.
The order of input arguments for the Set and SetValue methods are different. For example, create strArr, an object of class System.String[]:
strArr = NET.createArray('System.String', 2);
methodsview(strArr)
View the function signatures:
Set(System.String[] this, int32 scalar , System.String ) SetValue(System.String[] this, System.Object value, int32 scalar index)
The first argument for Set is the index. For example:
strArr.Set(0,'hello ');
The first argument for SetValue is the value. For example:
strArr.SetValue('world',1);
Type:
String.Concat(strArr)
MATLAB displays:
ans = hello world
Suppose you have a class in an assembly that contains the following information.
| Property Name | Description | Data Type |
|---|---|---|
| netData | Array of data values | System.Double |
| netName | Short description of the data | System.String |
| netStart | Starting time of data collection | System.DateTime |
| netEnd | Ending time of data collection | System.DateTime |
Use the following MATLAB commands to initialize these variables with sample data. This functionality is actually performed in the class method.
Set the starting time to 6:00 a.m. and the ending time to 10:00 a.m.
import System.*
netName = String('.NET Data');
netStart = DateTime(2008,11,1,6,0,0);
netEnd = DateTime(2008,11,1,10,0,0);There is one data point for each hour. Calculate the number of data points:
netNData = netEnd.Hour - netStart.Hour + 1;
Create a data array and fill with random values:
netData = NET.createArray('System.Double', netNData);
for index = 1:netNData
netData.Set(index-1,randi(1000,1));
endAfter calling this method, the MATLAB object contains the data netName, netStart, netEnd, and netData.
To use this data in MATLAB commands, convert the .NET data System.String and System.Double into MATLAB variable data, as described in Handling Data Returned from a .NET Object.
Use the double function to convert the data, netData:
mlData = double(netData);
To view this data, type:
bar(mlData)
Use the char function to convert the name netName to a string:
char(netName)
MATLAB displays:
ans = .NET Data
You can create a MATLAB timeseries object with this information. The data is a 1-by-5 array. Transpose the array to match the Time vector:
mlData';
To create a timeseries object tsNET, type:
tsNET = timeseries(mlData','Name',char(netName));
Set the following time parameters:
tsNET.TimeInfo.Units = 'hours'; tsNET.TimeInfo.Start = double(netStart.Hour)
The tsNET object now contains data like the following:
Time Series Object: .NET Data
Time vector characteristics
Length 5
Start time 6 hours
End time 10 hours
Data characteristics
Interpolation method linear
Size [5 1]
Data type double
Time Data Quality
-------------------------------------------
6 183
7 240
8 887
9 29
10 490To view the data, type:
tsNET.plot
When you are finished, type:
close
You cannot create a ragged array or specify a lower bound. You cannot concatenate .NET objects into an array. You cannot use the end function as the last index in a .NET array.
![]() | Handling .NET Data in MATLAB Software | Using Generic Classes | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |