-
Notifications
You must be signed in to change notification settings - Fork 1
/
sort_structure.m
executable file
·49 lines (42 loc) · 1.46 KB
/
sort_structure.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
function structure_out = sort_structure(structure)
%structure_out = sort_structure(structure)
%This function sorts the entries in a
%structure in an ascending order.
%Structures within the structure are sorted
%seperately. They are put before the other
%fields. They themselves are also sorted.
%Input: -Structure, with or without substructures
%Output: -Structure, sorted
%Written on 12th july 2002 by Mark Jobse
%Declarations
structure_out = [];
struct_cell = [];
non_struct_cell = [];
%Get all fields out of the structure
cell = fieldnames(structure);
%Now split those fields into structures and other fields
counter = 1;
counter2 = 1;
for i = 1:length(cell)
boolean = isstruct(getfield(structure, cell{i}));
if (boolean)
struct_cell{counter} = cell{i};
counter = counter + 1;
else
non_struct_cell{counter2} = cell{i};
counter2 = counter2 + 1;
end
end
%Sort both cells
struct_cell = sort(struct_cell);
non_struct_cell = sort(non_struct_cell);
%Now sort all entries in the found structures (with this function)
for i = 1:length(struct_cell)
value = sort_structure(getfield(structure, struct_cell{i}));
structure_out = setfield(structure_out, struct_cell{i}, value);
end
%Now do the same for the non-structure fields.
for i = 1:length(non_struct_cell)
value = getfield(structure, non_struct_cell{i});
structure_out = setfield(structure_out,non_struct_cell{i}, value);
end