"Brown"
49
true
64
119
122
80
Preallocate Table and Fill Rows
Sometimes you know the sizes and data types of the data that you want to store in a table, but you
plan to assign the data later. Perhaps you plan to add only a few rows at a time. In that case,
preallocating space in the table and then assigning values to empty rows can be more efficient.
For example, to preallocate space for a table to contain time and temperature readings at different
stations, use the table function. Instead of supplying input arrays, specify the sizes and data types of
the table variables. To give them names, specify the 'VariableNames' argument. Preallocation fills
table variables with default values that are appropriate for their data types.
sz = [4 3];
varTypes = ["double","datetime","string"];
varNames = ["Temperature","Time","Station"];
temps = table('Size',sz,'VariableTypes',varTypes,'VariableNames',varNames)
temps=4×3 table
Temperature
Time
Station
___________
____
_________
0
NaT
0
NaT
0
NaT
0
NaT
One way to assign or add a row to a table is to assign a cell array to a row. If the cell array is a row
vector and its elements match the data types of their respective variables, then the assignment
converts the cell array to a table row. However, you can assign only one row at a time using cell
arrays. Assign values to the first two rows.
temps(1,:) = {75,datetime('now'),"S1"};
temps(2,:) = {68,datetime('now')+1,"S2"}
temps=4×3 table
Temperature
Time
Station
___________
____________________
_________
75
01-Sep-2021 16:04:11
"S1"
68
02-Sep-2021 16:04:12
"S2"
0
NaT
0
NaT
As an alternative, you can assign rows from a smaller table into a larger table. With this method, you
can assign one or more rows at a time.
temps(3:4,:) = table([63;72],[datetime('now')+2;datetime('now')+3],["S3";"S4"])
temps=4×3 table
Temperature
Time
Station
___________
____________________
_______
75
01-Sep-2021 16:04:11
"S1"
68
02-Sep-2021 16:04:12
"S2"
9 Tables
9-4
49
true
64
119
122
80
Preallocate Table and Fill Rows
Sometimes you know the sizes and data types of the data that you want to store in a table, but you
plan to assign the data later. Perhaps you plan to add only a few rows at a time. In that case,
preallocating space in the table and then assigning values to empty rows can be more efficient.
For example, to preallocate space for a table to contain time and temperature readings at different
stations, use the table function. Instead of supplying input arrays, specify the sizes and data types of
the table variables. To give them names, specify the 'VariableNames' argument. Preallocation fills
table variables with default values that are appropriate for their data types.
sz = [4 3];
varTypes = ["double","datetime","string"];
varNames = ["Temperature","Time","Station"];
temps = table('Size',sz,'VariableTypes',varTypes,'VariableNames',varNames)
temps=4×3 table
Temperature
Time
Station
___________
____
_________
0
NaT
0
NaT
0
NaT
0
NaT
One way to assign or add a row to a table is to assign a cell array to a row. If the cell array is a row
vector and its elements match the data types of their respective variables, then the assignment
converts the cell array to a table row. However, you can assign only one row at a time using cell
arrays. Assign values to the first two rows.
temps(1,:) = {75,datetime('now'),"S1"};
temps(2,:) = {68,datetime('now')+1,"S2"}
temps=4×3 table
Temperature
Time
Station
___________
____________________
_________
75
01-Sep-2021 16:04:11
"S1"
68
02-Sep-2021 16:04:12
"S2"
0
NaT
0
NaT
As an alternative, you can assign rows from a smaller table into a larger table. With this method, you
can assign one or more rows at a time.
temps(3:4,:) = table([63;72],[datetime('now')+2;datetime('now')+3],["S3";"S4"])
temps=4×3 table
Temperature
Time
Station
___________
____________________
_______
75
01-Sep-2021 16:04:11
"S1"
68
02-Sep-2021 16:04:12
"S2"
9 Tables
9-4
