从结构中删除元素

时间:2014-03-05 17:48:08

标签: arrays matlab struct field

我有一个结构:

 p=struct('exponent',{1,2,2,4},'coeff',{2,5,6,7})

我正在尝试删除指数字段(2)和coeff字段(6)的第三个元素,以便它们这样的空白空间:

p=struct('exponent',{1,2,[],4},'coeff',{2,5,[],7})

但是变成了像这样的长度为3的结构

p=struct('exponent',{1,2,4},'coeff',{2,5,7})

我不确定该怎么做。

1 个答案:

答案 0 :(得分:0)

  1. 简单方法:

    p(3) = [];
    
  2. 另一种方法,允许更大的灵活性(例如,你可以删除第三个指数和第二个系数,如果有意义的话):

    e = [p.exponent]; %// convert to vector
    e = num2cell(e([1:2 4:end])); %// remove third and convert to cell array
    c = [p.coeff];
    c = num2cell(c([1:2 4:end])); %// or remove any other than the third here
    p = struct('exponent',e,'coeff',c) %// recreate p