A Structure is a named collection of data representing a single idea or "object". For anything in a computer more complicated than a list of numbers, structures can be used. Inside a structure are a list of fields each being a variable name for some sub-piece of data. Structures are similar to arrays in that they contain multiple data, but the main difference is, instead of an Index to each piece of data, we have a "name"; and instead of every piece of data being the same type, we can have a different type for each "field".
A Structure is one of the 5 data types in programming. A structure is used to represent information about something more complicated than a single number, character, or boolean can do (and more complicated than an array of the above data types can do). For example, a Student can be defined by his or her name, gpa, age, uid, etc. Each of these pieces of information should be labeled with an easily understood descriptive title, and then combined to form a whole (the structure).
Structures give us a way to "combine" multiple types of information under a single variable. The nice thing is that structures allow us to use Human Readable descriptions for our data. This is almost ALWAYS a better way to go then to use a group of arrays or even a cell array.
Below is the code to show a single variable (named student) which contains two fields which are "sub-components" of what it means to be a student. Each field has its own name and its own type.
student.name = 'jim';
student.gpa = 4.0;
Here is an example of how to use arrays to represent "logically connected" data:
student_ages = [ 21, 33, 25, 24 ];
student_names = { 'Jim', 'Jane', 'Joe', 'Janet' };
To the computer, these two variables are completely separate. It has no way for the computer to know that 'Jim' is 21. We have to rely on the fact that the programmer (you) sees that both variables have similar names, and thus they "might" be related.
Here is an example of how to create a structure (actually an array of structures) to represent the same data.
students(1).name = 'jim';
students(1).age = 21;
students(2).name = 'Jane';
students(2).age = 33;
students(3).name = 'Joe;
students(3).age = 25;
students(4).name = 'Janet;
students(4).age = 24;
% The following is the WRONG way to do it,
% Unfortunately, some of you will do it on the midterm and the
% final... Do not use the underscore when you mean array ( index ).
students_1.name = 'jim';
students_1.age = 21;
students_2.name = 'Jane';
students_2.age = 33;
students_3.name = 'Joe;
students_3.age = 25;
students_4.name = 'Janet;
students_4.age = 24;
% The following is ALSO the WRONG way to do it...
% Note that we want an array of students. Below we have a single
% student who has many names and many ages.
students.name(1) = 'jim';
students.age(1) = 21;
students.name(2) = 'Jane';
students.age(2) = 33;
students.name(3) = 'Joe;
students.age(3) = 25;
students.name(4) = 'Janet;
students.age(4) = 24;
We now have an SINGLE array variable "students" containing many Student Data Structures. Thus:
students(1) % gives us:
name = 'jim'
age = 21
The computer can now keep track of all the information dealing with a single student as a single "structure". (Again, above we create an array of these structures so that we can have lots of student records.)
The syntax of a structure is "The Variable Name" followed by a "period" followed by the "field". Thus student is the name of the "structure variable".
The "field" is the name of the sub part of the variable (the part the follows the dot. So with our student structure, we currently have two fields (i.e., name and age). Each field has a data type associated with it (which can be: numbers, characters, booleans, structures, or arrays).
This is the beginning of using Abstract Data Types. ADTs are collections of data and functions that work on the data. In this case the data is the student structure.
What kind of functions could we write using the data structure above?
How about:
a_student = find_student(students, 'jim');
This function would search through the entire array of students for any student with the name jim. We could then write a function that would compute the average quiz grade for a student.
Such a function would return the grade for that student, assuming of course the student structure contained a list of grades. To do that we would say:
a_student = find_student(students, 'jim');
average_grade = compute_average_quiz_grade(a_student);
% of course this would imply that each student had
% an array of quiz scores inside the student structure. Such a field
% could be added using code like:
students(1).quiz_scores = [98, 87, 89, 76, 95, 92];
Notice that the above code adds a third field (called quiz_scores) to the student structure. This field contains an ARRAY of data. Notice how we combine structures and arrays to build up more complex data types.
Here we have an array of structures (i.e., students). Each structure contains two arrays (i.e., a string and an array of numbers, (name, quiz_scores) and a single number (i.e., age (an integer)).
What do you think student(2).grades contains at this point? Try it on the computer.