这个示例展示了如何在表中添加和删除行。您还可以使用Variables Editor编辑表。
加载示例患者数据并创建一个表,T
.
负载病人T =表(LastName、性别、年龄、身高、体重、吸烟,收缩压、舒张压);大小(T)
ans =1×2100年8
桌上,T
,有100行和8个变量(列)。
从逗号分隔的文件中读取更多患者的数据,morePatients.csv
,变成一张表,T2
.然后,追加来自的行T2
到桌子的末端,T。
T2 = readtable (“morePatients.csv”);Tnew = (T, T2);大小(Tnew)
ans =1×2104年8
表Tnew
有104行。为了垂直连接两个表,两个表必须具有相同数量的变量和相同的变量名。如果变量名不同,可以直接将一个表中的新行分配给另一个表中的行。例如,T(:端+ 1:端+ 4)= T2
.
若要追加存储在单元格数组中的新行,请将单元格数组垂直连接到表的末尾。当单元格数组具有正确的列数时,您可以直接从单元格数组进行连接,并且单元格的内容可以连接到相应的表变量。
cellPatients = {“爱德华”,“男”、42、70158、0116、83;“福尔克”,“女”, 28岁,62125、1120、71};Tnew = [Tnew; cellPatients];大小(Tnew)
ans =1×2106年8
控件还可以将单元格数组转换为表cell2table
函数。
还可以追加存储在结构中的新行。将结构转换为表,然后连接表。
structPatients(1, 1)。LastName =“乔治”;structPatients(1, 1)。性别=“男”;structPatients(1, 1)。年龄= 45;structPatients(1, 1)。身高= 76;structPatients(1, 1)。重量= 182;structPatients(1, 1)。吸烟者= 1;structPatients(1, 1)。收缩压= 132;structPatients(1, 1)。舒张压= 85;structPatients(2, 1)。LastName =“哈德利”;structPatients(2, 1)。性别=“女”;structPatients(2, 1)。年龄= 29;structPatients(2, 1)。身高= 58;structPatients(2, 1)。重量= 120;structPatients(2, 1)。吸烟者= 0;structPatients(2, 1)。收缩压= 112;structPatients(2, 1)。舒张压= 70;Tnew = [Tnew; struct2table (structPatients)];大小(Tnew)
ans =1×2108年8
若要省略表中重复的任何行,请使用独特的
函数。
Tnew =独特(Tnew);大小(Tnew)
ans =1×2106年8
独特的
删除两个重复的行。
从表中删除第18、20和21行。
Tnew([18、20、21 ],:) = [];大小(Tnew)
ans =1×2103年8
该表包含103名患者的信息。
首先,指定标识符变量,姓
,作为行名。然后,删除变量,姓
,从Tnew
.最后,使用行名索引和删除行。
Tnew.Properties.RowNames = Tnew.LastName;Tnew。LastName = [];Tnew (“史密斯”:) = [];大小(Tnew)
ans =1×2102年7
表现在少了一行,少了一个变量。
您还可以在表中搜索观察结果。例如,删除年龄在30岁以下的任何患者的行。
删除= Tnew。< 30岁;: Tnew(删除)= [];大小(Tnew)
ans =1×285年7
该表现在少了17行。
array2table
|cell2table
|readtable
|struct2table
|表格