1
votes

I have a 1x1 structure (EEG) with 42 fields. One of these fields is called event and is a 1x180 structure, with 13 different fields, some of which are strings and some numeric values.

The 4th field of EEG.event is type and it contains strings (i.e. 'preo', 'pred', 'to', 'td', 'po', 'pd').

I would like to keep only those rows of the structure that contain 'preo' in the column EEG.event.type.

My ultimate aim is to create a matrix with all the columns from the structure EEG.event and only the rows with 'preo' in EEG.event.type, plus other columns from other variables.

So far I tried:

S = struct2table(EEG.event);

and it correctly returns a 180x13 table. However I was not able to select only the rows with 'preo' in type. I tried:

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

and it gives me an error:

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

I also tried:

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

and it gives me this error:

Deletion requires an existing variable.

Then I thought that maybe I should have converted the table into matrix, to directly delete rows from the matrix. I tried:

B = cell2mat(S);

but it returns this error:

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

Any suggestion or tip is welcome, because I don't know how to continue.

Example list that I have (only 18 rows here):

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

Example list that I would like to have (from the 18 rows above):

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
dunno which list are you applying this filter, could you provide a concrete example. - Abr001am
@Agawa001 I just added a list of what I have, and what I would like to obtain. - dede
Did you read the MATLAB docs titled Create Table from Subset of Larger Table? It would be helpful if you could save your table to a .mat file, upload it somewhere, and post a link to it here... Otherwise I don't see how we can reproduce your problem.. - Dev-iL
@Dev-iL me too, i dont believe i can handle EEG structures maybe something in my matlab is missing - Abr001am
@dede if you want the result without gettin thru EEG.event.type, i can help with that - Abr001am

1 Answers

1
votes

I found a solution, I post it here for others with my same issue.

I first create a cell array, and then I delete rows from the cell array. At the moment is the best I can think of.

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'.