How to sort structure arrays in MATLAB?

zenab picture zenab · Sep 30, 2009 · Viewed 25.8k times · Source

I'm working with an image retrieval system using color histogram intersection in MATLAB. This method gives me the following data: a real number which represents the histogram intersection distance, and the image file name. Because they are different data types, I store them in structure array with two fields, and then I save this structure in a .mat file. Now I need to sort this structure according to the histogram intersection distance in descending order in order to retrieve the image with the highest histogram intersection distance. I've tried many methods to sort this data but without result. Please can you help me solve this problem?

Answer

kyokley picture kyokley · Sep 30, 2009

It's also possible to sort the entire structure.

To build off of gnovice's example...

% Create a structure array
s = struct('value',{1 7 4},'file',{'img1.jpg' 'img2.jpg' 'img3.jpg'});

% Sort the structure according to values in descending order
% We are only interested in the second output from the sort command

[blah, order] = sort([s(:).value],'descend');

% Save the sorted output

sortedStruct = s(order);