How to sort a structure array based on a specific field
Afficher commentaires plus anciens
How do we sort a structure array based on a specific field?
Réponse acceptée
Plus de réponses (1)
Norbert Nitzsche
le 25 Juil 2019
Modifié(e) : Norbert Nitzsche
le 25 Juil 2019
8 votes
% suppose 's' is the struct array and you want to sort it by the values in field 'f_sortby'
[x,idx]=sort([s.f_sortby]);
s=s(idx);
1 commentaire
Bruno Luong
le 3 Août 2020
Modifié(e) : Bruno Luong
le 3 Août 2020
"Using curly braces should work for both numerical and text values"
Not for me
>> s=struct('num', {1 2 3})
s =
1×3 struct array with fields:
num
>> s(1)
ans =
struct with fields:
num: 1
>> s(3)
ans =
struct with fields:
num: 3
>> [x,idx]=sort({s.num})
Error using sort
Input argument must be a cell array of character vectors.
Catégories
En savoir plus sur Tables dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!