Skip to content

orderfields

Reorder fields of a structure.

s = orderfields(s)
s = orderfields(s1, s2)
s = orderfields(s, order)

Returns s with its fields reordered. With one argument, sorts alphabetically. With a second order argument (a cell array of field names or a permutation vector), uses the specified order. With a struct s2 argument, matches s2’s field order.

s.b = 1; s.a = 2; s.c = 3;
orderfields(s) % fields now a, b, c
orderfields(s, {'c', 'a', 'b'})
  • fieldnames — List of field names in a structure.
  • struct — Create a structure with named fields.