table.table
table: tbl = table (var1, var2, …, varN)
table: tbl = table ('Size', sz, 'VariableTypes', varTypes)
table: tbl = table (…, 'VariableNames', varNames)
table: tbl = table (…, 'RowNames', rowNames)
table: tbl = table (…, 'DimensionNames', dimNames)
Create a new table.
tbl = table (var1, var2, …, varN)
creates a new table with the given variables. The variables passed as
input arguments become the variables of the table. Their names are
automatically detected from the input variable names that you used.
tbl = table ( creates a new table of the
given size, sz, and with the given variable types, varTypes.
sz must be a two-element numeric array, where 'Size', sz,
'VariableTypes', varTypes)sz(1)
specifies the number of rows and sz(2) specifies the
number of variables. The variables will contain the default value for
elements of that type.
tbl = table (…, specifies the variable names to use in the constructed
table. varNames must be either a cell array of character vectors
or a string array with the same number of nonempty and unique elements as
the number of table variables.
'VariableNames',
varNames)
tbl = table (…,
specifies the row names to use in the constructed table. rowNames
must be either a cell array of character vectors or a string array with
the same number of nonempty and unique elements as the number of rows in
the table.
'RowNames', rowNames)
tbl = table (…, specifies the dimension names to use in the constructed
table. dimNames must be either a two-element cell array of
character vectors or a two-element string array with nonempty and unique
elements.
'DimensionNames',
dimNames)
tbl = table () returns an empty table with 0 rows and 0
variables.
Source Code: table
Preallocate a table by specifying its size and the variable data types
sz = [4, 3];
varTypes = {'double', 'datetime', 'string'};
T = table ('Size', sz, 'VariableTypes', varTypes)
T =
4x3 table
Var1 Var2 Var3
____ ____ _________
0 NaT <missing>
0 NaT <missing>
0 NaT <missing>
0 NaT <missing>
Specify variable names with the VariableNames name-value pair argument
sz = [4, 3];
varTypes = {'double', 'datetime', 'string'};
varNames = {'Temperature', 'Time', 'Station'};
T2 = table ('Size', sz, 'VariableTypes', varTypes, 'VariableNames', varNames)
T2 =
4x3 table
Temperature Time Station
___________ ____ _________
0 NaT <missing>
0 NaT <missing>
0 NaT <missing>
0 NaT <missing>
Add rows of data to the first two rows of table T2
T2(1,:) = {75, datetime(2024, 2, 5), string('S1')};
T2(2,:) = {75, datetime(2024, 2, 6), string('S2')}
T2 =
4x3 table
Temperature Time Station
___________ ___________ _________
75 05-Feb-2024 "S1"
75 06-Feb-2024 "S2"
0 NaT <missing>
0 NaT <missing>
Create a table from various types of arrays
T = table (string ({'M';'F';'M'}), [45;32;34], ...
{'NY';'CA';'MA'}, logical ([1;0;0]), ...
'VariableNames', {'Gender', 'Age', 'State', 'Vote'})
T =
3x4 table
Gender Age State Vote
______ ___ ______ _____
"M" 45 {'NY'} true
"F" 32 {'CA'} false
"M" 34 {'MA'} false
Create the same table using the state names as row names
T = table (string ({'M';'F';'M'}), [45;32;34], logical ([1;0;0]), ...
'VariableNames', {'Gender', 'Age', 'Vote'}, ...
'RowNames', {'NY';'CA';'MA'})
T =
3x3 table
Gender Age Vote
______ ___ _____
NY "M" 45 true
CA "F" 32 false
MA "M" 34 false
Display a table with mixed cell arrays as unicolumnar variables and other types as multicolumnar variables
Data_A = {[34, 32]; ['text';'picture']; 'text'; struct('c', 'data'); ...
[true, false]; ['some','text']; {'some','text'}; 25.34};
Data_B = {32, 25; 0.2, 135; 0.123, 456; 42, 5; 154, 12; 32, 10; 4, 4; 9, 94};
Data_C = datetime (2000, [1:8;9:16]', 1);
T = table (Data_A, Data_B, Data_C)
T =
8x3 table
Data_A Data_B Data_C
____________ ____________________ __________________________
{ [34 32]} { [32]} { [25]} 01-Jan-2000 01-Sep-2000
{2x7 char } { [0.2]} {[135]} 01-Feb-2000 01-Oct-2000
{'text' } {[0.123]} {[456]} 01-Mar-2000 01-Nov-2000
{1x1 struct} { [42]} { [5]} 01-Apr-2000 01-Dec-2000
{ [1 0]} { [154]} { [12]} 01-May-2000 01-Jan-2001
{'sometext'} { [32]} { [10]} 01-Jun-2000 01-Feb-2001
{1x2 cell } { [4]} { [4]} 01-Jul-2000 01-Mar-2001
{ [25.34]} { [9]} { [94]} 01-Aug-2000 01-Apr-2001
Create a nested table
T1 = table ([1; 2; 3], [4; 5; 6], [7; 8; 9])
T1 =
3x3 table
Var1 Var2 Var3
____ ____ ____
1 4 7
2 5 8
3 6 9
T2 = table ({'a'; 'b'; 'c'}, {'d'; 'e'; 'f'}, {'g'; 'h'; 'i'})
T2 =
3x3 table
Var1 Var2 Var3
_____ _____ _____
{'a'} {'d'} {'g'}
{'b'} {'e'} {'h'}
{'c'} {'f'} {'i'}
NT = table ([1; 2; 3], T1, [4; 5; 6], T2, {5; 6; 7}, ...
'VariableNames', {'A', 'B', 'C', 'D', 'E'})
NT =
3x5 table
A B C D E
_ ____________________ _ _______________________ _____
Var1 Var2 Var3 Var1 Var2 Var3
____ ____ ____ _____ _____ _____
1 1 4 7 4 {'a'} {'d'} {'g'} {[5]}
2 2 5 8 5 {'b'} {'e'} {'h'} {[6]}
3 3 6 9 6 {'c'} {'f'} {'i'} {[7]}
A table is read with three complementary indexing styles. Build a table whose variables span several types to see how each one behaves.
Name = string ({'Sanchez'; 'Johnson'; 'Li'; 'Diaz'});
Age = [38; 43; 38; 40];
Visit = datetime (2024, [1; 2; 3; 4], [5; 6; 7; 8]);
Gender = categorical ({'M'; 'M'; 'F'; 'F'});
T = table (Name, Age, Visit, Gender)
T =
4x4 table
Name Age Visit Gender
_________ ___ ___________ ______
"Sanchez" 38 05-Jan-2024 M
"Johnson" 43 06-Feb-2024 M
"Li" 38 07-Mar-2024 F
"Diaz" 40 08-Apr-2024 F
Dot indexing extracts one variable as its own native type.
T.Visit
ans =
4x1 datetime array
05-Jan-2024
06-Feb-2024
07-Mar-2024
08-Apr-2024
Parenthesis indexing selects a sub-table — the result is still a table.
T(1:2, {'Name', 'Age'})
ans =
2x2 table
Name Age
_________ ___
"Sanchez" 38
"Johnson" 43
Brace indexing pulls the raw contents out of the selected variables.
T{1:2, 'Age'}
ans = 38 43
Assignment mirrors the three styles. Starting from a mixed-type table:
Name = string ({'Sanchez'; 'Johnson'; 'Li'});
Age = [38; 43; 38];
Gender = categorical ({'M'; 'M'; 'F'});
T = table (Name, Age, Gender)
T =
3x3 table
Name Age Gender
_________ ___ ______
"Sanchez" 38 M
"Johnson" 43 M
"Li" 38 F
Dot assignment adds or overwrites a whole variable (here computed from Age).
T.Senior = T.Age >= 40
T =
3x4 table
Name Age Gender Senior
_________ ___ ______ ______
"Sanchez" 38 M false
"Johnson" 43 M true
"Li" 38 F false
Parenthesis assignment with a cell sets a sub-table; a new row index grows the table, taking one cell per variable in variable order.
T(4, :) = {string('Diaz'), 40, categorical({'F'}), true}
T =
4x4 table
Name Age Gender Senior
_________ ___ ______ ______
"Sanchez" 38 M false
"Johnson" 43 M true
"Li" 38 F false
"Diaz" 40 F true
Dot-then-index writes into part of an existing variable.
T.Age(1) = 39; T.Age'
ans = 39 43 38 40
This implementation fully supports chained referencing and assignment — indexing that reaches through several levels in a single expression. (Octave extension: MATLAB permits chained reference but not chained assignment.)
Age = [38; 43; 38; 40]; Height = [71; 69; 64; 67]; T = table (Age, Height)
T =
4x2 table
Age Height
___ ______
38 71
43 69
38 64
40 67
Chained reference: select a sub-table, then a variable, then an element.
T(2:3, :).Age(1)
ans = 43
Chained assignment writes straight through that same path.
T(2:3, :).Age(1) = 99; T.Age'
ans = 38 99 38 40
Chaining is most useful with nested tables, reaching an inner variable directly — again both for reading and, unlike MATLAB, for writing.
Inner = table ([1; 2; 3], [4; 5; 6], 'VariableNames', {'p', 'q'})
Inner =
3x2 table
p q
_ _
1 4
2 5
3 6
NT = table ([10; 20; 30], Inner, 'VariableNames', {'id', 'inner'})
NT =
3x2 table
id inner
__ ______________
p q
_____ _____
10 1 4
20 2 5
30 3 6
Read the 2nd element of inner variable q.
NT.inner.q(2)
ans = 5
Assign straight into it in one chained expression.
NT.inner.q(2) = 55; NT.inner
ans =
3x2 table
p q
_ __
1 4
2 55
3 6