How to sort a struct

140 vues (au cours des 30 derniers jours)
Peter Meier
Peter Meier le 23 Juil 2020
Commenté : Stephen23 le 23 Juil 2020
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 commentaires
KSSV
KSSV le 23 Juil 2020
Yes, you can.....read about sort. It depends how your structure is.
Peter Meier
Peter Meier le 23 Juil 2020
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)

Connectez-vous pour commenter.

Réponse acceptée

Mohammad Sami
Mohammad Sami le 23 Juil 2020
Modifié(e) : Mohammad Sami le 23 Juil 2020
You can try like this.
% a = somestruct;
[~,index] = sortrows([a.Var3].');
a = a(index);
  2 commentaires
Peter Meier
Peter Meier le 23 Juil 2020
Thank you so much
Stephen23
Stephen23 le 23 Juil 2020
[~,index] = sort([a.Var3]);

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by