数据集中的标题(Matlab) [英] Headers in dataset (Matlab)

查看:135
本文介绍了数据集中的标题(Matlab)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到关于dataset()的任何好的文档,所以这就是为什么我想问你们,我会保留这个问题:



可以我在数据集中设置标题(列标题),而不在数据集中输入数据?我猜不是这样,所以第二部分的问题是:

我可以使用一行数据集,其中使用空数据命名标题,然后再覆盖?



让我告诉你我正在尝试什么,但没有工作:

  dmsdb = dataset({'John','Name'},{'Amsterdam','City'},{10,'number'}); 
产生:
名称城市号码
John Amsterdam 10 - >标题不错

问题是,当我要为数据集添加更多的数据时,它期望所有字符串长度相同所以我使用cellstr():

  dmsdb(1,1:3)= dataset({cellstr('John' 'name'},{cellstr('Amsterdam'),'City'},{10,'number'}); 
生产:
Var1 Var2 Var3
'John''Amsterdam'10

我的标题在哪里?如何解决这个问题,这是什么原因?

解决方案

您可以设置一个空数据集,如

  data = dataset({[],'Name'},{[],'City'},{[],'number' }); 

  data = dataset([],[],[],'VarNames',{'Name','City','number'}); 

两者都会给你:

 >>数据

data =

[空0-by-3数据集]

但是我们可以看到列名通过检查

 >> get(data,'VarNames')

ans =

'名称''城市''''

现在我们可以向数据集添加行:

 >> ; data = [data;数据集({'John'},{'Amsterdam'},10,'VarNames',get(data,'VarNames')] 

data =

名称城市号
'约翰''阿姆斯特丹'10

你有基本的想法,但只是需要把你的字符串数据放在单元格中。这种替换您的第一行作品:

 >> dmsdb = dataset({{'John'},'Name'},{{'Amsterdam'},'City'},{10,'number'}); 

dmsdb =

名称城市号码
'John''Amsterdam'10

dataset()的内置帮助实际上很好地阐述了构建数据集的这些和其他方法的细节。另请参阅在线文档,其示例如下:



http://www.mathworks.com/help/toolbox/stats/dataset.html



其中一个Mathworks博客一个不错的帖子:



http://blogs.mathworks.com/loren/2009/05/20/from-struct-to-dataset/



祝你好运!


I can't find any good documentation about dataset(), so that's why I want to ask you guys, I'll keep the question short:

Can I set headers (column titles) in a dataset, without entering data into the dataset yet? I guess not, so the 2nd part of the question would be:
Can I make a one-row dataset, in which I name the headers, with empty data, and overwrite it later?

Let me show you what I was trying, but did not work:

dmsdb = dataset({ 'John','Name'},{'Amsterdam','City'},{10,'number' });  
produces:  
    Name    City         number  
    John    Amsterdam    10 --> Headers are good!  

Problem is, that when I am going to add more data to the dataset, it expects all strings to be of the same length. So I use cellstr():

dmsdb(1,1:3) = dataset({ cellstr('John'),'Name'},{cellstr('Amsterdam'),'City'},{10,'number' });  
Produces:  
    Var1          Var2               Var3  
    'John'        'Amsterdam'        10  

Where did my headers go? How do I solve this issue, and what is causing this?

解决方案

You can set up an empty dataset like either

data = dataset({[], 'Name'}, {[], 'City'}, {[], 'number'});

or

data = dataset([], [], [], 'VarNames', {'Name', 'City', 'number'});

Both will give you:

>> data

data = 

[empty 0-by-3 dataset]

But we can see that the column names are set by checking

>> get(data, 'VarNames')                                             

ans = 

    'Name'    'City'    'number'

Now we can add rows to the dataset:

>> data = [data; dataset({'John'}, {'Amsterdam'}, 10, 'VarNames', get(data, 'VarNames'))]

data = 

    Name          City               number
    'John'        'Amsterdam'        10    

You had the basic idea, but just needed to put your string data in cells. This replacement for your first line works:

>> dmsdb = dataset({ {'John'},'Name'},{{'Amsterdam'},'City'},{10,'number' }); 

dmsdb = 

    Name          City               number
    'John'        'Amsterdam'        10    

The built-in help for dataset() is actually really good at laying out the details of these and other ways of constructing datasets. Also check out the online documentation with examples at:

http://www.mathworks.com/help/toolbox/stats/dataset.html

One of the Mathworks blogs has a nice post too:

http://blogs.mathworks.com/loren/2009/05/20/from-struct-to-dataset/

Good luck!

这篇关于数据集中的标题(Matlab)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