【问题标题】:How to sort a structure array如何对结构数组进行排序
【发布时间】:2015-03-14 23:09:46
【问题描述】:

如何按项目名称的字母顺序对oo结构数组进行排序。

oo = struct('Item', {'Quill','Ink Pen', 'Pencil'}, 'Cost', {10, 2, 1})

我尝试使用 sort() 函数,但它不起作用?
谢谢。

【问题讨论】:

    标签: arrays matlab structure cell-array


    【解决方案1】:

    首先索引您的字段,在本例中为oo.Items,它返回一个逗号分隔的列表。对于字符串数据,使用{} 连接到字符串单元格,否则使用[] 获取数组:

    %get the right order using second output of sort
    [~,index]=sort({oo.Item})
    %sort it
    oo=oo(index)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-12
      • 1970-01-01
      • 1970-01-01
      • 2014-09-06
      • 2021-04-07
      相关资源
      最近更新 更多