Structures
Structures are a data type similar to cells. However, where cells use numeric indices to refer to specific pieces of data, structures use strings.
These strings refer to entities called 'fields'. Each field has its own value which can be of any datatype. Fields must be valid identifiers.
For example, to track data about students, the following structure could be
used:
student.name = 'Harry'
student.age = 22
student.id = 9028390911
Structures can be combined to make an array of structures, as to make a database of
information. One way of doing this is to index to the end of the structure array and add the
new information to that index of the
array.
student(end+1).name ='Nicole'
student(end).age = 21
student(end).id = 90294067
Another way to make a structure array is to combine multiple structures together in an
array. However, there must be the same number of fields for this to work.
stArray = [struct1 struct2]
A field inside a structure may contain another structure. The fields inside the child
structure are accessed in the same manner as the parent, but they are placed after the
parent.
inner_struct.value = 5;
outer_struct.data = inner_struct;
outer_struct.data.value => 5
Structure fields names can be dynamically set from a variable. For example, continuing the
student
structure example above, this can be
done:fieldnameA = 'score';
student(1).(fieldnameA) = [56];
student(2).(fieldnameA) = [89];
fieldnameB = 'status';
student(1).(fieldnameB) = ['pending'];
student(2).(fieldnameB) = ['registered'];
This will produce this
structure:
> student(2)
ans = struct [
age: 21
id: 90294067
name: Nicole
score: 89
status: registered
]
And the fields can be accessed as
follows:
> student.score
ans = 56
ans = 89
> student.(fieldnameA)
ans = 56
ans = 89
>