根据字段条目删除struct array matlab中的整行

时间:2016-03-26 12:24:13

标签: arrays matlab struct row matlab-struct

我有1x1008结构数组EEG.event with fields

latency
duration
channel
bvtime
bvmknum
type
code
urevent

我想删除所有在EEG.event.type =' boundary'字段中输入的行。或者' R 1'

我尝试了以下循环:

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

这当然不起作用,因为计数变量b在某个时刻超过了EEG.event的长度。

有没有人知道如何删除特定行?

3 个答案:

答案 0 :(得分:0)

您遇到的根本问题是您正在修改您尝试循环的相同结构数组。这通常是一个坏主意,并将导致您所看到的问题。

最简单的方法是将所有结构的event.type字段实际转换为单元格数组,并同时在所有结构上使用strcmp来构造一个可用于索引到EEG.event以获取您关注的条目。

您可以将所有type值放在像这样的单元格数组中

types = {EEG.event.type};

然后通过查找'boundary'的事件类型来创建逻辑数组

isBoundary = strcmp(types, 'boundary');

获取此类EEG.event条目的子集。

boundaryEvents = EEG.event(isBoundary);

如果您想要一个类型不是“边界”或“R 1”的事件子集,那么您可以通过这种方式获得该子集。

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));

答案 1 :(得分:0)

更改循环以在数组中向后迭代,首先删除元素:

for b = length(EEG.event):-1:1
   ...

答案 2 :(得分:0)

谢谢大家的回答!

这段直接的代码完成了这项工作:

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

干杯!