Main Content

orderfields

Order fields of structure array

Description

example

S = orderfields(S1) orders the fields in S1 by name.

Since field names can contain only letters, digits, and underscores, this syntax sorts field names in ASCII order. All uppercase letters come before all lowercase letters.

example

S = orderfields(S1,S2) returns a copy of S1 with its fields reordered to match the order of the fields of S2. The input structure arrays S1 and S2 must have the same field names.

example

S = orderfields(S1,C) matches the order of the names specified in the input array C. The name of every field in S1 must appear once in C.

example

S = orderfields(S1,P) matches the order specified by the permutation vector P.

If S1 has n fields, then the elements of P are the integers from 1 through n, arranged in any order. For example, if S1 has three fields and P is [3 1 2], then the third field of S1 is the first field of the output S. This syntax is useful for ordering multiple structure arrays in the same way.

example

[S,Pout] = orderfields(___) also returns a permutation vector. The elements of Pout are the integers from 1 through n, arranged in an order that represents the change in order of the fields. You can use this syntax with any of the input arguments of the previous syntaxes.

Examples

collapse all

Create a structure with several fields.

S1 = struct('b',1,'B',2,'a',3,'A',4)
S1 = struct with fields:
    b: 1
    B: 2
    a: 3
    A: 4

Order the fields. This syntax orders the fields by their names, in ASCII order.

S = orderfields(S1)
S = struct with fields:
    A: 4
    B: 2
    a: 3
    b: 1

Create two structures that have the same fields, in different orders. The field names are the same, but the field values are different.

S1 = struct('b',1,'B',2,'a',3,'A',4)
S1 = struct with fields:
    b: 1
    B: 2
    a: 3
    A: 4

S2 = struct('a',0,'b',20,'B',10,'A',0)
S2 = struct with fields:
    a: 0
    b: 20
    B: 10
    A: 0

Order the fields in S1 to match the order of fields in S2.

S = orderfields(S1,S2)
S = struct with fields:
    a: 3
    b: 1
    B: 2
    A: 4

Create a structure.

data.x = linspace(0,2*pi);
data.y = sin(data.x);
data.title = 'y = sin(x)'
data = struct with fields:
        x: [0 0.0635 0.1269 0.1904 0.2539 0.3173 0.3808 0.4443 0.5077 0.5712 0.6347 0.6981 0.7616 0.8251 0.8885 0.9520 1.0155 1.0789 1.1424 1.2059 1.2693 1.3328 1.3963 1.4597 1.5232 1.5867 1.6501 1.7136 1.7771 1.8405 1.9040 1.9675 ... ] (1x100 double)
        y: [0 0.0634 0.1266 0.1893 0.2511 0.3120 0.3717 0.4298 0.4862 0.5406 0.5929 0.6428 0.6901 0.7346 0.7761 0.8146 0.8497 0.8815 0.9096 0.9341 0.9549 0.9718 0.9848 0.9938 0.9989 0.9999 0.9969 0.9898 0.9788 0.9638 0.9450 0.9224 ... ] (1x100 double)
    title: 'y = sin(x)'

Order the fields by listing their names in a cell array.

C = {'title','x','y'};
data = orderfields(data,C)
data = struct with fields:
    title: 'y = sin(x)'
        x: [0 0.0635 0.1269 0.1904 0.2539 0.3173 0.3808 0.4443 0.5077 0.5712 0.6347 0.6981 0.7616 0.8251 0.8885 0.9520 1.0155 1.0789 1.1424 1.2059 1.2693 1.3328 1.3963 1.4597 1.5232 1.5867 1.6501 1.7136 1.7771 1.8405 1.9040 1.9675 ... ] (1x100 double)
        y: [0 0.0634 0.1266 0.1893 0.2511 0.3120 0.3717 0.4298 0.4862 0.5406 0.5929 0.6428 0.6901 0.7346 0.7761 0.8146 0.8497 0.8815 0.9096 0.9341 0.9549 0.9718 0.9848 0.9938 0.9989 0.9999 0.9969 0.9898 0.9788 0.9638 0.9450 0.9224 ... ] (1x100 double)

Create a structure.

data.x = linspace(0,2*pi);
data.y = sin(data.x);
data.title = 'y = sin(x)'
data = struct with fields:
        x: [0 0.0635 0.1269 0.1904 0.2539 0.3173 0.3808 0.4443 0.5077 0.5712 0.6347 0.6981 0.7616 0.8251 0.8885 0.9520 1.0155 1.0789 1.1424 1.2059 1.2693 1.3328 1.3963 1.4597 1.5232 1.5867 1.6501 1.7136 1.7771 1.8405 1.9040 1.9675 ... ] (1x100 double)
        y: [0 0.0634 0.1266 0.1893 0.2511 0.3120 0.3717 0.4298 0.4862 0.5406 0.5929 0.6428 0.6901 0.7346 0.7761 0.8146 0.8497 0.8815 0.9096 0.9341 0.9549 0.9718 0.9848 0.9938 0.9989 0.9999 0.9969 0.9898 0.9788 0.9638 0.9450 0.9224 ... ] (1x100 double)
    title: 'y = sin(x)'

