Main Content

Convert .NET Arrays to Cell Arrays

To convert .NET System.String and System.Object arrays to MATLAB® cell arrays, use the cell function. Elements of the cell array are of the MATLAB type closest to the .NET type. For more information, see .NET Type to MATLAB Type Mapping.

For example, use the .NET System.IO.Directory class to create a cell array of folder names in your c:\ folder.

myList = cell(System.IO.Directory.GetDirectories('c:\'));

Convert Nested System.Object Arrays

The conversion is not recursive for a System.Object array contained within a System.Object array. You must use the cell function to convert each System.Object array.

For an example, build the NetDocCell assembly using the directions in Build a .NET Application for MATLAB Examples. The source code is here.

 C# NetDocCell Source File

Load the assembly and create a cell array, mlData.

dllPath = fullfile('c:','work','NetDocCell.dll');
obj = NetDocCell.MyGraph;
mlData = cell(obj.getNewData)

The cell array contains elements of type

mlData = 
    [1x1 System.String]    [1x1 System.Object[]]

To access the contents of the System.Object array, create another cell array mlPlotData.

mlPlotData = cell(mlData{2})

This cell array contains elements of type

mlPlotData = 
    [1x1 System.String]    [1x1 System.Double[]]

cell Function Syntax for System.Object[,] Arrays

Use this cell function syntax to convert System.DateTime and System.String data contained in a System.Object[,] array to cell arrays of MATLAB data,

A = cell(obj,'ConvertTypes',type)

where obj is a .NET System.Object[,] array, and type is one of the following:

  • {'System.DateTime'} — Convert System.DateTime elements to MATLAB datetime elements.

  • {'System.String'} — Convert System.String elements to MATLAB character arrays.

  • {'all'} — Convert all supported .NET types to equivalent MATLAB types.

A is a cell array, that is the same size as the obj array.

Related Topics