Age: 38
Smoker: 1
BloodPressure: [124 93]
Fields in a structure array are analogous to variables in a table. However, unlike with
tables, you cannot enforce homogeneity within a field. For example, you can have some
values of S.Gender that are categorical array elements, Male or Female, others that are
character vectors, 'Male' or 'Female', and others that are integers, 0 or 1.
Now consider the same data stored in a scalar structure, with four fields each containing
one variable from the table.
ScalarStruct = struct(...
'Gender',{Gender},...
'Age',Age,...
'Smoker',Smoker,...
'BloodPressure',BloodPressure)
ScalarStruct = struct with fields:
Gender: [100x1 categorical]
Age: [100x1 double]
Smoker: [100x1 logical]
BloodPressure: [100x2 double]
Unlike with tables, you cannot enforce that the data is rectangular. For example, the field
ScalarStruct.Age can be a different length than the other fields.
A table allows you to maintain the rectangular structure (like a structure array) and
enforce homogeneity of variables (like fields in a scalar structure). Although cell arrays do
not have named fields, they have many of the same disadvantages as structure arrays and
scalar structures. If you have rectangular data that is homogeneous in each variable,
consider using a table. Then you can use numeric or named indexing, and you can use
table properties to store metadata.
Access Data Using Numeric or Named Indexing
You can index into a table using parentheses, curly braces, or dot indexing. Parentheses
allow you to select a subset of the data in a table and preserve the table container. Curly
braces and dot indexing allow you to extract data from a table. Within each table indexing
method, you can specify the rows or variables to access by name or by numeric index.
9 Tables