Matlab-按属性对对象的单元格数组进行排序 [英] Matlab - sort cell array of objects by property

查看:88
本文介绍了Matlab-按属性对对象的单元格数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个名为Foo的类,具有一个名为DateTime的datenum属性.如果我有一个Foo对象的单元格数组集合,如何根据每个对象的DateTime属性对其进行排序?

Suppose I had a class named Foo, with a datenum property named DateTime. If I had a cell array collection of Foo objects, how would I sort that according to each object's DateTime property?

我已经看到了有关重载sort方法和使用对象数组的参考,但是由于动态调整大小,我使用了单元格数组,并且这些指令没有成立.有人有建议吗?干杯

I have seen references to overloading the sort method and working with arrays of objects, however I'm using a cell array due to dynamic sizing and those instructions aren't holding up. Anybody got some suggestions? Cheers

推荐答案

最简单的方法是将时间值提取到向量中,对其进行排序,然后使用新的顺序对原始数组进行排序.

The simplest approach is to extract the time-values into a vector, sort that, and use the new order to sort the original array.

%# extract DateTime from the cell array fooCell
dateTime = cellfun(@(x)x.DateTime, fooCell);

[~,sortIdx] = sort(dateTime);

%# reorder fooCell
fooCell = fooCell(sortIdx);

这篇关于Matlab-按属性对对象的单元格数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