How to sort a structure array based on a specific field
이전 댓글 표시
How do we sort a structure array based on a specific field?
채택된 답변
추가 답변 (1개)
Norbert Nitzsche
2019년 7월 25일
편집: Norbert Nitzsche
2019년 7월 25일
8 개 추천
% 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
Bruno Luong
2020년 8월 3일
편집: Bruno Luong
2020년 8월 3일
"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.
카테고리
도움말 센터 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!