在MATLAB中删除结构中的字段

时间:2013-01-29 13:09:47

标签: matlab

我的结构Vinlu大小为1X36:

        Vinlu = 

        vessel: [1x36 struct]

每个都有5个字段:

  
    

Vinlu.vessel

  
        ans = 

        1x36 struct array with fields:
        pixels
        indexNOmask
        indexWHOLEvessel
        widths
        Meanwidth

有些船只字段的像素字段为空,例如:

  
    

Vinlu.vessel(1,4)

  
   ans = 

              pixels: [0x2 double]
         indexNOmask: [0x1 double]
    indexWHOLEvessel: [0x1 double]
              widths: [1x0 single]
           Meanwidth: NaN

我想从像素字段为空的结构中删除所有那些容器字段(与其他字段无关)。容器中的一个或多个字段可能是空的但我想删除那些具有空像素字段的字段,这样我将获得大小为1 X n的结构Vinlu,其中n <36

1 个答案:

答案 0 :(得分:7)

首先找到具有空vessel字段的pixels结构的索引:

idx = cellfun('isempty', {Vinlu.vessel.pixels});

然后只保留vessel中具有非空pixels字段的元素:

Vinlu.vessel = Vinlu.vessel(~idx);

在单行中看起来像这样:

Vinlu.vessel = Vinlu.vessel(~cellfun('isempty', {Vinlu.vessel.pixels}))
相关问题