Order the fields by listing their original positions in a different order. For example, move the third field so that it is the first field of the output structure.

P = [3 1 2];
data = orderfields(data,P)
data = struct with fields:
    title: 'y = sin(x)'
        x: [0 0.0635 0.1269 0.1904 0.2539 0.3173 0.3808 0.4443 0.5077 0.5712 0.6347 0.6981 0.7616 0.8251 0.8885 0.9520 1.0155 1.0789 1.1424 1.2059 1.2693 1.3328 1.3963 1.4597 1.5232 1.5867 1.6501 1.7136 1.7771 1.8405 1.9040 1.9675 ... ] (1x100 double)
        y: [0 0.0634 0.1266 0.1893 0.2511 0.3120 0.3717 0.4298 0.4862 0.5406 0.5929 0.6428 0.6901 0.7346 0.7761 0.8146 0.8497 0.8815 0.9096 0.9341 0.9549 0.9718 0.9848 0.9938 0.9989 0.9999 0.9969 0.9898 0.9788 0.9638 0.9450 0.9224 ... ] (1x100 double)

Create a structure.

data1.x = linspace(0,2*pi);
data1.y = sin(data1.x);
data1.title = 'y = sin(x)';

Reorder the structure using the orderfields function. Store the new field order in a permutation vector Pout.

[S,Pout] = orderfields(data1,{'title','x','y'})
S = struct with fields:
    title: 'y = sin(x)'
        x: [0 0.0635 0.1269 0.1904 0.2539 0.3173 0.3808 0.4443 0.5077 0.5712 0.6347 0.6981 0.7616 0.8251 0.8885 0.9520 1.0155 1.0789 1.1424 1.2059 1.2693 1.3328 1.3963 1.4597 1.5232 1.5867 1.6501 1.7136 1.7771 1.8405 1.9040 1.9675 ... ] (1x100 double)
        y: [0 0.0634 0.1266 0.1893 0.2511 0.3120 0.3717 0.4298 0.4862 0.5406 0.5929 0.6428 0.6901 0.7346 0.7761 0.8146 0.8497 0.8815 0.9096 0.9341 0.9549 0.9718 0.9848 0.9938 0.9989 0.9999 0.9969 0.9898 0.9788 0.9638 0.9450 0.9224 ... ] (1x100 double)

Pout = 3×1

     3
     1
     2

Create a second structure with the same fields.

data2.x = data1.x;
data2.y = cos(data2.x);
data2.title = 'y = cos(x)';

Reorder the fields of data2 using Pout. If you have many structures with the same field names, then you can use Pout to reorder them all in the same way.

S2 = orderfields(data2,Pout)
S2 = struct with fields:
    title: 'y = cos(x)'
        x: [0 0.0635 0.1269 0.1904 0.2539 0.3173 0.3808 0.4443 0.5077 0.5712 0.6347 0.6981 0.7616 0.8251 0.8885 0.9520 1.0155 1.0789 1.1424 1.2059 1.2693 1.3328 1.3963 1.4597 1.5232 1.5867 1.6501 1.7136 1.7771 1.8405 1.9040 1.9675 ... ] (1x100 double)
        y: [1 0.9980 0.9920 0.9819 0.9679 0.9501 0.9284 0.9029 0.8738 0.8413 0.8053 0.7660 0.7237 0.6785 0.6306 0.5801 0.5272 0.4723 0.4154 0.3569 0.2969 0.2358 0.1736 0.1108 0.0476 -0.0159 -0.0792 -0.1423 -0.2048 -0.2665 -0.3271 ... ] (1x100 double)

Input Arguments

collapse all

Input structure, specified as a structure array.

Field order by structure, specified as a structure array. S2 has the same fields as S1 but specifies them in a different order.

Field order by name, specified as a cell array of character vectors or a string array. The names in C must match the field names of S1.

Field order by number, specified as a numeric vector. The numbers must be the integers from 1 through n, where n is the number of fields of S1.

Output Arguments

collapse all

Reordered structure, returned as a structure array. S has the same fields as S1 but they might be in a different order.

Output field order, returned as a numeric vector. The elements of Pout are the integers from 1 through n, where n is the number of fields of S1. The permutation of the integers represents the change in the order of the fields.

Tips

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

Extended Capabilities

Version History

Introduced before R2006a