matlab中的table数据类型的一些使用方法,感觉和python中的pandas差不多

Posted Icy Hunter

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了matlab中的table数据类型的一些使用方法,感觉和python中的pandas差不多相关的知识,希望对你有一定的参考价值。

首先table数据类型是一种比较强的数据类型,其读写文件只需要

readtable('文件名')
writetable(data,"文件名");

下面将展示一些对于table数据类型的操作。

首先,创建包含患者数据的工作区变量。这些变量可以具有任何数据类型,但必须具有相同的行数。
示例

LastName = 'Sanchez';'Johnson';'Li';'Diaz';'Brown';
Age = [38;43;38;40;49];
Smoker = logical([1;0;1;0;1]);
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80];
T = table(LastName,Age,Smoker,Height,Weight,BloodPressure)

结果如下
T=5×6 table
LastName Age Smoker Height Weight BloodPressure
___________ ___ ______ ______ ______ _____________

'Sanchez'    38     true        71       176       124     93  
'Johnson'    43     false       69       163       109     77  
'Li'         38     true        64       131       125     83  
'Diaz'       40     false       67       133       117     75  
'Brown'      49     true        64       119       122     80  

感觉直接在代码里加注释比较好理解吧,就直接上代码好了。
参考的就是matlab说明文档里的东西。

clc,clear
%----------
LastName = 'Sanchez';'Johnson';'Li';'Diaz';'Brown';
Age = [38;43;38;40;49];
Smoker = logical([1;0;1;0;1]);
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80];
T = table(LastName,Age,Smoker,Height,Weight,BloodPressure)

meanHeight = mean(T.Height) % 计算平均身高

T.BMI = (T.Weight*0.453592)./(T.Height*0.0254).^2  % 计算后增加一列
% T.Variables % 返回数据的矩阵形式,但其中不得有非数值数据

%-----------
T = table(categorical('M';'F';'M'),[45;32;34],logical([1;0;0]),...
          'VariableNames','Gender','Age','Vote',...
          'RowNames','NY';'CA';'MA')% 创建一个table的方法并指定行索引和列索引


%------------
LastName = 'Sanchez';'Johnson';'Lee';'Diaz';'Brown';
Age = [38;43;38;40;49];
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
T = table(Age,Weight,Height,'RowNames',LastName)

T('Lee',:) % 按行索引一行的数据
T('Lee','Brown',:) % 索引多行数据


%-------------
FlightNum = [1261;547;3489];
Customer = ["Jones";"Brown";"Smith"];
Date = datetime(2016,12,20:22)';
Rating = categorical(["Good";"Poor";"Fair"]);
Comment = ["Flight left on time, not crowded";...
           "Late departure, ran out of dinner options";...
           "Late, but only by half an hour. Otherwise fine."];
T = table(FlightNum,Customer,Date,Rating,Comment)

Customer = cellstr(Customer);  %将某行索引改为指定字符串数据
T = table(FlightNum,Date,Rating,Comment,'RowNames',Customer)


%--------------------
Date = '12/25/11','1/2/12','1/23/12','2/7/12','2/15/12';
location1 = [20 5 13 0 17];
location2 = [18 9 21 5 12];
location3 = [26 10 16 3 15];

T = table;
T.Date = Date';
T.Natick = location1';
T.Boston = location2';
T.Worcester = location3' % 另一种生成table的形式


%------------------------

Age = [38;43;38;40;49];
Smoker = logical([1;0;1;0;1]);
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80];
T = table(Age,Smoker) % 按列索引获取对应列数据
T.Variables  % 转为矩阵就能随便用了
T.('29-May-2019 Blood Pressure Reading') = BloodPressure  % 给的列名不是标识符时
T.Height = Height;
T.Weight = Weight % 给的列名是标识符时
T(:,'Age','Smoker','29-May-2019 Blood Pressure Reading') % 用列名索引看看

x = table(Age,Smoker,Smoker,Smoker,Smoker) % 表名直接换成别的表的表名
x.Properties.VariableNames = T.Properties.VariableNames



      


以上是关于matlab中的table数据类型的一些使用方法,感觉和python中的pandas差不多的主要内容,如果未能解决你的问题,请参考以下文章

MATLAB | MATLAB海洋气象数据colormap配色补充包(NCL color tables)

在matlab中,如何将struct类型转化为矩阵类型?

MATLAB-数组

matlab gui设计中的问题

matlab把workpace的多个数据拼成一个

用PHP替换数据库中的一些字符