Navigation

Operators and Keywords

Function List:

C++ API

: sout = orderfields (s1)
: sout = orderfields (s1, s2)
: sout = orderfields (s1, {cellstr})
: sout = orderfields (s1, p)
: [sout, p] = orderfields (…)

Return a copy of s1 with fields arranged alphabetically, or as specified by the second input.

Given one input struct s1, arrange field names alphabetically.

If a second struct argument is given, arrange field names in s1 as they appear in s2. The second argument may also specify the order in a cell array of strings cellstr. The second argument may also be a permutation vector.

The optional second output argument p is the permutation vector which converts the original name order to the new name order.

Examples:

s = struct ("d", 4, "b", 2, "a", 1, "c", 3);
t1 = orderfields (s)
     ⇒ t1 =
        {
          a =  1
          b =  2
          c =  3
          d =  4
        }
t = struct ("d", {}, "c", {}, "b", {}, "a", {});
t2 = orderfields (s, t)
     ⇒ t2 =
        {
          d =  4
          c =  3
          b =  2
          a =  1
        }
t3 = orderfields (s, [3, 2, 4, 1])
     ⇒ t3 =
        {
          a =  1
          b =  2
          c =  3
          d =  4
        }
[t4, p] = orderfields (s, {"d", "c", "b", "a"})
     ⇒ t4 =
        {
          d =  4
          c =  3
          b =  2
          a =  1
        }
        p =
           1
           4
           2
           3

See also: fieldnames, getfield, setfield, rmfield, isfield, isstruct, struct.

Package: octave