How to sort a struct

180 次查看(过去 30 天)
Peter Meier
Peter Meier 2020-7-23
评论: Stephen23 2020-7-23
Is it possible to sort a struct? I want to sort the third column.
0 should be in the first row and the highest value at the end (last row). The other values are not important and should be the same ranking.
Thank you.
  2 个评论
KSSV
KSSV 2020-7-23
Yes, you can.....read about sort. It depends how your structure is.
Peter Meier
Peter Meier 2020-7-23
T = struct2table( parameter.list); % convert the struct array to a table
sortedT = sortrows(T, 'value'); % sort the table by value
sortedS = table2struct(sortedT) % change it back to str
%it works but i just want to sort the lowest and highest value (rows)

请先登录,再进行评论。

采纳的回答

Mohammad Sami
Mohammad Sami 2020-7-23
编辑:Mohammad Sami 2020-7-23
You can try like this.
% a = somestruct;
[~,index] = sortrows([a.Var3].');
a = a(index);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by