从结构

时间:2015-11-18 14:56:52

标签: matlab matrix structure rows

我有一个1x1结构(EEG),有42个字段。其中一个字段称为事件,是一个1x180结构,有13个不同的字段,其中一些是字符串和一些数值。

EEG.event的第4个字段是 type ,它包含字符串(即'preo','pred','to','td','po','pd')。

我想只保留EEG.event.type列中包含'preo'的结构行。

我的最终目标是创建一个矩阵,其中包含来自结构EEG.event的所有列,以及只有EEG.event.type中带有'preo'的行,以及来自其他变量的其他列。

到目前为止,我试过了:

S = struct2table(EEG.event);

并正确返回180x13表。 但是,我无法仅选择类型中带有'preo'的行。我试过了:

A= S(S.type=='preo', :);

它给了我一个错误:

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

我也尝试过:

array(strcmp(S(:, 4), 'preo'), :) = [];

它给了我这个错误:

Deletion requires an existing variable.

然后我想也许我应该把表转换成矩阵,直接从矩阵中删除行。我试过了:

B = cell2mat(S);

但它会返回此错误:

Error using cell2mat (line 42)
You cannot subscript a table using only one subscript. Table subscripting requires both row and variable subscripts.

欢迎任何建议或提示,因为我不知道如何继续。

我有的示例列表(这里只有18行):

13  1   201011  'preo'  2502    201 1   1   'y' 'h' 13  13.9230000000000    13
14  1   201011  'pred'  2684    201 1   1   'y' 'h' 14  14.1049999960000    14
15  1   201012  'to'    2707    201 1   2   'y' 'h' 15  14.1280000000000    15
16  1   201012  'td'    2993    201 1   2   'y' 'h' 16  14.4140000000000    16
17  1   201013  'po'    3019    201 1   3   'y' 'h' 17  14.4400000000000    17
18  1   201013  'pd'    3383    201 1   3   'y' 'h' 18  14.8040000000000    18
55  2   61011   'preo'  8213    61  1   1   'y' 'h' 55  53.9000000000000    55
56  2   61011   'pred'  8522    61  1   1   'y' 'h' 56  54.2089999850000    56
57  2   61012   'to'    8547    61  1   2   'y' 'h' 57  54.2340000000000    57
58  2   61012   'td'    8834    61  1   2   'y' 'h' 58  54.5210000000000    58
59  2   61013   'po'    8858    61  1   3   'y' 'h' 59  54.5450000000000    59
60  2   61013   'pd'    9091    61  1   3   'y' 'h' 60  54.7780000000000    60
85  3   124011  'preo'  13924   124 1   1   'y' 'h' 85  82.4550000000000    85
86  3   124011  'pred'  14159   124 1   1   'y' 'h' 86  82.6899999990000    86
87  3   124012  'to'    14181   124 1   2   'y' 'h' 87  82.7120000000000    87
88  3   124012  'td'    14448   124 1   2   'y' 'h' 88  82.9790000000000    88
89  3   124013  'po'    14470   124 1   3   'y' 'h' 89  83.0010000000000    89
90  3   124013  'pd'    14713   124 1   3   'y' 'h' 90  83.2440000000000    90

我想要的示例列表(来自上面的18行):

13  1   201011  'preo'  2502    201 1   1   'y' 'h' 13  13.9230000000000    13
55  2   61011   'preo'  8213    61  1   1   'y' 'h' 55  53.9000000000000    55
85  3   124011  'preo'  13924   124 1   1   'y' 'h' 85  82.4550000000000    85

1 个答案:

答案 0 :(得分:1)

我找到了一个解决方案,我在这里发布了同样问题的其他人。

我首先创建一个单元格数组,然后从单元格数组中删除行。目前是我能想到的最好的。

myCell= struct2cell(EEG.event);
%it results in a 3d cell array, with the fields as first dimension (42) x a singleton dimension as second dimension x the number of the rows as third dimension (180)
new_Cell = permute(myCell,[3,1,2]);
%it deletes the singleton dimension and swap the other 2 dimensions, obtaining 180x42.
[r,c] = find(strcmp(new_Cell,'preo'));
%indices as rows (r) and columns (c) of cells with the string 'preo'
y = new_Cell([r],:);
%It keeps only the rows that you want from the original cell array 'myCell'.