File : Details

# orderfields()

reorders the primary fields of a structure or an array of structures
Details
Version
1.0
Author
Samuel Gougeon
Maintainer
Samuel Gougeon
Category
Supported Scilab Version
5.5
Creation Date
May 29, 2015
Description
```            -->orderfields

function [So, k] = orderfields(S, pattern)

Reorders primary fields of a (array of) structure according to a pattern

CALLING SEQUENCES
orderfields         // displays this help
orderfields demo    // runs a demo in the console
[So, [k]] = orderfields(S)
[So, [k]] = orderfields(S, pattern)

PARAMETERS
S : structure or array of structures whose primary fields must be re-ordered.
pattern: another structure with same fields as S (in the desired order),
or vector of fields names of S, in the desired order,
or cell of fields names of S, in the desired order,
or vector of ranks in S of fields names of the reordered structure.
So : re-ordered structure. The re-ordering is not applied to sub-structures.
k  : row vector providing the ranks in S of So fields names: the field #i
in So in met as field #k(i) in S.

DESCRIPTION
So = orderfields(S) reorders the primary fields of S in alphabetical order,
and returns the corresponding structure So copied from S.
So = orderfields(S, pattern) reorders the primary fields of S according to
the order
- of the fields of pattern, if pattern is a structure similar to S.
- of the fields names listed in pattern, if pattern is a vector of strings
or a cell of strings: these strings must be the fields names of S, in
the desired order)
- specified by their ranks in S, pattern being a vector of ranks
= any permutation of 1 : size(fieldnames(S),"*").

EXAMPLES
a = struct('txt','hello', 'real',%pi, 'bool',%f, 'pol',%z)
b = struct('real',%e, 'txt','bonjour', 'pol',(%s-1)^2, 'bool',%t)
[r, i] = orderfields(a, b)
a
[r, i] = orderfields(a, ['bool' 'real' 'pol' 'txt'])
a
c = makecell([1,4], 'bool', 'pol', 'txt', 'real')
[r, i] = orderfields(a, c)
a
[r, i] = orderfields(a, [4 3 2 1])

RESULTS
-------
-->   a = struct('txt','hello', 'real',%pi, 'bool',%f, 'pol',%z)
a  =
txt: "hello"
real: 3.1415927
bool: %f
pol: z

-->   b = struct('real',%e, 'txt','bonjour', 'pol',(%s-1)^2, 'bool',%t)
b  =
real: 2.7182818
txt: "bonjour"
pol: 1-2*s+s^2
bool: %t

-->   [r, i] = orderfields(a, b)
i  =
2.    1.    4.    3.
r  =
real: 3.1415927
txt: "hello"
pol: z
bool: %f

-->   a
a  =
txt: "hello"
real: 3.1415927
bool: %f
pol: z

-->   [r, i] = orderfields(a, ['bool' 'real' 'pol' 'txt'])
i  =
3.    2.    4.    1.
r  =
bool: %f
real: 3.1415927
pol: z
txt: "hello"

-->   a
a  =
txt: "hello"
real: 3.1415927
bool: %f
pol: z

-->   c = makecell([1,4], 'bool', 'pol', 'txt', 'real')
c  =
!"bool"  "pol"  "txt"  "real"  !

-->   [r, i] = orderfields(a, c)
i  =
3.    4.    1.    2.
r  =
bool: %f
pol: z
txt: "hello"
real: 3.1415927

-->   a
a  =
txt: "hello"
real: 3.1415927
bool: %f
pol: z

-->   [r, i] = orderfields(a, [4 3 2 1])
i  =
4.    3.    2.    1.
r  =
pol: z
bool: %f
real: 3.1415927
txt: "hello"

```
Files (1)
[6.43 kB]
Miscellaneous file
```Macro defining the function orderfields()

* MAKE IT AVAILABLE in EVERY SESSION: Click there to see indications

* Get help: Enter "orderfields" without parameters

```
News (0)