搜索内容 搜索用户

# MATLAB table数据结构 再篇

2016-3-10 01:33| 发布者: ilovematlab| 查看: 56700| 评论: 1|原作者: oopmatlab

## table的操作

### 删除行列

```% 删除行
>> nasdaq(3,:) =[]
nasdaq =
Symbol          Name          MarketCap     IPOYear
______    ________________    __________    _______

'AAPL'    'Apple Inc'         '\$742.63B'    1980
'AMZN'    'Amazon.com Inc'    '\$173.33B'    1997
```

```% 删除列
>> nasdaq(:,2) =[]
nasdaq =
Symbol    MarketCap     IPOYear
______    __________    _______

'AAPL'    '\$742.63B'    1980
'AMZN'    '\$173.33B'    1997
```

```% 删除行
>> nasdaq.IPOYear=[]
nasdaq =
Symbol    MarketCap
______    __________

'AAPL'    '\$742.63B'
'AMZN'    '\$173.33B'
```

### 添加行列

```% 添加列
nasdaq.Sector={'Computer Manufacturing';...
'Consumer Services';...
'Computer Software'}
```

```%  table新增了Sector列
Symbol            Name             MarketCap     IPOYear             Sector
______   _______________________   __________    _______    ________________________
'AAPL'   'Apple Inc'               '\$742.63B'    1980       'Computer Manufacturing'
'AMZN'   'Amazon.com Inc'          '\$173.33B'    1997       'Consumer Services'
'MSFT'   'Microsoft Corporation'   '\$346.9B'     1986       'Computer Software'
```

```% 给table添加行
newTable = cell2table(newCell)
newTable.Properties.VariableNames = {'Symbol','Name','MarketCap','IPOYear','Sector'};
newNasdaq =[nasdaq;newTable]
```

### 合并table

Figure.2 纵向合并table
Figure.1横向合并示例如下，已有两个电话号码簿table, 分别是t1和t2:
```t1 =
Name          Number
_________    ____________

'Abby'       '5086470001'
'Bob'        '5086470002'
'Charlie'    '5086470003'
```
```t2 =
Name         Number
_______    ____________

'Dave'     '5086470004'
'Eric'     '5086470005'
'Frank'    '5086470006'
```

```% 直接串接table
>> new_t =[t1  ; t2]
new_t =
Name          Number
_________    ____________

'Abby'       '5086470001'
'Bob'        '5086470002'
'Charlie'    '5086470003'
'Dave'       '5086470004'
'Eric'       '5086470005'
'Frank'      '5086470006'
```
```% 使用vertcat
>> new_t = vertcat(t1,t2)
new_t =
Name          Number
_________    ____________

'Abby'       '5086470001'
'Bob'        '5086470002'
'Charlie'    '5086470003'
'Dave'       '5086470004'
'Eric'       '5086470005'
'Frank'      '5086470006'
```
Figure.2纵向合并示例如下，已有号码簿t1,以及另一个关于办公室和楼号的table
```t1 =
Name          Number
_________    ____________

'Abby'       '5086470001'
'Bob'        '5086470002'
'Charlie'    '5086470003'
```
```t3 =
Office    Building
______    ________

'331'     'A1'
'201'     'A2'
'328'     'A4'
```

```% 直接串接table
>> new_t = [t1,t3]
new_t =
Name          Number       Office    Building
_________    ____________    ______    ________

'Abby'       '5086470001'    '331'     'A1'
'Bob'        '5086470002'    '201'     'A2'
'Charlie'    '5086470003'    '328'     'A4'
```

```% 使用horzcat
>> new_t = horzcat(t1,t3)
new_t =
Name          Number       Office    Building
_________    ____________    ______    ________

'Abby'       '5086470001'    '331'     'A1'
'Bob'        '5086470002'    '201'     'A2'
'Charlie'    '5086470003'    '328'     'A4'
```

### 操作列数据

```% nasdaq table原始数据
nasdaq =
Symbol             Name              MarketCap     IPOYear
______    _______________________    __________    _______

'AAPL'    'Apple Inc'                '\$742.63B'    1980
'AMZN'    'Amazon.com Inc'           '\$173.33B'    1997
'MSFT'    'Microsoft Corporation'    '\$346.9B'     1986
```

```% helper函数
function out_num = marketcap_helper(in_string)
out_num = str2num(in_string(2:end-1));
end
```

