MATLAB:根据字符串中行的分隔来分离表 [英] MATLAB: Separate a table according to the separation of the rows in a string

查看:60
本文介绍了MATLAB:根据字符串中行的分隔来分离表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想分离一个表的库仑,使其与我想加载到表中的字符串的数字和数据相对应.否则,如果我有一个2x1的字符串,我希望有一个表,其中来自两个不同行的信息保持分开(但是对于nx1的字符串).

I would like to separate a table's coulombs to correspond with the number and data of the string I would like to load into the table. Otherwise said, if I have a 2x1 string, I would like to have a table in which the information from the two different rows remain separate (But for a nx1 string).

这是我的示例,并尝试这样做:

Here is my example and attempt of doing it:

String = ["Random info in middle one, "+ ...
        "Random info still continues. ",
        "Random info in middle two. "+ ...
        "Random info still continues. "];

    documents_Middle = tokenizedDocument(String);
    tdetails = tokenDetails(documents_Middle);
    
    for y=1:height(tdetails)
    Table_RandomInfoMiddle(y,:) = tdetails(y,{'Token','Type'});
    end

请注意2x1字符串,因此每一行都有其自己的唯一信息.现在,我的2x1字符串的所有信息都被扔到一个表中,如下所示:

Notice the 2x1 string, thus each row has its own unique information. Right now all the information of my 2x1 string is just thrown into one single table like this:

我希望得到的结果看起来像这样(将图像切成适合回答的想法):

The result I'm hoping for would look something like this(image cut to fit idea of answer):

主要思想是将这些信息分开.

The main idea is to get this information separate.

感谢所有帮助.谢谢.

推荐答案

您将不能拥有名称相同的不同列.我建议您执行以下操作.构造两个变量 variableNames variables ,它们包含标记和类型.然后使用它们创建表:

You won't be able to have different columns with the same name. I suggest you do something like the following. Construct two variables, variableNames and variables, which contain the tokens and the types. Then use those to create the table:

s = ["Random info in middle one, "+ ...
            "Random info still continues. ",
            "Random info in middle two. "+ ...
            "Random info still continues. "];
t = table();
d = tokenizedDocument(s);

variableNames = [];
variables = [];

for n=1:length(d)
 variableNames = [variableNames {sprintf('Tokens for sentence %d',n)} {sprintf('Type for sentence %d',n)}];
 variables = [variables {d(n).tokenDetails.Token} {d(n).tokenDetails.Type}];
end

table(variables{:},'VariableNames',variableNames)

ans =

  11×4 table

    Tokens for sentence 1    Type for sentence 1    Tokens for sentence 2    Type for sentence 2
    _____________________    ___________________    _____________________    ___________________

         "Random"                letters                 "Random"                letters        
         "info"                  letters                 "info"                  letters        
         "in"                    letters                 "in"                    letters        
         "middle"                letters                 "middle"                letters        
         "one"                   letters                 "two"                   letters        
         ","                     punctuation             "."                     punctuation    
         "Random"                letters                 "Random"                letters        
         "info"                  letters                 "info"                  letters        
         "still"                 letters                 "still"                 letters        
         "continues"             letters                 "continues"             letters        
         "."                     punctuation             "."                     punctuation

这篇关于MATLAB:根据字符串中行的分隔来分离表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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