#### 6.1.4 Manipulating Structures

Other functions that can manipulate the fields of a structure are given below.

— Built-in Function: nfields (s)

> Return the number of fields of the structure s.

— Built-in Function: fieldnames (struct)

> Return a cell array of strings naming the elements of the structure struct. It is an error to call `fieldnames` with an argument that is not a structure.

— Built-in Function: isfield (x, name)

> Return true if the x is a structure and it includes an element named name. If name is a cell array of strings then a logical array of equal dimension is returned.

— Function File: [v1, ...] = getfield (s, key, ...)

> Extract a field from a structure (or a nested structure). For example:

>               ss(1,2).fd(3).b = 5;
>               getfield (ss, {1,2}, "fd", {3}, "b")
>                    ⇒ ans = 5

> Note that the function call in the previous example is equivalent to the expression

>               i1 = {1,2}; i2 = "fd"; i3 = {3}; i4= "b";
>               ss(i1{:}).(i2)(i3{:}).(i4)
>                    ⇒ ans = 5

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

— Function File: [k1, ..., v1] = setfield (s, k1, v1, ...)

> Set a field member in a (nested) structure array. For example:

>               oo(1,1).f0 = 1;
>               oo = setfield (oo, {1,2}, "fd", {3}, "b", 6);
>               oo(1,2).fd(3).b == 6
>                    ⇒ ans = 1

> Note that the same result as in the above example could be achieved by:

>               i1 = {1,2}; i2 = "fd"; i3 = {3}; i4 = "b";
>               oo(i1{:}).(i2)(i3{:}).(i4) == 6
>                    ⇒ ans = 1

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

— Built-in Function: rmfield (s, f)

> Return a copy of the structure (array) s with the field f removed. If f is a cell array of strings or a character array, remove the named fields.

> See also: cellstr, iscellstr, setfield.

— Function File: [t, p] = orderfields (s1)
— Function File: [t, p] = orderfields (s1, s2)

> Return a copy of s1 with fields arranged alphabetically or as specified by s2.

> Given one struct, arrange field names in s1 alphabetically.

> If the second argument is a struct, arrange field names in s1 as they appear in s2. The second argument may also specify the order in a permutation vector or a cell array of strings containing the fieldnames of s1 in the desired order.

> The optional second output argument p is assigned the permutation vector which converts the original name order into 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: getfield, rmfield, isfield, isstruct, fieldnames, struct.

— Function File: substruct (type, subs, ...)

> Create a subscript structure for use with `subsref` or `subsasgn`. For example:

>               idx = substruct("()", {3, ":"})
>                    ⇒
>                      idx =
>                      {
>                        type = ()
>                        subs =
>                        {
>                          [1,1] =  3
>                          [1,2] = :
>                        }
>                      }
>               x = [1, 2, 3; 4, 5, 6; 7, 8, 9];
>               subsref(x, idx)
>                    ⇒ ans =
>                       7  8  9

> See also: subsref, subsasgn.