1 / 12

CMPS 1371 Introduction to Computing for Engineers

STRUCTURE ARRAYS. CMPS 1371 Introduction to Computing for Engineers. Structure Arrays. Similar to Cell Arrays Multiple arrays of differing data types can be stored in structure arrays Instead of using content indexing each of the matrices is given a location called a field. Structures.

Download Presentation

CMPS 1371 Introduction to Computing for Engineers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. STRUCTURE ARRAYS CMPS 1371Introduction to Computing for Engineers

  2. Structure Arrays Similar to Cell Arrays Multiple arrays of differing data types can be stored in structure arrays Instead of using content indexing each of the matrices is given a location called a field

  3. Structures This class of arrays allows you to store dissimilar arrays together. The elements in the structure are accessed using named fields You create a structure array either by using assignment statements or by using the struct function. Structure arrays use the dot notation (.) to specify and to access the fields.

  4. Structures • Think about a student database (e.g. name, social security number, email address, test scores). • There are four fields (4 field names): 3 string and 1 vector containing numerical elements. • A structure consists of all this information for a single student and a structure array is an array of such structures for different students. Student record name ssn email test scores

  5. Arrangement of data in the structure array student.

  6. Entering Data • We can enter the data directly >> student.name = 'John Smith'; >> student.ssn = '123-45-6789'; >> student.email = 'smithj@myschool.edu'; >> student.tests = [67,75,84];

  7. Entering Data • We can also enter the data through a structure function “struct”: >> student = struct(‘name’, ‘John Smith’, ‘ssn’, ‘123-45-6789’, ‘email’, ’smithj@myschool.edu’, ‘tests’, [67,75,84])

  8. Access Data • The name studentis now used to refer to the entire array of structures. • We can access elements of a structure array as if it was a normal array • For example: >> student(1)

  9. Functions • The Matlab function fieldnames • recovers all the field names associated with a structure. • returns a cell array of strings. • For example: >> fieldnames(student) ans = 'name' 'ssn' 'email' 'tests‘ • Matlab function rmfield - removes fields from a structure.

  10. Arrays of structures • Often we will want multiple instances of a single structure. • We can build up an array of structures. We call this array a structure array. • Matlab allows a structure array to grow automatically.

  11. >>student(2).name = ‘Mary Jones’; Since we only specified the name field of student(2), Matlab initializes the remaining fields to empty arrays: We can then fill in extra details as required. >>student(2).ssn = ‘431-56-9832’; >>student(2).email = ‘jonesm@myschool.edu’ >>student(2).tests = [84, 78, 93]; Add another student

  12. Why are structures so much better Structures use the "name" of a "field" to access the data…so the name can be useful things like 'artist'. Compared to "3rd element" Structures can be thought of as a "data structure" organizes the data in a logical way that can be applied many times. Structures can be put into arrays as long as the structures all have the same fields. This makes having many "objects" of the same type easy.

More Related