MATLAB: Separate a table according to the separation of the rows in a string

岁酱吖の 提交于 2021-01-29 04:50:54

问题


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

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.

All help is appreciated. Thanks.


回答1:


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


来源:https://stackoverflow.com/questions/64290841/matlab-separate-a-table-according-to-the-separation-of-the-rows-in-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!