0

I have the 1x1008 struct array EEG.event with fields

latency
duration
channel
bvtime
bvmknum
type
code
urevent

I want to delete all rows where entry in field EEG.event.type = 'boundary' or 'R 1'

I tried the following loop:

for b = 1:length(EEG.event)  

     if strcmp(EEG.event(b).type, 'boundary')
        EEG.event(b) = [];
     elseif strcmp(EEG.event(b).type, 'R  1')
        EEG.event(b) = [];
     end

end

This does not work of course, since the counting variable b at some point exceeds the length of EEG.event.

Does anyone have an idea how to delete particular rows?

Suever
  • 64,497
  • 14
  • 82
  • 101
C_Hut
  • 31
  • 8

3 Answers3

0

The fundamental issue that you are having is that you are modifying the same array of structs that you are trying to loop through. This is generally a bad idea and will lead to the issue that you are seeing.

The easiest way to do this is to actually convert the event.type fields of all structs to a cell array, and use strcmp on all of them simultaneously to construct a logical matrix that you can use to index into EEG.event to get the entries you care about.

You can put all of the type values in a cell array like this

types = {EEG.event.type};

Then create your logical array by looking for event types of 'boundary'

isBoundary = strcmp(types, 'boundary');

And get the subset of EEG.event entries like this.

boundaryEvents = EEG.event(isBoundary);

If you want a subset of your events where the type isn't 'boundary' or 'R 1', then you can get that subset this way.

isBoundary = strcmp(types, 'boundary');
isR1 = strcmp(types, 'R  1');

% Keep the entries that aren't boundary or R1 types
events_to_use = EEG.event(~(isBoundary | isR1));
Suever
  • 64,497
  • 14
  • 82
  • 101
0

Change your loop to iterate backward through the array, deleting elements toward the end first:

for b = length(EEG.event):-1:1
   ...
beaker
  • 16,331
  • 3
  • 32
  • 49
0

Thanks all for your answers!

This straight forward line of code does the job:

[ EEG.event( strcmp( 'boundary', { EEG.event.type } ) | strcmp( 'R  1', { EEG.event.type } ) ) ] = [];

Cheers!

C_Hut
  • 31
  • 8