MATLAB advanced data types table

substitution table data type using the statistics toolbox in the dataset, to generate a table of data with the header.

  1. Generating a data table read table = readtable('1.txt')
    Here Insert Picture Description
    by a comma or space separated, saved as txt or CSV format.
    Here Insert Picture Description2. The table generated call the constructor
number = ['1'; '2'; '3'] % 也可以是cell{}
value = ['1'; '2'; '3' ] % 也可以是cell{}
colName = ["number" ,"value"] % 也可以是cell{},如果是[]需要是""而不能是''
tabl1 = table(number,value,'VariableNames',colName)

Here Insert Picture Description
2. array2table, cell2table, struct2table converted to table
1. array2table
· · · for converting the array into a table, similar to the above table generated by the constructor, the need to specify additional header.

ta = table(array,'VariableNames',colName) % 注意需要维度匹配
array = table2array(ta) % 表头会被去掉
  1. cell2table
    · · · for converting into a cellular array table, similar to the above table generated by the constructor, the need to specify additional header.
ta = table(cell,'VariableNames',colName) % 注意需要维度匹配
cell = table2array(ta) % 表头会被去掉
  1. struct2table
    · · · for converting into Table structure, similar to the above-specified header to generate Table, with no additional constructors, is the name of the header field.
stu.name = ["AA","BB","CC"]';
stu.grade = [1 2 3]';
table = struct2table(stu)
kk = table2struct(table) % 转换成struct数组,与stu不同,体会区别

Here Insert Picture Description
Conversion back to the original structure and the structure is different, the difference can be seen from FIG.


To access and delete table and general arrays are very similar, because operators are overloaded Well, this not elaborate, can try their own hands, it has two access methods, type of data returned is not the same.

table(1,:) % 返回类型是table
table{1,:} % 返回类型是array
table.name % 返回类型是array,与上面的方法效果一样

Here Insert Picture Description

table.name = []; % 删掉某列,某个单元同理
[table1; table2] % 合并table
[table1  table2] % 合并table  需要注意的是,两个都必须是同类型的表格
table.new = {...} % 这样可以直接增加一栏,增加一行可以考虑生成table再合并

A need to introduce more important function is varfun (), the function is mainly used to quantization operation, and cellfun and arrayfun is the same, except for using this table for each element in operation.

out = varfun(@fun,table) 

The combined operating table is mainly join (), outerjoin (), innerjoin (), these functions are used to combine form, function more about the table we can see the table object's properties .
Here Insert Picture Description

Published 58 original articles · won praise 69 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_43157190/article/details/104721950
Recommended