修改列的数据类型

表或DataFrame中每列数据具有相同的数据类型。MATLAB和Python都提供了相应的函数或方法修改指定列的数据类型。

下面创建一个表T。

code.matlab
>> T=table({'张三';'李四';'杨二';'王五'},{'M';'F';'M';'F'},[18;19;17;25],...
    [162;169;164;167],...
    'VariableNames',{'Name' 'Gender' 'Age' 'Height'});

用summary函数查看每列的数据类型。

code.matlab
>> summary(T)
Variables:
    Name: 4×1 cell array of character vectors
    Gender: 4×1 cell array of character vectors
    …….

现在Gender列为字符向量组成的元胞数组。

用convertvars函数将Gender列的数据类型修改为分类数组,然后用summary函数查看修改后的表T2各列的数据类型。

code.matlab
>> T2=convertvars(T,{'Gender'},'categorical')
>> summary(T2)
Variables:
    Name: 4×1 cell array of character vectors
    Gender: 4×1 categorical
    ……

现在Gender列的数据类型变成了分类数组。