Class Prep - PowerPoint PPT Presentation

1 / 39
About This Presentation
Title:

Class Prep

Description:

Hands-On DEMO: CD Albums. Simplified version of Engineering Example 7.5 ... album(2) = makeCD( 'rock', 'Lincoln Park', 'Metea', 16, 3, 2003) ... – PowerPoint PPT presentation

Number of Views:60
Avg rating:3.0/5.0
Slides: 40
Provided by: davidh67
Category:
Tags: albums | class | prep

less

Transcript and Presenter's Notes

Title: Class Prep


1
Class Prep
  • Bring to Class
  • In-Class Exercises
  • Homework 6
  • Paper for Classroom Printer
  • Copy Programs and CD Folder
  • Run MATLAB

2
Grab Files
  • Copy Clue_script.m, students.m, (CDs.m, makeCD.m,
    filterCDs.m, printAll.m) in CD folder from
    70week06 of the csc070 course folder into your
    sub-folder
  • Run MATLAB and set the Current Directory to your
    sub-folder.

3
HW 5d
  • function ltrs firstLetters(str) by Dave
    Hannay
  • find the first letters in this string
  • where regexp(str,'\lt\w')
  • ltrs str(where)

4
Week 06-b(7.3-7.5)
  • Structures

5
Creating a Structure
  • Lets create a simple structure
  • person.firstname 'George'
  • person.lastname 'Orwell'
  • person.address1 '123 Sesame St.'
  • person.city 'Any City'
  • person.state 'NY'
  • person.zip '12345-6789'

6
Structures are Universal Containers
  • Any type of data can be loaded into a structure
    array
  • Different data types can be loaded and will
    overwrite whatever is already there
  • Structure addressing is simpler that Cell Arrays
  • fieldnames( ) will return the field names in a
    structure
  • You should get used to using the dot notation
    because it is widely used in programming,
    especially with objects

7
Structures The Big Picture
  • Structures can hold elements in fields, which in
    turn contain data.

text1
'Hello'
numb1
1 2 3 4
mystruc
AE6382 Fall 2004
text2
5 6 7 8
numb2
8
Terminology
  • A structure array is a collection of records.
  • A record is a set of related fields where each
    field may contain a different data type.
  • A field is an array of data that defines a
    particular attribute of an object.
  • i.e. The collection of fields comprise a record
    the collections of records comprise a structure
    array.
  • For example
  • Structure Array employees
  • Record employees(3)
  • Fields name, address, date of employment,
    salary, etc.

9
Hands-On DEMO Sample of Direct Assignment(see
Clue_script.m)
  • Clue struct Set up "Clue" as an empty struct
  • Structure fields can be
  • created and assigned dynamically.
  • Clue.who 'Prof Plum'
  • Clue.where 'Study'
  • Clue.what 'Candlestick'
  • disp(Clue)
  • who 'Prof Plum'
  • where 'Study'
  • what 'Candlestick'

10
More Samples
  • Even though no index was used in creating the
  • first record in 'Clue', another set can be
  • appended by using an index.
  • Clue(2).who 'Ms. Scarlet'
  • Clue(2).where 'Library'
  • Clue(2).what 'Rope'
  • disp(Clue(2))
  • who 'Ms. Scarlet'
  • where 'Library'
  • what 'Rope'

11
Adding Fields
  • New fields can be added after structures
  • have been created.
  • Clue(2).turns 15 "turns" is a new field
  • disp(Clue(2)) used 1st in record 2
  • who 'Ms. Scarlet'
  • where 'Library'
  • what 'Rope'
  • turns 15

12
Adding Fields
  • Adding new fields will cause a 'null' field in
    the
  • cells already defined.
  • disp(Clue(1))
  • who 'Prof Plum'
  • where 'Study'
  • what 'Candlestick'
  • turns turns was not defined originally

13
The struct Function
  • All fields (a complete record) can be assigned
  • with one statement.
  • All text values are enclosed in quotes
  • numerical data are not.
  • Clue(3) struct('who', 'Col Mustard', ...
    'where', 'kitchen', ...
    'what', 'revolver', ...
    'turns', 22)
  • disp(Clue(3))
  • who 'Col Mustard'
  • where 'kitchen'
  • what 'revolver'
  • turns 22

