MATLAB Function Reference Previous page   Next Page
orderfields

Order fields of structure array

Syntax

Description

s = orderfields(s1) orders the fields in s1 so that the new structure array s has field names in ASCII dictionary order.

s = orderfields(s1, s2) orders the fields in s1 so that the new structure array s has field names in the same order as those in s2. Structures sl and s2 must have the same fields.

s = orderfields(s1, c) orders the fields in s1 so that the new structure array s has field names in the same order as those in the cell array of field name strings c. Structure s1 and cell array c must contain the same field names.

s = orderfields(s1, perm) orders the fields in s1 so that the new structure array s has fieldnames in the order specified by the indices in permutation vector perm.

If s1 has N fieldnames, the elements of perm must be an arrangement of the numbers from 1 to N. This is particularly useful if you have more than one structure array that you would like to reorder in the same way.

[s, perm] = orderfields(...) returns a permutation vector representing the change in order performed on the fields of the structure array that results in s.

Remarks

orderfields only orders top-level fields. It is not recursive.

Examples

Create a structure s. Then create a new structure from s, but with the fields ordered alphabetically:

Arrange the fields of s in the order specified by the second (cell array) argument of orderfields. Return the new structure in snew and the permutation vector used to create it in perm:

Now create a new structure, s2, having the same fieldnames as s. Reorder the fields using the permutation vector returned in the previous operation:

See Also

struct, fieldnames, setfield, getfield, isfield, rmfield, dynamic field names


Previous page  ordeig ordqz Next page

© 1994-2005 The MathWorks, Inc.