数据集中的标题 (Matlab)
我找不到关于 dataset() 的任何好的文档,所以这就是为什么我想问你们,我会简短地回答这个问题:
我可以在数据集中设置标题(列标题),而不需要将数据输入到数据集中?我想不是,所以问题的第二部分是:
我可以制作一个单行数据集,在其中用空数据命名标题,然后再覆盖它吗?
让我向您展示我正在尝试但没有成功的方法:
dmsdb = dataset({ 'John','Name'},{'Amsterdam','City'},{10,'number' });
produces:
Name City number
John Amsterdam 10 --> Headers are good!
问题是,当我要向数据集中添加更多数据时,它期望所有字符串具有相同的长度。所以我使用 cellstr():
dmsdb(1,1:3) = dataset({ cellstr('John'),'Name'},{cellstr('Amsterdam'),'City'},{10,'number' });
Produces:
Var1 Var2 Var3
'John' 'Amsterdam' 10
我的标题去了哪里?我该如何解决这个问题,是什么原因造成的?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我同意,数据集的帮助很难理解,主要是因为创建数据集的方法有很多,而且大多数方法都涉及很多元胞数组。这是我最喜欢的两种方法:
I agree, the help for dataset is hard to understand, mainly because there are so many ways to create a dataset and most methods involve a lot of cell arrays. Here are my two favorite ways to do it:
您可以设置一个空数据集,如任一
或
两者都会给您:
但我们可以看到列名是通过检查设置的
现在我们可以向数据集添加行:
您有基本的想法,但只需要放置字符串数据在细胞中。第一行的这种替换是有效的:
dataset()
的内置帮助实际上非常擅长列出这些以及其他构建数据集的方法的详细信息。另请查看带有示例的在线文档:http://www.mathworks.com /help/toolbox/stats/dataset.html
Mathworks 博客之一也有一篇不错的文章:
http://blogs.mathworks.com/loren/2009/ 05/20/from-struct-to-dataset/
祝你好运!
You can set up an empty dataset like either
or
Both will give you:
But we can see that the column names are set by checking
Now we can add rows to the dataset:
You had the basic idea, but just needed to put your string data in cells. This replacement for your first line works:
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!
下面是一个示例:
最后的数据集如下所示:
注意:如果要使用串联而不是索引,则必须指定变量名称:
Here is an example:
The dataset at the end looks like:
Note: if you want to use concatenation instead of indexing, you have to specify the variable names: