Use Cell Arrays of .NET Data
Cell arrays in MATLAB® are useful for passing object arrays or character arrays to .NET functions as well as for handling jagged array data returned from .NET functions. For more information, see Use MATLAB Arrays with .NET Functions.
Pass Cell Arrays to .NET
When you call a .NET function from MATLAB, you can pass MATLAB cell arrays as arguments.
If you pass a cell array of string scalars or character arrays, MATLAB automatically converts the data to a .NET array of strings or objects.
If you pass a cell array with elements of other types, MATLAB automatically converts the data to a .NET array of objects.
For more information on the automatic conversion, see Pass MATLAB Cell Arrays to .NET.
However, if you want to pass cell arrays to a .NET function that expects
nonrectangular (jagged) arrays, you must explicitly convert the cell arrays to .NET
arrays by using the NET.createArray function in
MATLAB. For examples, see Pass Jagged Arrays.
Convert Returned .NET Data to MATLAB Cell Arrays
When you call a .NET function from MATLAB, the function can return .NET data that you can convert to a cell array. These examples show how to convert:
.NET arrays that contain strings or objects
Multidimensional .NET arrays with known data types
Nested .NET arrays
Convert System.String[] to Cell Array
To convert .NET System.String and
System.Object arrays to MATLAB cell arrays, use the cell function. MATLAB automatically converts the data so that elements of the resulting
cell array are of the MATLAB type closest to the .NET type. For data type mapping information,
see Handle Data Returned from .NET Objects.
For example, the .NET System.IO.Directory class has a
method named GetDirectories that returns a .NET
System.String array. Convert the returned .NET array to a
cell array of folder names in your C:\ folder.
myList = cell(System.IO.Directory.GetDirectories("C:\"));Check the data type of converted data.
class(myList)
ans =
'cell'Convert System.Object[,] to Cell Array of Specified Type
If you know the data types of the elements in a
System.Object[,] array, you can convert the .NET array to
a cell array with elements of a specified data type by using the
cell function with the ConvertTypes
name-value argument.
A = cell(obj,'ConvertTypes',type)In that cell function syntax:
objis a .NETSystem.Object[,]array.typecan be one of these values:{'System.DateTime'}— ConvertSystem.DateTimeelements to MATLABdatetimeelements.{'System.String'}— ConvertSystem.Stringelements to MATLAB character arrays.{'all'}— Convert all supported .NET types to equivalent MATLAB types.
Ais a cell array that is the same size as theobjarray.
For example, to convert System.DateTime data in a
System.Object[,] array named netDates
to a cell array of MATLAB datetimes:
mlDates = cell(netDates,'ConvertTypes',{'System.DateTime'})
Convert Nested System.Object[][] Array to Cell Array
MATLAB does not automatically convert a nested
System.Object array (that is, a
System.Object array contained within another
System.Object array). To convert these .NET arrays to
cell arrays, call the cell function on each
System.Object array.
For example, the sample NetDocCell.cs file defines the
MyGraph class and its getNewData
method. (To see the C# code in the sample file, see C# NetDocCell.cs File.) The
getNewData method returns a
System.Object array that contains:
A
System.Stringobject for the graph labelA nested
System.Objectarray that contains:A
System.Stringobject for the x-axis labelA
System.Doublearray for the y-coordinates
Convert the returned nested array to a cell array that contains all the data:
graph = NetDocCell.MyGraph; mlData = cell(graph.getNewData)
mlData =
[1x1 System.String] [1x1 System.Object[]]Then use the cell array to plot the data. To use the data in the cell array, you must convert the individual .NET objects into MATLAB types.
figure(Name=char(mlData{1}))
plot(double(mlData{2}(2)))
xlabel(char(mlData{2}(1)))Alternatively, you can flatten and organize the nested data by converting each nested .NET array to its own cell array and converting the .NET data to MATLAB variables with MATLAB types. This technique avoids the need to keep track of the different data types and dimensions within one large cell array.
For example, the second element of the mlData cell array is
a nested System.Object array. To access the contents of the
nested array, create another cell array mlPlotData.
mlPlotData = cell(mlData{2})
mlPlotData =
[1x1 System.String] [1x1 System.Double[]]
Then convert the .NET data into MATLAB types, and store the data as MATLAB variables:
% Convert System.String to char mytitle = char(mlData{1}); myxlabel = char(mlPlotData{1}); % Convert System.Double to double y = double(mlPlotData{2});
These additional conversions simplify the code to plot the .NET data:
figure(Name=mytitle) plot(y) xlabel(myxlabel)
C# NetDocCell.cs File
The C# example NetDocCell.cs shows how to handle nested
System.Object objects. To build and load the
NetDocCell assembly, see Build and Load .NET Assembly for MATLAB. The source code is:
using System;
/*
* C# Assembly used in MATLAB .NET documentation.
* Method getNewData is used to demonstrate
* how MATLAB handles a System.Object
* that includes another System.Object.
*/
namespace NetDocCell
{
public class MyGraph
{
public Object[] getNewData()
/*
* Create a System.Object array to use in MATLAB examples.
* Returns containerArr System.Object array containing:
* fLabel System.String object
* plotData System.Object array containing:
* xLabel System.String object
* doubleArr System.Double array
*/
{
String fLabel = "Figure Showing New Graph Data";
Double[] doubleArr = {
18, 32, 3.133, 44, -9.9, -13, 33.03 };
String xLabel = "X-Axis Label";
Object[] plotData = { xLabel, doubleArr };
Object[] containerArr = { fLabel, plotData };
return containerArr;
}
}
}