cell2table

Convert cell array to table

Syntax

  • T = cell2table(C)
  • T = cell2table(C,Name,Value) example

Description

T = cell2table(C) converts the contents of an m-by-n cell array, C, to an m-by-n table, T. Each variable in the table, T, is numeric, with a data type double, or a cell array of strings.

cell2table uses the input array name appended with the column number for the variable names in the table. If these names are not valid MATLAB® identifiers, cell2table uses strings of the form 'Var1',...,'VarN' where N is the number of columns in C.

example

T = cell2table(C,Name,Value) creates a table from a cell array, C, with additional options specified by one or more Name,Value pair arguments.

For example, you can specify row names or variable names to include in the table.

Examples

expand all

Convert Cell Array to Table

Create a cell array containing strings and numeric data.

C = {5 'cereal' 110 'C+'; 12 'pizza' 140 'B';...
    23 'salmon' 367 'A'; 2 'cookies' 160 'D'}
C = 

    [ 5]    'cereal'     [110]    'C+'
    [12]    'pizza'      [140]    'B' 
    [23]    'salmon'     [367]    'A' 
    [ 2]    'cookies'    [160]    'D' 

Convert the cell array, C, to a table and specify variable names.

T = cell2table(C,...
    'VariableNames',{'Age' 'FavoriteFood' 'Calories' 'NutritionGrade'})
T = 

    Age    FavoriteFood    Calories    NutritionGrade
    ___    ____________    ________    ______________

     5     'cereal'        110         'C+'          
    12     'pizza'         140         'B'           
    23     'salmon'        367         'A'           
     2     'cookies'       160         'D'           

The variables T.Age and T.Calories are numeric while the variables T.FavoriteFood and T.NutritionGrade are cell arrays of strings.

Convert Column Headings to Variable Names

Convert a cell array to a table, and then include the first row from the cell array as variable names for the table.

Create a cell array where the first row contains strings to identify column headings.

Patients = {'Gender' 'Age' 'Height' 'Weight' 'Smoker';...
    'M' 38 71 176 true;...
    'M' 43 69 163 false;...
    'M' 38 64 131 false;...
    'F' 38 64 131 false;...
    'F' 40 67 133 false;...
    'F' 49 64 119 false}
Patients = 

    'Gender'    'Age'    'Height'    'Weight'    'Smoker'
    'M'         [ 38]    [    71]    [   176]    [     1]
    'M'         [ 43]    [    69]    [   163]    [     0]
    'M'         [ 38]    [    64]    [   131]    [     0]
    'F'         [ 38]    [    64]    [   131]    [     0]
    'F'         [ 40]    [    67]    [   133]    [     0]
    'F'         [ 49]    [    64]    [   119]    [     0]

Exclude the columns headings and convert the contents of the cell array to a table.

C = Patients(2:end,:);
T = cell2table(C)
T = 

    C1     C2    C3    C4      C5  
    ___    __    __    ___    _____

    'M'    38    71    176    true 
    'M'    43    69    163    false
    'M'    38    64    131    false
    'F'    38    64    131    false
    'F'    40    67    133    false
    'F'    49    64    119    false

The table, T, has variable names C1,...,C5.

Change the variable names by setting the table property, T.Properties.VariableNames, to the first row of the cell array.

T.Properties.VariableNames = Patients(1,:)
T = 

    Gender    Age    Height    Weight    Smoker
    ______    ___    ______    ______    ______

    'M'       38     71        176       true  
    'M'       43     69        163       false 
    'M'       38     64        131       false 
    'F'       38     64        131       false 
    'F'       40     67        133       false 
    'F'       49     64        119       false 

Input Arguments

expand all

C — Input cell array2-D cell array

Input cell array, specified as a 2-D cell array.

If the cell contents have compatible sizes and types, then cell2table vertically concatenates the contents of the cells in each column of C to create each variable in T. If the cell contents have different sizes or incompatible types, then the corresponding variable in the table, T, is a column of cells.

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: RowNames',{'row1','row2','row3'} uses the row names, row1, row2, and row3 for the table, T.

'RowNames' — Row names for T{} (default) | cell array of nonempty, distinct strings

Row names for T, specified as the comma-separated pair consisting of 'RowNames' and a cell array of nonempty, distinct strings. The number of strings must equal the number of rows, size(C,1).

'VariableNames' — Variable names for Tcell array of nonempty, distinct strings

Variable names for T, specified as the comma-separated pair consisting of 'VariableNames' and a cell array of nonempty, distinct strings. The number of strings must equal the number of variables, size(C,2).

Furthermore, the strings must be valid MATLAB identifiers. If valid MATLAB identifiers are not available for use as variable names, MATLAB uses a cell array of N strings of the form {'Var1' ... 'VarN'}, where N is the number of variables. You can determine valid MATLAB variable names using the function isvarname.

Output Arguments

expand all

T — Output tabletable

Output table, returned as a table. The table can store metadata such as descriptions, variable units, variable names, and row names. For more information, see Table Properties.

Was this topic helpful?