```% 调用cellfun对table列数据进行操作
>> nasdaq.MarketCap = cellfun(@ marketcap_helper,nasdaq.MarketCap)
nasdaq =

Symbol             Name              MarketCap    IPOYear
______    _______________________    _________    _______

'AAPL'    'Apple Inc'                742.63       1980
'AMZN'    'Amazon.com Inc'           173.33       1997
'MSFT'    'Microsoft Corporation'     346.9       1986
```
table类还提供了varfun方法来进行列操作，和使用cellfun的区别是，cellfun的处理对象是table中的一部分，即元胞。而varfun处理的对象直接是table对象。下例中对Yahoo股票table的第二列7天的开盘价求均值：
```% yhoo的table在命令行的显示
yhoo =
date       open     high      low     closing      volumn      adjusted
__________    _____    _____    _____    _______    __________    ________
7.3603e+05    42.57    42.92    42.18    42.68      1.0601e+07    42.68
7.3603e+05     43.6    43.93    42.67    42.98      1.1802e+07    42.98
7.3603e+05    43.98    44.24     43.4    43.44      1.1888e+07    43.44
7.3603e+05    44.18    44.31     43.5    44.16      1.1868e+07    44.16
7.3603e+05    42.08    44.38    41.97    43.99      3.0099e+07    43.99
7.3603e+05     43.7    43.95    42.42    42.62      2.2392e+07    42.62
7.3603e+05    44.06    44.43     43.7    44.11      1.1027e+07    44.11
```

```% varfun对表中的列数据进行操作
>> varfun(@mean,yhoo(:,3))
ans =
mean_high
_________

44.023
```

```% 变换date列的数据格式
>> formatOut = 'dd-mm-yy';
>> yhoo.date = datestr(yhoo.date,formatOut)  % datestr接受table输入
yhoo =
date      open     high      low     closing      volumn      adjusted
________    _____    _____    _____    _______    __________    ________

10-03-15    42.57    42.92    42.18    42.68      1.0601e+07    42.68
09-03-15     43.6    43.93    42.67    42.98      1.1802e+07    42.98
06-03-15    43.98    44.24     43.4    43.44      1.1888e+07    43.44
05-03-15    44.18    44.31     43.5    44.16      1.1868e+07    44.16
04-03-15    42.08    44.38    41.97    43.99      3.0099e+07    43.99
03-03-15     43.7    43.95    42.42    42.62      2.2392e+07    42.62
02-03-15    44.06    44.43     43.7    44.11      1.1027e+07    44.11
```

```% range列的数据来自于high low和closing列数据
>>yhoo.range = (yhoo.high - yhoo.low)./yhoo.closing
yhoo =
date      open     high      low     closing      volumn      adjusted   range
________    _____    _____    _____    _______    __________    ________  ________
10-03-15    42.57    42.92    42.18    42.68      1.0601e+07    42.68     0.017338
09-03-15     43.6    43.93    42.67    42.98      1.1802e+07    42.98     0.029316
06-03-15    43.98    44.24     43.4    43.44      1.1888e+07    43.44     0.019337
05-03-15    44.18    44.31     43.5    44.16      1.1868e+07    44.16     0.018342
04-03-15    42.08    44.38    41.97    43.99      3.0099e+07    43.99     0.054785
03-03-15     43.7    43.95    42.42    42.62      2.2392e+07    42.62     0.035899
02-03-15    44.06    44.43     43.7    44.11      1.1027e+07    44.11      0.01655
```

### 排序

```% 调用cellfun对table列数据进行操作
nasdaq =
Symbol             Name              MarketCap    IPOYear
______    _______________________    _________    _______

'AAPL'    'Apple Inc'                742.63       1980
'AMZN'    'Amazon.com Inc'           173.33       1997
'MSFT'    'Microsoft Corporation'     346.9       1986
```

```% 根据MarketCap列数据进行排序
>> sorted = sortrows(nasdaq,'MarketCap','descend')
sorted  =
Symbol             Name              MarketCap    IPOYear
______    _______________________    _________    _______

'AAPL'    'Apple Inc'                742.63       1980
'MSFT'    'Microsoft Corporation'     346.9       1986
'AMZN'    'Amazon.com Inc'           173.33       1997
```

### 筛选和查找

table的下标也接受logical index, 下例选出所有股票中市值大于200B的股票
```% 筛选
>> nasdaq(nasdaq.MarketCap>200,:)
ans =
Symbol             Name              MarketCap    IPOYear
______    _______________________    _________    _______

'AAPL'    'Apple Inc'                742.63       1980
'MSFT'    'Microsoft Corporation'     346.9       1986
```

```% 筛选
>> nasdaq( (nasdaq.MarketCap>200) & (nasdaq.IPOYear > 1985),:)
ans =
Symbol             Name              MarketCap    IPOYear
______    _______________________    _________    _______

'MSFT'    'Microsoft Corporation'    346.9        1986
```
logical index还可以提供查找功能，下例查找所有行中`Symbol='AMZN'` 的数据
```% 查找
>> nasdaq(strcmp(nasdaq.Symbol,'AMZN'),:)
ans =
Symbol          Name          MarketCap     IPOYear
______    ________________    __________    _______
'AMZN'    'Amazon.com Inc'    '\$173.33B'    1997
```

