array2table

Convert homogeneous array to table

Syntax

Description

example

T = array2table(A) converts the m-by-n array, A, to an m-by-n table, T. Each column of A becomes a variable in 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 strings of the form 'Var1',...,'VarN', where N is the number of columns in A.

example

T = array2table(A,Name,Value) creates a table from an array, A, 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 Numeric Array to Table

Create an array of numeric data.

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

     1     4     7
     2     5     8
     3     6     9

Convert the array, A, to a table.

T = array2table(A)
T = 

    A1    A2    A3
    __    __    __

    1     4     7 
    2     5     8 
    3     6     9 

The table has variable names that append the column number to the input array name, A.

Convert Array to Table Including Variable Names

Create an array of numeric data.

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

    1.0000   12.0000   30.4800
    2.0000   24.0000   60.9600
    3.0000   36.0000   91.4400

Convert the array, A, to a table and include variable names.

T = array2table(A,...
    'VariableNames',{'Feet' 'Inches' 'Centimeters'})
T = 

    Feet    Inches    Centimeters
    ____    ______    ___________

    1       12        30.48      
    2       24        60.96      
    3       36        91.44      

Input Arguments

expand all

A — Input arraymatrix

Input array, specified as a matrix.

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

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(A,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(A,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.

More About

expand all

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 strings. array2table(A) creates a table where each variable is a column of cells.

Was this topic helpful?