1

让我们假设我们有下表mytable

                     Name                            id         Factor       Correct
    ___________________________________________    ________    _________    ____________

    'Maria'                                        '7324'      'x'           'Yes'      
    'Lina'                                         '7827'      'x'           'Yes'      
    'Jack'                                         '7831'      ''            'No'      
    'Van'                                          '7842a'     'x'           'No'      
    'Julia'                                        '7842c'     ''            'Yes'   

我想选择mytableFactor = 'x' AND Correct = 'Yes' 的所有行并将它们分配到一个新表中。对于每列的类(例如,'Name'列),我们有:

class(mytable.Name) = cell

我试过这段代码:

newtable = mytable((mytable.Correct == 'Yes') & (mytable.Factor == 'x'))

并得到错误:

Undefined operator '==' for input arguments of type 'cell'

我尝试了第二种方法:

newtable = mytable((cell2mat(mytable.Correct) == 'Yes') & (cell2mat(mytable.Factor) == 'x'))

这一次得到了错误:

Error using cat
Dimensions of matrices being concatenated are not consistent.

这是有道理的,因为与“否”相比,“是”的大小不同。我正在考虑用“Y”和“N”重新分配“正确”列。然而,这并不是我真正想要的。有什么建议吗?

4

1 回答 1

3

由于您的表格在每个单元格中都包含字符数组,因此您可以进行比较strcmp以提取所需的行:

newtable = mytable(strcmp(mytable.Correct, 'Yes') & strcmp(mytable.Factor, 'x'), :);

产生以下结果:

newtable = 

     Name        id      Factor    Correct
    _______    ______    ______    _______

    'Maria'    '7324'    'x'       'Yes'  
    'Lina'     '7827'    'x'       'Yes'  

请注意,我将比较的结果用作行索引,然后用作:列索引以选择所有列。您可以阅读此文档以获取有关访问表中数据的不同方式的更多信息。

于 2017-09-20T13:43:14.603 回答