【问题标题】:Removing items from a structure array in matlab从matlab中的结构数组中删除项目
【发布时间】:2012-11-21 20:12:52
【问题描述】:

我在 matlab 中有一个非常大的结构数组。假设,为了论证,为了简化情况,我有类似的东西:

structure(1).name = 'a';
structure(2).name = 'b';
structure(3).name = 'c';
structure(1).returns = 1;
structure(2).returns = 2;
structure(3).returns = 3;

现在假设我遇到了一些情况,让我想删除 structure(2) 中的所有内容(我的结构数组中的所有条目)。有什么好的方法吗?

我的解决方案是将相应的字段设置为[](例如structure(1).name = [];),但这不会删除它们,只会使它们为空。我如何真正从结构数组中完全删除它们?有什么办法吗?

【问题讨论】:

    标签: matlab structure removeall


    【解决方案1】:

    如果您想删除索引i 处的元素,请执行以下操作:

    i = 3
    structure(i) = [];
    

    这将删除索引3处的元素。

    例子:

    st.name = 'text';
    st.id = 1524;
    arrayOfSt = [st st st st st];
    

    现在:

    arrayOfSt = 
    
        1x5 struct array with fields:
            name
            id
    

    如果我们执行:

    arrayOfSt(2) = [];
    

    那么结构体数组的新值将是:

    arrayOfSt = 
    
        1x4 struct array with fields:
            name
            id
    

    试试吧!

    【讨论】:

      猜你喜欢
      • 2021-09-07
      • 1970-01-01
      • 1970-01-01
      • 2017-12-06
      • 2020-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多