MATLAB: Is there a way to perform queries on object collections

arrayclassMATLAB

Hi, I know I can use arrayfun to call a method on each object in an array, but is there a way to perform queries across the array?
This is a made up example, but if I have a class
MATLAB code
classdef Employee
properties
salary = 0; %Set later
end
And I have an array of instances of this class
MATLAB code
staff = [employee1 employee2 employee3 ..... employeeN];
Is there an easy way I can write a query to get, say, the 3 Employees with the largest salarys?
Thanks for any help, just give me a shout if this isn't clear.
Tom.

Best Answer

Use comma separated list syntax
staff = [employee1 employee2 employee3 ..... employeeN];
salary = [staff.salary];
[~, i] = sort(salary, 2, 'descend');
top3 = staff(i(1:3))
Keep in mind that if you ever want to subclass your Employee class (into e.g. Secretary and Manager classes) you won't be able to store them all in the same array without converting them all into simple Employee objects.