Products & Services Solutions Academia Support User Community Company

Learn more about MATLAB   

Using Arrays with .NET Applications

.NET Arrays

.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.

Creating .NET Arrays in MATLAB

Suppose you want to pass an array to a method. The following examples create .NET arrays.

Examples — Creating .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[,].

Examples — Creating .NET Arrays From Existing MATLAB Arrays

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[].

Example — Passing Data To a .NET Assembly

Suppose you want to call a class method in an assembly. The class documentation defines the following data.

Variable NameDescriptionData 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.

Using .NET Arrays in MATLAB

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.

Example — Creating a MATLAB Array from a .NET Array

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

Example — Converting a Multidimensional .NET Array

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
end

Verify 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     5

To sum the columns of mlArray, type:

M = sum(mlArray)

MATLAB displays:

M =
     9    12

Accessing .NET Array Elements in MATLAB

Array 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.

Example — Accessing Elements of a .NET Array

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

Example — Setting .NET Array Elements with Set and SetValue

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

Example — Reading Data From a .NET Assembly

Suppose you have a class in an assembly that contains the following information.

Property NameDescriptionData 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

In the Class Method

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));
end

In MATLAB

After 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                490

To view the data, type:

tsNET.plot

When you are finished, type:

close

Limitations to Support of .NET Arrays

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.

  


Recommended Products

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