This preview shows pages 1–2. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: Organizing Data - 1 6 - Organizing Data Motivation So far, our data types have been rather simple: scalars, arrays and strings. We are now going to introduce an aggregate data type which is called a structure . The concept of structured programming is the first step toward object-oriented programming. Structures are a way of organizing multiple pieces of information in one variable. They serve two very important functions in computer programs. The first is to collect different types of data about an object in one place. For example, you might want to represent the manufacturer, number of cylinders, and firing sequence for an internal combustion engine in a single record that describes the engine. The second function of structures is to represent relations between objects by containing pointers to other structures. Organizing information about an object Lets pursue the engine example and construct a MATLAB structure describing an engine. A structure consists of named fields , each of which has a value . We can make the structure in MATLAB by simply listing and assigning values to the fields: >> Engine.maker = 'Harley-Davidson'; >> Engine.cylinders = 4; >> Engine.firingOrder = [1 3 2 4]; The field names are maker, cylinders , and firingOrder , and their values are 'Harley-Davidson' , 4 , and [1 3 2 4], respectively. They are separated from the structure variable by a .. If we print the MATLAB variable Engine , we get: >> Engine maker: 'Harley-Davidson' cylinders: 4 firingOrder: [1 3 2 4] Notice that the fields of Engine all have different types. One is a string, one is a scalar, and one is a row vector. This is precisely the reason that structures are useful. They allow and one is a row vector....
View Full Document
This note was uploaded on 02/12/2011 for the course E 7 taught by Professor Patzek during the Spring '08 term at University of California, Berkeley.
- Spring '08