这个例子展示了如何添加和删除一个表中的记录。您还可以使用变量编辑器编辑表。
加载示例患者数据和创建一个表,T
。
负载病人T =表(LastName、性别、年龄、身高、体重、吸烟,收缩压、舒张压);大小(T)
ans =1×2100年8
桌上,T
,有100行和八个变量(列)。
从一个用逗号分隔的文件,读取数据更多的病人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行。
表
|readtable
|array2table
|cell2table
|struct2table