Structures

Structures represent a "named" collection of related data. For anything in a computer more complicated than a list of numbers, structures can be used. For example, to represent any given car, we might want to know how many doors it has, if it has AC, what its max speed is, etc. All of this information relates to a single "Car" entity, and should be saved in a computer using a structure data type. We use the "DOT" notation to access a structure stored in a variable, thus, car.max_speed, car.number_of_doors, car.ac, are all valid references to a structure.

Structure

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).

        
        
        student.name = 'jim';
        student.gpa  = 4.0;

        
      

Above we create a variable called student. The student variable is of TYPE: STRUCTURE. The structure has two fields. The names of the fields are "name" and "gpa". The name field tells the programmer that this data represents the name of the student. The type of the "name" part of the structure is "array of characters" (aka STRING). The type of the "gpa" part of the structure is Number (or floating point number to be more precise).


The Dot notation.

To reference a field of a structure, we use a "dot" between the name of the variable containing the structure, and the name of the field of the structure.


Syntax and Semantics

Structures are defined with sub-fields. A sub-field is:

  1. Named: It has its own name which represents its relationship to the whole entity. For example, car.number_of_doors. number_of_doors is the field. cars is the structure.
  2. Typed: Each sub-field of the object must have its own specific type. The valid types are the same as for any variable.

Structures vs. Objects

Structures (found in Matlab and C) and Objects (found in C++, Java, ActionScript, etc) are mostly the same thing. The both contain named data fields. Objects also contain functions to manipulate the data inside of the object. In C and Matlab, functions must be created separately to manipulate the data inside an object.

Matlab Structures

For more information on Structures in Matlab see the appropriate sub-chatper


Back to Topics List