How can I sort an array of structures based upon a particular field in MATLAB?
Show older comments
I have a structure array, and I would like to use a function like SORT to arrange those structures.
Accepted Answer
More Answers (1)
Observer
on 18 Jan 2017
function outStructArray = SortArrayofStruct( structArray, fieldName )
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
if ( ~isempty(structArray) && length (structArray) > 0)
[~,I] = sort(arrayfun (@(x) x.(fieldName), structArray)) ;
outStructArray = structArray(I) ;
else
disp ('Array of struct is empty');
end
end
Categories
Find more on Structures in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!