{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

06 Organizing Data

# 06 Organizing Data - 6 Organizing Data Motivation So far...

This preview shows pages 1–2. Sign up to view the full content.

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 Let’s 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

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 5

06 Organizing Data - 6 Organizing Data Motivation So far...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online