Table

A table in MATLAB represents a datatype for collecting heterogeneous elements with its metadata properties, such as variable names, row and column headers, descriptions, and unit of measurement of the variables, all in one container. Tables are particularly suitable for containing tabular data that is often placed in columns in a text file or spreadsheet. Each variable in the table can host a different type of data, but must have the same number of rows. A typical use of a table is to store experimental data, in which the rows represent the different observations of the phenomenon and the columns represent the different measured variables.

Here is a dataset example:

Figure 2.13: Simulated hospital data

Let's consider now how to create a table from variables in the MATLAB workspace and how to proceed with its visualization. Alternatively, we can use the import tool or the readtable() function to create a table from an existing spreadsheet or text file. When importing data from an external file with these tools, each column becomes a table variable. To get the data to be included in the table, quickly and easily, we can draw on the many examples in the software distribution. In this case, we load in the MATLAB workspace the MAT-file hospital.mat (present in software distribution), which contains simulated hospital data for 100 patients. After doing this we will be able to see all variables contained in the MATLAB workspace (Figure 2.12):

>> load hospital
>> whos
Name Size Bytes Class Attributes
Description 1x23 46 char
hospital 100x7 46480 dataset

We can access variable data, or select a subset of variables, by using variable (column) names and dot indexing. For our needs we will extract individual fields in separate variables:

>> LastName=hospital.LastName;
>> Sex=hospital.Sex;
>> Age=hospital.Age;
>> Weight=hospital.Weight;

Now we will create a table and populate it with data contained in the fields (columns) Gender, Smoker, Height, and Weight already present as work variables, so we will only view the top five rows for space reasons:

>> TablePatients = table(LastName,Sex,Age,Weight);
>> TablePatients(1:5,:)
ans =
10×4 table
LastName Sex Age Weight
__________ ______ ___ ______
'SMITH' Male 38 176
'JOHNSON' Male 43 163
'WILLIAMS' Female 38 131
'JONES' Female 40 133
'BROWN' Female 49 119

We can add new fields using dot notation. First, we extract the blood pressure from the starting dataset, dividing it into two variables:

>> BlPrMax=hospital.BloodPressure(:,1);
>> BlPrMin=hospital.BloodPressure(:,2);

 Now add the two new variables to the table:

>> TablePatients.BlPrMax=BlPrMax;
>> TablePatients.BlPrMin=BlPrMin;

To confirm the correctness of the operations, we print the first five rows of the table and only three columns:

>> TablePatients(1:5,[1 5:6])
ans =
5×3 table
LastName BlPrMax BlPrMin
__________ _______ _______
'SMITH' 124 93
'JOHNSON' 109 77
'WILLIAMS' 125 83
'JONES' 117 75
'BROWN' 122 80

We can create a summary table to see the datatype, description, units of measurement, and other descriptive statistics for each variable, using the summary() function:

>> summary(TablePatients)
Variables:
LastName: 100×1 cell array of character vectors
Sex: 100×1 categorical
Values:
Female 53
Male 47
Age: 100×1 double
Values:
Min 25
Median 39
Max 50
Weight: 100×1 double
Values:
Min 111
Median 142.5
Max 202
BlPrMax: 100×1 double
Values:
Min 109
Median 122
Max 138
BlPrMin: 100×1 double
Values:
Min 68
Median 81.5
Max 99
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset