Convert cell array to table (2024)

Main Content

Convert cell array to table

collapse all in page

Syntax

T = cell2table(C)

T = cell2table(C,Name,Value)

Description

T = cell2table(C) converts the contents of an m-by-n cell array, C, to an m-by-n table, T. Each column of C provides the data contained in a variable of T.

To create variable names in the output table, cell2table appends column numbers to the input array name. If the input array has no name, then cell2table creates variable names of the form "Var1",...,"VarN", where N is the number of columns in C.

example

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

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

Examples

collapse all

Convert Cell Array to Table

Open Live Script

Create a cell array that contains strings and numeric data. (Cell arrays of strings are not recommended. But in this case, it is appropriate to include strings in a cell array that contains both strings and numbers. This cell array is not a container for text, but for values that are grouped together though they have different data types.)

C = {5 "cereal" 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 table 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 string arrays.

Convert Column Headings to Variable Names

Open Live Script

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. (Cell arrays of strings are not recommended. But in this case, it is appropriate to include strings in a cell array that contains strings, numbers, and logical values. This cell array is not a container for text, but for values that are grouped together though they have different data types.)

Patients = {"LastName" "Age" "Height" "Weight" "Smoker";... "Chang" 38 71 176 true;... "Brown" 43 69 163 false;... "Ruiz" 38 64 131 false;... "Lee" 38 64 131 false;... "Smith" 40 67 133 false;... "Garcia" 49 64 119 false}
Patients=7×5 cell array {["LastName"]} {["Age"]} {["Height"]} {["Weight"]} {["Smoker"]} {["Chang" ]} {[ 38]} {[ 71]} {[ 176]} {[ 1]} {["Brown" ]} {[ 43]} {[ 69]} {[ 163]} {[ 0]} {["Ruiz" ]} {[ 38]} {[ 64]} {[ 131]} {[ 0]} {["Lee" ]} {[ 38]} {[ 64]} {[ 131]} {[ 0]} {["Smith" ]} {[ 40]} {[ 67]} {[ 133]} {[ 0]} {["Garcia" ]} {[ 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 table C1 C2 C3 C4 C5 ________ __ __ ___ _____ "Chang" 38 71 176 true "Brown" 43 69 163 false "Ruiz" 38 64 131 false "Lee" 38 64 131 false "Smith" 40 67 133 false "Garcia" 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 include the names from the first row of the cell array. To extract the names from the first row, use curly braces. Then concatenate the names into a string array. Assign the string array to T.Properties.VariableNames.

LastName = [Patients{1,:}]
LastName = 1x5 string "LastName" "Age" "Height" "Weight" "Smoker"
T.Properties.VariableNames = LastName
T=6×5 table LastName Age Height Weight Smoker ________ ___ ______ ______ ______ "Chang" 38 71 176 true "Brown" 43 69 163 false "Ruiz" 38 64 131 false "Lee" 38 64 131 false "Smith" 40 67 133 false "Garcia" 49 64 119 false 

Input Arguments

collapse all

CInput cell array
2-D cell array

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

  • If the contents of the cells in a column of C have 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 pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: "RowNames",["row1","row2","row3"] uses the row names, row1, row2, and row3 for the table, T.

RowNamesRow names
{} (default) | string array | cell array of character vectors

Row names, specified as the comma-separated pair consisting of "RowNames" and a string array or a cell array of character vectors 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, then cell2table removes them from the row names.

VariableNamesVariable names
string array | cell array of character vectors

Variable names, specified as the comma-separated pair consisting of "VariableNames" and a string array or a cell array of character vectors 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.

DimensionNamesDimension names
two-element cell array of character vectors | two-element string array

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.

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

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

Output Arguments

collapse all

Extended Capabilities

Thread-Based Environment
Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.

Version History

Introduced in R2013b

expand all

See Also

table2cell | array2table | struct2table | table | isvarname

Topics

  • Access Data in Tables

MATLAB-Befehl

Sie haben auf einen Link geklickt, der diesem MATLAB-Befehl entspricht:

 

Führen Sie den Befehl durch Eingabe in das MATLAB-Befehlsfenster aus. Webbrowser unterstützen keine MATLAB-Befehle.

Convert cell array to table (1)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本 (日本語)
  • 한국 (한국어)

Contact your local office

Convert cell array to table (2024)
Top Articles
Latest Posts
Article information

Author: Amb. Frankie Simonis

Last Updated:

Views: 5770

Rating: 4.6 / 5 (56 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Amb. Frankie Simonis

Birthday: 1998-02-19

Address: 64841 Delmar Isle, North Wiley, OR 74073

Phone: +17844167847676

Job: Forward IT Agent

Hobby: LARPing, Kitesurfing, Sewing, Digital arts, Sand art, Gardening, Dance

Introduction: My name is Amb. Frankie Simonis, I am a hilarious, enchanting, energetic, cooperative, innocent, cute, joyous person who loves writing and wants to share my knowledge and understanding with you.