Main Content

cell2table

Convert cell array to table

Description

T= cell2table(C)converts the contents of anm-by-ncell array,C, to anm-by-ntable,T. Each column ofCprovides the data contained in a variable ofT.

To create variable names in the output table,cell2tableappends column numbers to the input array name. If the input array has no name, thencell2tablecreates variable names of the form'Var1',...,'VarN', whereNis the number of columns inC.

example

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

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

Examples

collapse all

Create a cell array containing character vectors and numeric data.

C = {5“麦片”110'C+'; 12'pizza'140'B';...23'salmon'367'A'; 2'cookies'160'D'}
C=4×4 cell array{[ 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=4×4 tableAge FavoriteFood Calories NutritionGrade ___ ____________ ________ ______________ 5 {'cereal' } 110 {'C+'} 12 {'pizza' } 140 {'B' } 23 {'salmon' } 367 {'A' } 2 {'cookies'} 160 {'D' }

The variablesT.AgeandT.Caloriesare numeric while the variablesT.FavoriteFoodandT.NutritionGradeare cell arrays of character vectors.

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 character vectors 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=7×5 cell array{'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=6×5 tableC1 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 namesC1,...,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=6×5 tableGender 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

collapse all

Input cell array, specified as a 2-D cell array. Each column ofCprovides data for a table variable.

  • If the contents of the cells in a column ofChave compatible sizes and types, then the corresponding table variable is the vertical concatenation of those contents into an array.

  • If the contents of the cells in a column have different sizes and types, then the corresponding table variable is a cell array.

    • If the contents of the cells in a column are all character vectors, then the corresponding table variable is a cell array of character vectors.

Name-Value Arguments

Specify optional comma-separated pairs ofName,Valuearguments.Nameis the argument name andValueis the corresponding value.Namemust appear inside quotes. You can specify several name and value pair arguments in any order asName1,Value1,...,NameN,ValueN.

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

Row names, specified as the comma-separated pair consisting of'RowNames'and a cell array of character vectors or a string array, whose elements are nonempty and distinct. The number of names must equal the number of rows,size(C,1).

Row names can have any Unicode®characters, including spaces and non-ASCII characters.

If you specify row names that have leading or trailing whitespace characters, thencell2tableremoves them from the row names.

Variable names, specified as the comma-separated pair consisting of'VariableNames'and a cell array of character vectors or a string array, whose elements are nonempty and distinct. The number of names must equal the number of variables,size(C,2).

Variable names can have any Unicode characters, including spaces and non-ASCII characters.

Since R2021a

Dimension names, specified as a two-element cell array of character vectors or two-element string array whose elements are nonempty and distinct.

维度名称可以有Unicode字符,我ncluding spaces and non-ASCII characters.

Before R2021a, you can specify dimension names only by setting theDimensionNamesproperty of the output.

Output Arguments

collapse all

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 the Properties section oftable.

Compatibility Considerations

expand all

Behavior changed in R2019b

Extended Capabilities

Introduced in R2013b