表是用于存储具有相同数量行的面向列的变量的容器。括号使您可以在表中选择数据子集并保留表容器。卷曲括号和点索引使您可以从表中提取数据。
If you use curly braces, the resulting array is the horizontal concatenation of the specified table variables containing only the specified rows. The data types of all the specified variables must be compatible for concatenation. You can then perform calculations using MATLAB®functions.
Dot indexing extracts data from one table variable. The result is an array of the same data type as extracted variable. You can follow the dot indexing with parentheses to specify a subset of rows to extract from a variable.
t.variables
horizontally concatenates all table variables into an array.t.variables
等同于t {:,:}
。
to subscript into a table and select variables of a specified type, use theVARTYPE
功能。
Consider a table,t
。
type of Indexing | Result | 句法 | Rows | Variables |
---|---|---|---|---|
Parentheses | table | t( |
One or more rows, specified by |
一个或多个变量,由 |
大括号 | 提取的数据 | t { |
One or more rows, specified by |
一个或多个变量,由 |
Dot Indexing | 提取的数据 |
|
所有行 |
一个变量,由 |
Dot Indexing | 提取的数据 |
|
One or more rows, specified by |
一个变量,由 |
Variables Property | 提取的数据 |
|
所有行 |
所有变量可以水平串联到数组中时它们 |
通过可变类型订阅 | table |
|
One or more rows, specified by |
指定的一个或多个变量 |
通过可变类型订阅 | 提取的数据 |
|
One or more rows, specified by |
指定的一个或多个变量 |
When indexing into a table with parentheses, curly braces, or dot indexing, you can specify
as a colon, numeric indices, or logical expressions. Furthermore, you can index by name using a single row name or a cell array of row names.rows
逻辑表达式可以包含卷曲括号或点索引来提取数据,您可以从中定义行的子集。例如,rows = T.Var2>0
returns a logical array with logicaltrue
((1
)对于变量中的值的行var2
is greater than zero.
当用括号或卷曲括号索引到表格时,您可以指定
作为结肠,数字索引,逻辑表达式,单个变量名称,可变名称的单元格数组,或作为输出var
VARTYPE
功能。。
使用DOT索引时,您必须指定单个变量以访问。对于单个变量名称,请使用T.
。For a single variable index, specified as a positive integer, usevar
t (
。varindex
)
this example shows how to create a table from a subset of a larger table.
加载样本数据
加载样本患者数据并创建表。使用唯一标识符姓
作为行名。
加载patients患者=表(年龄,性别,身高,体重,吸烟者,...'Rownames',姓);
the table,patients
,包含100行和5个变量。
View the data type, description, units, and other descriptive statistics for each variable by using概括
总结表。
概括((patients)
Variables: Age: 100x1 double Values: Min 25 Median 39 Max 50 Gender: 100x1 cell array of character vectors Height: 100x1 double Values: Min 60 Median 67 Max 72 Weight: 100x1 double Values: Min 111 Median 142.5 Max 202 Smoker: 100x1 logical Values: True 34 False 66
Index Using Numeric Indices
创建一个串曲,其中包含前五行和表中的所有变量,patients
。使用括号内的数字索引来指定所需的行和变量。这类似于用数字阵列索引。
T1 =患者(1:5,:)
T1=5×5 table年龄性别身高体重吸烟者___ __________________________________________________er'71 176 true Johnson 43'Male'69 163 False Williams 38'女性'64 131 False Jones 40'女性'67 133 FALSE BROWN 49''64 119'64 119 FALSE FALSE FALSE
T1
是一张5 x-5桌。除数字索引外,您还可以在括号内使用行或变量名称。在这种情况下,使用行索引和结肠比使用行或变量名称更紧凑。
Index Using Names
选择all the data for the patients with the last names'Adams'
and'棕色的'
。在这种情况下,使用行名比使用数字索引要简单。
T2 =患者({'Adams',,,,'棕色的'},:)
T2=2×5 table年龄Gender Height Weight Smoker ___ ________ ______ ______ ______ Adams 48 'Female' 66 137 false Brown 49 'Female' 64 119 false
T2
is a 2-by-5 table.
Index Using a Logical Expression
Create a new table,T3
,,,,containing the gender, height, and weight of the patients under the age of 30. Select only the rows where the value in the variable,年龄
,小于30。
使用点符号从表变量中提取数据和逻辑表达式来根据提取的数据来定义行的子集。
rows = patients.Age<30; vars = {'性别',,,,'Height',,,,'Weight'};
rows
is a 100-by-1 logical array containing logicaltrue
((1
)对于变量中的值的行,年龄
,小于30。
使用括号返回包含数据子集的表。
T3 =患者(行,var)
T3=15×3桌Gender Height Weight ________ ______ ______ Moore 'Male' 68 183 Jackson 'Male' 71 174 Garcia 'Female' 69 131 Walker 'Female' 65 123 Hall 'Male' 70 189 Young 'Female' 63 114 Hill 'Female' 64 138 Rivera 'Female' 63 130 Cooper 'Female' 65 127 Cox 'Female' 66 111 Howard 'Female' 68 134 James 'Male' 66 186 Jenkins 'Male' 69 189 Perry 'Female' 64 120 Alexander 'Male' 69 171
T3
is a 15-by-3 table.
此示例显示了如何使用卷曲括号或点索引提取表内容。
加载样本数据
加载样本患者数据并创建表。使用唯一标识符姓
作为行名。
加载patients患者=表(年龄,性别,身高,体重,吸烟者,...'Rownames',姓);
the table,patients
,包含100行和5个变量。
提取多行和多个变量
Extract data from multiple variables in the table,patients
by using curly braces. Since dot indexing extracts data from a single variable at a time, braces are more convenient when you want to extract more than one variable.
提取前五名患者的身高和体重。使用数字索引选择行的子集,1:5
,,,,and variable names to select the subset of variables,{身高体重}
。
a =患者{1:5,{'Height',,,,'Weight'}}
a =5×271 176 69 163 64 131 67 133 64 119
一个
是一个5 x-2数字阵列。
从一个变量中提取数据
使用DOT索引轻松提取单个变量的内容。在变量中绘制数字数据的直方图,重量
。
图()直方图(患者。年)标题(“患者体重”)
病人
is a double-precision column vector with 100 rows. Alternatively, you can use curly braces,patients{:,'Weight'}
,提取变量的所有行重量
。
to specify a subset of rows for a single variable, you can follow the dot indexing with parentheses or curly braces. Extract the heights of the nonsmoker patients under the age of 30.
使用点符号从表变量中提取数据和逻辑表达式来根据提取的数据来定义行的子集。
行=患者。Smoker== false&cterate.AGE <30;
Use dot notation to extract the desired rows from the variable,Height
。
patients.Height(rows)
ans =11×168 71 70 63 64 63 65 66 68 66⋮
输出是11 x-1数字阵列。另外,您可以指定单个变量,Height
,,,,within curly braces to extract the desired data,patients{rows,'Height'}
。