数据集中的标题 (Matlab)

发布于 2024-12-11 04:36:25 字数 704 浏览 0 评论 0原文

我找不到关于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花开半夏魅人心 2024-12-18 04:36:26

我同意,数据集的帮助很难理解,主要是因为创建数据集的方法有很多,而且大多数方法都涉及很多元胞数组。这是我最喜欢的两种方法:

% 1) Create the 3 variables of interest, then make the dataset.  
% Make sure they are column vectors!
>> Name = {'John' 'Joe'}';  City = {'Amsterdam' 'NYC'}'; number = [10 1]';
>> dataset(Name, City, number)

ans = 

    Name          City               number
    'John'        'Amsterdam'        10    
    'Joe'         'NYC'               1    

% 2) More compact than doing 3 separate cell arrays
>> dataset({{'John' 'Amsterdam' 10} 'Name' 'City' 'number'})

ans = 

    Name          City               number  
    'John'        'Amsterdam'        [10]    

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:

% 1) Create the 3 variables of interest, then make the dataset.  
% Make sure they are column vectors!
>> Name = {'John' 'Joe'}';  City = {'Amsterdam' 'NYC'}'; number = [10 1]';
>> dataset(Name, City, number)

ans = 

    Name          City               number
    'John'        'Amsterdam'        10    
    'Joe'         'NYC'               1    

% 2) More compact than doing 3 separate cell arrays
>> dataset({{'John' 'Amsterdam' 10} 'Name' 'City' 'number'})

ans = 

    Name          City               number  
    'John'        'Amsterdam'        [10]    
夏末染殇 2024-12-18 04:36:25

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

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

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

两者都会给您:

>> data

data = 

[empty 0-by-3 dataset]

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

>> get(data, 'VarNames')                                             

ans = 

    'Name'    'City'    'number'

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

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

data = 

    Name          City               number
    'John'        'Amsterdam'        10    

您有基本的想法,但只需要放置字符串数据在细胞中。第一行的这种替换是有效的:

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

dmsdb = 

    Name          City               number
    '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/

祝你好运!

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!

短叹 2024-12-18 04:36:25

下面是一个示例:

%# create dataset with no rows
ds = dataset(cell(0,1),cell(0,1),zeros(0,1));
ds.Properties.VarNames = {'Name', 'City', 'number'};

%# adding one row at a time
for i=1:3
    row = {{'John'}, {'Amsterdam'}, 10};  %# construct new row each iteration
    ds(i,:) = dataset(row{:});
end

%# adding a batch of rows all at once
rows = {{'Bob';'Alice'}, {'Paris';'Boston'}, [20;30]};
ds(4:5,:) = dataset(rows{:});

最后的数据集如下所示:

>> ds
ds = 
    Name           City               number
    'John'         'Amsterdam'        10    
    'John'         'Amsterdam'        10    
    'John'         'Amsterdam'        10    
    'Bob'          'Paris'            20    
    'Alice'        'Boston'           30    

注意:如果要使用串联而不是索引,则必须指定变量名称:

vars = {'Name', 'City', 'number'};
ds = [ds ; dataset(rows{:}, 'VarNames',vars)]

Here is an example:

%# create dataset with no rows
ds = dataset(cell(0,1),cell(0,1),zeros(0,1));
ds.Properties.VarNames = {'Name', 'City', 'number'};

%# adding one row at a time
for i=1:3
    row = {{'John'}, {'Amsterdam'}, 10};  %# construct new row each iteration
    ds(i,:) = dataset(row{:});
end

%# adding a batch of rows all at once
rows = {{'Bob';'Alice'}, {'Paris';'Boston'}, [20;30]};
ds(4:5,:) = dataset(rows{:});

The dataset at the end looks like:

>> ds
ds = 
    Name           City               number
    'John'         'Amsterdam'        10    
    'John'         'Amsterdam'        10    
    'John'         'Amsterdam'        10    
    'Bob'          'Paris'            20    
    'Alice'        'Boston'           30    

Note: if you want to use concatenation instead of indexing, you have to specify the variable names:

vars = {'Name', 'City', 'number'};
ds = [ds ; dataset(rows{:}, 'VarNames',vars)]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文