table

Creates a table: column-oriented data with named variables, like a spreadsheet.

Category: Language Fundamentals > Data Types

Syntax

T = table(var1, var2, ...)

Creates a table from variables of the same height.

T = table(___, 'VariableNames', names)

Names the variables.

Description

A table holds columns of different types side by side: numbers, text, dates, categories. Each column is a named variable, so you work with it by name instead of by position. T.num returns a column, T(2, :) returns a row as a table, T{2, 'num'} returns its content, and size(T) gives rows by variables.

Without 'VariableNames' the variables are called Var1, Var2, and so on, or take the names of the input variables when they are plain variable names. Tables can be sorted with sortrows, grouped with groupsummary, and combined with join.

Input arguments

var1, var2, ...
Arrays with the same number of rows. Each one is a column (a matrix variable keeps several columns).
'VariableNames'
Cell array of names, one per variable. Names must be unique and valid.

Output arguments

T
Table with one row per row of the inputs.

Example

Build, index and slice

T = table([1;2;3], {'a';'b';'c'}, 'VariableNames', {'num','txt'})
T.num
size(T)
T(2,:)
Output
T = 3×2 table

    num     txt 
    ___    _____

     1     {'a'}
     2     {'b'}
     3     {'c'}
ans = 3×1

     1
     2
     3
ans = 1×2

     3     2
ans = 1×2 table

    num     txt 
    ___    _____

     2     {'b'}

Notes

The form table('Size', ..., 'VariableTypes', ...) for preallocating an empty table is not supported by MatLivre.

See also

array2table cell2table readtable sortrows timetable struct2table