Main Content

array2table

Convert homogeneous array to table

Description

T = array2table(A) converts an m-by-n array to an m-by-n table. Each column of input A becomes a variable in output T.

array2table 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, array2table uses names of the form "Var1",...,"VarN", where N is the number of columns in A.

example

T = array2table(A,Name=Value) specifies options using one or more name-value arguments in addition to the input argument in the previous syntax. For example, you can specify variable names by setting VariableNames to a string array of variable names.

example

Examples

collapse all

Create a numeric array.

A = [1 4 7; 2 5 8; 3 6 9]
A = 3×3

     1     4     7
     2     5     8
     3     6     9

Convert the array to a table. The table has default variable names created from the name of the input array, A, and the column numbers from the input array.

T = array2table(A)
T=3×3 table
    A1    A2    A3
    __    __    __

    1     4     7 
    2     5     8 
    3     6     9 

Create a numeric array.

A = [1 12 30.48; 2 24 60.96; 3 36 91.44]
A = 3×3

    1.0000   12.0000   30.4800
    2.0000   24.0000   60.9600
    3.0000   36.0000   91.4400

Convert the array to a table with specified variable names.

T = array2table(A,VariableNames=["Feet" "Inches" "Centimeters"])
T=3×3 table
    Feet    Inches    Centimeters
    ____    ______    ___________

     1        12         30.48   
     2        24         60.96   
     3        36         91.44   

Input Arguments

collapse all

Input array, specified as a matrix.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char | string | struct | cell
Complex Number Support: Yes

Name-Value Arguments

collapse all

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.

Example: RowNames=["row1" "row2" "row3"] specifies the row names, row1, row2, and row3 for a table that has three rows.

Row names, specified as a string array or cell array of character vectors, whose elements are nonempty and distinct. The number of row names must equal the number of rows of the input array.

Row names can have any Unicode® characters, including spaces and non-ASCII characters, except for the colon character, :.

If you specify row names that have leading or trailing white space characters, then array2table removes them from the row names.

Variable names, specified as a string array or cell array of character vectors, whose elements are nonempty and distinct. The number of variable names must equal the number of columns of the input array.

Variable names can have any Unicode characters, including spaces and non-ASCII characters. However, a variable name cannot match any table dimension name or the reserved names Properties, RowNames, VariableNames, or the colon character, :.

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

Dimension names can have any Unicode characters, including spaces and non-ASCII characters. However, a dimension name cannot match any table variable name or the reserved names Properties, RowNames, VariableNames, or the colon character, :.

As an alternative, in all releases you can specify dimension names by setting the DimensionNames property of the table.

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 of table.

Tips

  • If A is a cell array, use cell2table(A) to create a table from the contents of the cells in A. Each variable in the table is numeric or a cell array of character vectors. array2table(A) creates a table where each variable is a column of cells.

Extended Capabilities

expand all

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