FUNCTION REFERENCE

table

Table of named variables (columns) of a common height.

Table of named variables (columns) of a common height.

Syntax

T = table(v1, v2, ...)

Syntax

T = table(..., 'VariableNames', names, 'RowNames', rows)

Syntax

T = table()

Each input is one variable; every variable must have the same number of rows (a variable may have several columns, such as a 3x2 matrix). Variable names come from the input variable names, else Var1, Var2, ... by position. 'VariableNames' (cellstr or string array) and 'RowNames' (one unique name per row) set them explicitly; 'DimensionNames' renames the two dimensions (default {'Row', 'Variables'}). A variable name must be nonempty, at most 63 characters, unique, and may not be 'Properties' or a dimension name (table(1, 'VariableNames', {'Row'}) is an error); dimension names must be valid identifiers.

Access: T.Var, T.(name), T.Var(i), T.Var(i,j) read a variable; T.Var = v replaces or appends a variable (its height must match; a 0x0 table takes the height), T.Var(i) = v writes into it, T.Var = [] deletes it. T(rows, vars) is a table and T{rows, vars} the selected values side by side (an error for incompatible types). Rows are numbers, a logical mask, ':' or row names; variables are numbers, a logical mask, ':', a name, a cellstr or a string array. T(rows, vars) = T2 (same size; T2's variables are matched to the selected ones by name, in any order), T(rows, vars) = {...} and T{rows, vars} = value write; T(rows, :) = [] deletes rows, T(:, vars) = [] deletes variables, and T(end+1, :) = {...} appends a row.

Properties: T.Properties.VariableNames, RowNames, DimensionNames, Description, VariableUnits and VariableDescriptions are read and set (VariableNames must stay unique valid names that differ from 'Properties' and from the two dimension names, so T.Properties and T.Row never return a variable; RowNames unique; '' or {} removes the row names); T.Properties alone is a struct of them.

Functions on tables: istable, class ('table'), height, width, size (rows by variables), numel (height times width), isempty, head, tail, sortrows, isequal, horzcat/vertcat ([T1, T2] needs distinct names and the same height, and when both have row names the same set, which T2's rows then follow in T1's order; [T1; T2] needs the same names in any order, which follow T1), table2array, table2cell, table2struct, writetable. whos shows Class table.

Display: a size line, then each variable under its name and a rule of underscores as wide as the column, four spaces apart. Numbers are right-aligned and printed as the current format prints a scalar; logicals show true/false, cellstr elements {'text'}, strings "text", datetime and duration values their text. Row names are a first column without a header. Trailing blanks are trimmed.

Example

T = table([31; 45], {'Ann'; 'Bob'}, 'VariableNames', {'Age', 'Name'})   % 2x2 table

Example

Age = [31; 45]; T = table(Age); T.Height = [1.6; 1.8]; T.Age(2)   % 45

Example

T = table([1; 2; 3], 'RowNames', {'a', 'b', 'c'}); T({'c', 'a'}, :)   % rows c and a

Example

T = table([3; 1; 2], [10; 20; 30]); S = T(T.Var1 > 1, :)   % 2x2 table

Example

T = table([1; 2]); T(end+1, :) = {3}; height(T)   % 3

Differences from MATLAB: Part 1 of tables. Not supported yet (planned): joins (join, innerjoin, outerjoin), groupsummary, varfun and rowfun, timetable, readtable datetime detection, xlsx tables, categorical variables, the 'Size'/'VariableTypes' constructor, nested tables, sparse, function handle and graphics handle variables (a cell variable may hold handles) and growing a table by assigning past the end of one variable. Tables cannot be saved to MAT files yet. A brace write into a cell variable takes a non-cell value as the content of one cell. Wide tables are not wrapped to the console width.

These are the engine’s help pages. Each page keeps its compatibility notes. File operations are unavailable in the browser.