### 输出到文件

```% writetable
>> wrietable(nasdaq,'mydata.csv')
```
writetable默认的分割符是逗号，writetable还可以通过delimiter来设置分割符，下例空格代替逗号
```% 指定分隔符
writetable(T,'mydata.txt','Delimiter',' ')
```

```% mydata.txt
Symbol Name MarketCap IPOYear
AAPL Apple Inc \$742.63B 1980
AMZN Amazon.com Inc \$173.33B 1997
MSFT Microsoft Corporation \$346.9B 1986
```

## 其它数据类型之间和table相互转换

MATLAB支持table和struct,cell,array之间的相互转换。如图Figure.3所示，下面一一介绍： 本节使用数据如表Table.1所示，内容是美元和人民币的货币转换速查表：
Table.1 美元人民币换算表
USD CNY
1 6.21
5 31.03
10 62.06

```% array2table
>> a = [1  6.21;5 31.03 ;10 62.06 ]
a =
1.0000    6.2100
5.0000   31.0300
10.0000   62.0600
>> t = array2table(a,'VariableNames',{'USD' 'CNY'})   % 通过VariableNames提供表头信息
t =
USD     CNY
___    _____
1      6.21
5     31.03
10     62.06
```

```% table2array
>> ap = table2array(t)
ap =
1.0000    6.2100
5.0000   31.0300
10.0000   62.0600
```

```% struct2table
>> s.USD = [1 ; 5 ;10];
>> s.CNY = [6.21 ;31.03; 62.06];
>> t = struct2table(s)      % 输入s是标量
t =
USD     CNY
___    _____
1      6.21
5     31.03
10     62.06
```
struct2table还接受struct是非标量的输入
```% struct2table 矢量
s(1).USD = 1;
s(1).CNY = 6.21;
s(2).USD = 5;
s(2).CNY = 31.03;
s(3).USD = 10 ;
s(3).CNY = 62.06

% 输入s是矢量
t = struct2table(s)

```
```

s =
1x3 struct array with fields:
USD
CNY
t =
USD     CNY
___    _____
1      6.21
5     31.03
10     62.06
```

```% table2struct
>> sp = table2struct(t)  % non-scalar struct
sp =
3x1 struct array with fields:
USD
CNY
```

Table.3 电话号码簿

Abby 5086470001
Bob 5086470002
Charlie 5086470003
```c = {'Abby', '508647001';...
'Bob','5086470002';...
'Charlie','5086470003'};
t = cell2table(c,...
'VariableNames',{'Name','Number'})

```
```

t =
Name          Number
_________    ____________
'Abby'       '508647001'
'Bob'        '5086470002'
'Charlie'    '5086470003'
```

```% table2cell
>> c = table2cell(t)
c =
'Abby'       '508647001'
'Bob'        '5086470002'
'Charlie'    '5086470003'
```

## 关于作者

oopmatlab，计算物理博士，计算机硕士，《MATLAB面向对象编程-从入门到设计模式》作者，现就职一家科学工程计算公司的任架构组C++软件工程师。业余兴趣包括如何把软件工程中的现代手段，应用到科学和工程计算当中去，来更好的解决复杂的问题。包括如何从整体上设计科学计算程序的结构；如何让程序便于扩展和修改；在改进和开发算法的时候，如何保证程序已有的功能没有收到影响；如何让算法开发和测试系统的建立齐头并进。

 19 鲜花 握手 雷人 路过 鸡蛋

### 最新评论

philip.ren 2021-2-23 16:03

# MATLAB table数据结构 首篇

MATLAB table是R2013b中引入的一个新的数据结构，虽然不像常用的基本数据类型为人熟悉，但是在编程中非常有用。它用来存放表状类型的数据结构，并且支持常见的表和表之间的运算。 ... ... ... ... ... ... ... ...

# MATLAB table数据结构 再篇

MATLAB table是R2013b中引入的一个新的数据结构，虽然不像常用的基本数据类型为人熟悉，但是在编程中非常有用。它用来存放表状类型的数据结构，并且支持常见的表和表之间的运算。 ... ... ... ... ... ... ...

# MATLAB App Designer教程1-6

Mathworks在R2016a中正式推出了GUIDE的替代产品:AppDesigner， 这是在MATLAB图形系统转向使用面向对象系统之后(2014b)，一个重要的后续产品，它旨在顺应Web的潮流,帮助用户利用新的图形系统方便的设计更加美观的GUI. ...

# MATLAB性能测试框架

MATLAB Performance Test 框架是Mathworks 在MATLAB R2016a 中推出的⼀个新的框架，该框架⽤来获得代码性能在统计意义上的数据，还可以⽤来⽐较算法的性能，并且给出详细完整的报告。 ... ... ... ... ... ... ... . ...