askvity

How to convert cell array to table in MATLAB?

Published in MATLAB Data Conversion 4 mins read

Converting a cell array to a table in MATLAB is a common operation, and the most direct way to achieve this is by using the built-in cell2table function.

Using the cell2table Function

The primary function for converting a cell array into a table in MATLAB is cell2table.

According to the reference, T = cell2table( C ) converts the contents of an m-by-n cell array to an m-by-n table. This means that the number of rows and columns in the resulting table T will be the same as the input cell array C.

Each column from the input cell array C becomes a distinct variable (column) in the output table T. The data type of each table variable is automatically determined based on the data within the corresponding cell array column.

Default Variable Naming

When you use the basic T = cell2table(C) syntax, MATLAB needs to assign names to the variables (columns) in the new table. As the reference states, to create variable names in the output table, cell2table appends column numbers to the input array name. If the input cell array doesn't have a specific name when passed to the function (e.g., you pass a literal cell array like {'A', 1; 'B', 2}), MATLAB often uses a default prefix like Var followed by the column number (e.g., Var1, Var2, etc.).

Example: Basic Conversion

Let's consider a simple cell array C holding mixed data types, such as names and ages:

C = {'Alice', 30; 'Bob', 25; 'Charlie', 35};
T = cell2table(C);
disp(T);

Output:

    Var1     Var2
    ______    ____

    'Alice'    30
    'Bob'      25
    'Charlie'  35

In this example, C is a 3x2 cell array. The resulting table T is also 3x2. By default, cell2table named the columns Var1 and Var2.

Customizing Table Properties

While the basic conversion is straightforward, you often want more descriptive names for your table variables and potentially assign names to your rows.

Specifying Variable Names

You can provide custom names for the table variables using the 'VariableNames' name-value pair argument with cell2table. The custom names must be provided as a cell array of strings or character vectors, with each name corresponding to a column in your cell array.

C = {'Alice', 30; 'Bob', 25; 'Charlie', 35};
variableNames = {'Name', 'Age'}; % Specify names for the two columns
T = cell2table(C, 'VariableNames', variableNames);
disp(T);

Output:

      Name     Age
    _________    ____

    'Alice'      30
    'Bob'        25
    'Charlie'    35

This is generally the preferred method when converting structured cell array data, as it makes the table much more readable and easier to work with.

Specifying Row Names

Similarly, you can assign names to the rows of the table using the 'RowNames' name-value pair argument. The row names should correspond to the rows of your cell array and must be unique strings or character vectors.

C = {'Alice', 30; 'Bob', 25; 'Charlie', 35};
variableNames = {'Name', 'Age'};
rowNames = {'Person1', 'Person2', 'Person3'}; % Specify names for the three rows
T = cell2table(C, 'VariableNames', variableNames, 'RowNames', rowNames);
disp(T);

Output:

            Name     Age
          _________    ____

    Person1   'Alice'    30
    Person2   'Bob'        25
    Person3   'Charlie'  35

Using cell2table with appropriate variable and row names makes the resulting table a powerful and self-descriptive data structure for further analysis and organization in MATLAB.

Related Articles