14
Using Array Functions
  • Clue(1).turns is null, we can assign it a
    value.
  • Clue(1).turns 17
  • Below, notice the lack of an index for Clue and
    the square brackets
  • around Clue.turn. This combination returns a
    vector of 'turns'
  • to the MATLAB function, sum, which calculates
    the sum of that
  • vector.
  • The length function returns the number of
    records in Clue.
  • Dividing the sum by the number of records
    gives us the average
  • of the numbers stored in turns.
  • avg_turns sum(Clue.turns)/length(Clue)
  • mean(Clue.turns) would also
    work!
  • Then we display our results.
  • fprintf('Average number of turns
    0.1f\n',avg_turns)
  • 18.0

15
Hands-On DEMO people Structure
  • people(1).name 'Elmo'
  • people(1).city 'Schenectady'
  • people(1).state 'NY'
  • people(2).name 'Oscar'
  • disp(people(2))
  • Also try
  • Disp(people)

16
DEMO Adding Fields to Structures
  • You add a field to every structure in an array by
    adding the field to a single structure. For
    example, to add a social security number field to
    the person array
  • people(2).ssn '555-12-6666'
  • Now people(2).ssn has the assigned value.
  • NOTE Every other structure in the array now also
    has the ssn field, but these fields contain the
    empty matrix until you explicitly assign a value
    to them.

17
Manipulating Field Names
  • To determine the names of the fields in a
    structure, the built-in function fieldnames(...)
    returns a cell array containing the field names
    as strings.
  • gtgt fieldNames fieldnames(people)
  • fieldNames
  • 'name'
  • 'item2'...
  • Fields can also be accessed indirectly by
    setting a variable to the name of the field, and
    then using parentheses to indicate that the
    variable contents should be used as the field
    name
  • gtgt fn fieldNames1
  • gtgt people(1).(fn) people(1).(fn) 'xxx'
  • people
  • name 'Elmoxxx'
  • city Schenectady
  • ...

18
DEMO Deleting Fields from Structures
  • You can remove a given field from every structure
    within a structure array using the rmfield
    function. Its most basic form is
  • struct2 rmfield(struct1, 'field')
  • newpeople rmfield(people,'state')
  • disp(newpeople)
  • disp(people)
  • disp(newpeople(1))

19
Hands-On DEMO Examining Structure Values
  • Note that you can double-click variables in the
    Workspace to examine their values
  • For structure arrays, you can double-click
    individual records (in the grid) to examine
    individual field values
  • Try this with the people and newpeople structures

20
Nesting Structures
  • A structure field can contain another structure,
    or even an array of structures.
  • Once you have created a structure, you can use
    the struct function or direct assignment
    statements to nest structures within existing
    structure fields.

21
Nested Structures with the struct Function
  • For example, create a 1-by-1 structure array
  • A struct('data', 3 4 7 8 0 1, 'nest',...
  • struct('testnum', 'Test 1', 'xdata', 4 2
    8,...
  • 'ydata', 7 1 6) )
  • gtgt disp(A(1).data)
  • gtgt disp(A(1).nest.xdata)
  • Try double-clicking on the A variable in the
    Workspace

22
Nested Structures using Direct Assignment
  • These statements add a second element to the
    array
  • A(2).data 9 3 2 7 6 5
  • A(2).nest.testnum 'Test 2'
  • A(2).nest.xdata 3 4 2
  • A(2).nest.ydata 5 0 9

23
Indexing Nested Structures
  • To index nested structures, append nested field
    names using dot notation.
  • The first text string in the indexing expression
    identifies the structure array, and subsequent
    expressions access field names that contain other
    structures.
  • To access all the xdata vector in the nested
    structure in A(2), use A(2).nest.xdata.
  • To access element 2 of the ydata field in A(1),
    use A(1).nest.ydata(2).

24
Another Nested Structure
  • student.name.first 'Joe'
  • student.name.last 'Smith'
  • student.score 82.39
  • student.grade 'B'
  • student(2).name.first 'Jane'
  • student(2).name.last 'Lee'
  • student(2).score 94.50
  • Student(3).name.first 'Jerry'
  • NOTE There are other spaces to fill, but we
    havent assigned any values to these fields, so
    they remain as an empty matrix.

25
Picture of Student Grade Structure
student(2)
student(3)
student(1)
first
Joe
Jane
Jerry
name
last
Smith
Lee

student
score
82.39
94.50

grade
B


26
Hands-On DEMO Student Structure(see students.m)
  • Creates a Structure Array called student with
    fields name and grade (grade will be an array
    with three exam grades in it)
  • Fills in the structure with made up data for
    three students
  • Averages the grades of all three students and
    displays the result in the command window

27
7.5 Engineering ExampleAssembling a Structure
  • Given this structure, determine the order of
    assembly starting from A.

28
Hands-On DEMO CD Albums
  • Simplified version of Engineering Example 7.5
  • Each entry in the array will be a structure
    describing the CD
  • Genre
  • Artist
  • Title
  • Tracks
  • Year
  • See CD folder

29
Custom Constructor Functions
  • See makeCD.m
  • gtgt CD makeCD('Blues', 'Charles, Ray', ...
  • 'Genius Loves Company', 18, 4.5, 2004 )
  • CD
  • genre 'Blues'
  • artist 'Charles, Ray'
  • title 'Genius Loves Company'
  • tracks 18
  • stars 4.5000
  • year 2004

30
Building Structure Arrays with makeCD()
  • See CDs.m
  • album(1) makeCD( 'rap', 'P Diddy', 'Doofus',
    14, 4, 1996)
  • album(2) makeCD( 'rock', 'Lincoln Park',
    'Metea', 16, 3, 2003)
  • album(3) makeCD( 'classic', 'Yanni',
    'Dreaming', 26, 0.6, 1999)
  • album(4) makeCD('country', 'Garth Brooks',
    'Sevens',20, 4.5, 1995)

31
Getting at ALL the Fields
  • function printAll(CDs)
  • flds fieldnames(CDs(1))
  • for CD CDs
  • str '-gt'
  • for index 1length(flds)
  • attrib fldsindex
  • str str ' ' strValue(CD.(attrib))
  • end
  • disp(str)
  • end

32
Hands-On DEMO Filter by Year
  • filterCDs should display the titles
  • of all CDs since some given year
  • year input('List CDs since what year? ')
  • filterCDs(album, year)
  • Try 1999

33
The filterCDs function
  • function c filterCDs( a, yr )
  • extract the titles of CDs recorded since this
    year
  • for ix 1length(a)
  • thisCD a(ix)
  • if thisCD.year gt yr
  • disp(thisCD.title)
  • end
  • end

34
SUMMARY Cell Array vs. Structure
35
Further Study
  • There are only 6 different operations you can
    possibly perform on a collection of data items.
  • Each operation requires a function to specify how
    that operation should be performed
  • The operations are
  • Build create the collection from some source
  • Traverse touch each item and summarize
  • Map change the values in each item
  • Filter remove some items from the collection
  • Search determine whether a specific item exists
  • Sort re-order the collection

36
What else could we do with our catalog of CDs?
  • Display each item as a catalog
  • Delete items you have sold or discarded
  • Add items as you buy new ones
  • Update all the records to add a field (like price
    or perhaps its position in the top 100)
  • Find a particular CD by ANY fieldname
  • Sort them by artist or genre

37
Homework 6 Structures File I/O
  • 6a) Problem 9, page 182.
  • 6b) Problem 13, page 184.
  • 6c) Problem 1, page 200.
  • 6d) Problem 5, page 201.

38
In-Class Exercise 6b Structured Data(Problem
10, Page 182)
  • Create a library as an array of structures. Each
    structure has the following fields Title,
    Author, ISBN.
  • Using MATLAB structures, create the first three
    entries for the library structure array, using
    data of your choosing.
  • Write a script that will display the titles of
    all the books in the library structure array.
  • You should be able to deal with libraries of all
    different sizes.

39
END
Write a Comment
User Comments (0)
About PowerShow.com