A table is a data structure that stores information in a table format with rows and columns, each of which can be mnemonically labeled. For example, the following uses the table function to store some simple information for a doctor’s patients.
>>names= {'Harry', 'Sally', 'Jose'}; >>weights= [185; 133; 210]; % Note column vectors >>heights= [74; 65.4; 72.2]; >>patients= table (weights, heights, 'RowNames', names) patients= weights heights ---- ----- Harry 185 74 Sally 133 65.4 Jose 210 72.2
This created a 3 x 2 table, with two variables named weights and heights.
There are many ways to index into tables, to either create a new table that is a subset of the original, or to extract information from the table into other types of data structures. Using parentheses to index, the result is another table; the indexing can be done using integers (as with arrays we have seen so far) or by using row or variable names.
>> patients(1:2, 1) ans = weights _______ Harry 185 Sally 133
>> patients ( {'Harry' 'Jose'}, : ) ans = weights heights _______ _______ Harry 185 74 Jose 210 72.2
Using curly braces to index, the data can be extracted; in the following example, into a double matrix.
>> mat = patients{{'Harry' 'Jose'},:} mat = 185.0000 74.0000 210.0000 72.2000
The summary function can be used for tables; it shows the variables and some statistical data for each.
>> summary (patients) Variables: weights: 3x1 double Values: min 133 median 185 max 210 heights: 3x1 double Values: min 65.4 median 72.2 max 74