Main Content

Pass Cell Arrays of .NET Data

Example of Cell Arrays of .NET Data

In the Convert Nested System.Object Arrays example, the cell array mlData contains data from the MyGraph.getNewData method. By reading the class documentation in the source file, you can create the following MATLAB® graph:

dllPath = fullfile('c:','work','NetDocCell.dll');
asm = NET.addAssembly(dllPath);
graph = NetDocCell.MyGraph;

% Create cell array containing all data
mlData = cell(graph.getNewData);

% Plot the data and label the graph
figure('Name',char(mlData{1}))
plot(double(mlData{2}(2)))
xlabel(char(mlData{2}(1)))

However, keeping track of data of different types and dimensions and the conversions necessary to map .NET data into MATLAB types is complicated using the cell array structure. Here are some tips for working with the contents of nested System.Object arrays in MATLAB. After reading data from a .NET method:

  • Create cell arrays for all System.Object arrays.

  • Convert the .NET types to MATLAB types, according to the information in Handle Data Returned from .NET Objects.

  • Create MATLAB variables for each type within the cell arrays.

  • Call MATLAB functions with the MATLAB variables.

Create a Cell Array for Each System.Object

This example shows how to copy System.Object data into a cell array.

The following statement creates the cell array mlData:

mlData = cell(graph.getNewData)
mlData = 
    [1x1 System.String]    [1x1 System.Object[]]

This cell array contains elements of these types.

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

mlPlotData = cell(mlData{2})
mlPlotData = 
    [1x1 System.String]    [1x1 System.Double[]]

This cell array contains elements of these types.

Create MATLAB Variables from the .NET Data

Assign cell data to MATLAB variables and convert:

% Create descriptive variables
% Convert System.String to char
mytitle = char(mlData{1});
myxlabel = char(mlPlotData{1});
% Convert System.Double to double
y = double(mlPlotData{2});

Call MATLAB Functions with MATLAB Variables

Create a MATLAB graph with this data:

% Remove the previous figure
close
% Plot the data and label the graph
figure('Name',mytitle,'NumberTitle','off')
plot(y)
xlabel(myxlabel)