matlabsortingmatlab-struct

Order fields in struct


I would like to push a field before another one in a struct. I coulnd't find any functions with Matlab help. Is there an easy and efficient way to implement this? Do I have to shift all the struct fields when I insert a new one 'before an other field'?

Example:

a=struct;
a.b='2';
a.c='3';
a.bb='2.5';
a %displays the struct fields and sequence

Solution

  • s = struct('b',2,'c',3,'a',1):
    snew = orderfields(s);
    

    Reference: https://mathworks.com/help/matlab/ref/orderfields.html