Covenant College December 18, 20151 Laura Broussard, Ph.D. Visiting Professor COS 131: Computing for Engineers Chapter 7: Cell Arrays and Structures.

Slides:



Advertisements
Similar presentations
Lists Chapter 4 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Advertisements

COMP 116: Introduction to Scientific Programming Lecture 37: Final Review.
Chapter 10 Introduction to Arrays
CMPS 1371 Introduction to Computing for Engineers STRUCTURE ARRAYS.
Chapter 9. 2 Objectives You should be able to describe: Addresses and Pointers Array Names as Pointers Pointer Arithmetic Passing Addresses Common Programming.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
Lecture 4 Sept 8 Complete Chapter 3 exercises Chapter 4.
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
EGR 106 – Week 2 – Arrays & Scripts Brief review of last week Arrays: – Concept – Construction – Addressing Scripts and the editor Audio arrays Textbook.
Lec 13 Oct 20 cell arrays (Chapter 4) structures (Chapter 4) generating subsets generating permutations (HW 4 problem)
Lec 15 Oct 27 more examples of recursive programs more about cell arrays structures in Matlab.
Data types and variables
Chapter 9: Arrays and Strings
Computer Science II Exam I Review Monday, February 6, 2006.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Concatenation MATLAB lets you construct a new vector by concatenating other vectors: – A = [B C D... X Y Z] where the individual items in the brackets.
Chapter 2 Data Types, Declarations, and Displays
C++ for Engineers and Scientists Third Edition
JavaScript, Third Edition
Lecture 18 Oct 24 Cell arrays (Ch 4), structures recursion (Ch 12)
Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections.
Chapter 12 Pointers and linked structures. 2 Introduction  The data structures that expand or contract as required during the program execution is called.
EGR 106 – Week 3 – More on Arrays Brief review of last week Additional ideas: – Special arrays – Changing an array – Some array operators – Character arrays.
Chapter 2: Algorithm Discovery and Design
Lec 14 Oct 22 more examples of recursive programs more about cell arrays structures in Matlab.
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
Objectives You should be able to describe: Data Types
Covenant College September 3, Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Chapter 6: Character Strings.
Operaciones y Variables
Tutorial 14 Working with Forms and Regular Expressions.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Engineering Computation with MATLAB Second Edition by David M. Smith.
Lecture 2 Arrays, Pointers, and Structures. Objective In this chapter, we will discuss several concepts: Arrays (first-class arrays, using vector) Strings.
Lists in Python.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Fall 2006AE6382 Design Computing1 Cell Arrays and Structures Learning Objectives Learn about characters, cell arrays & structures Topics Data Types Character.
Input, Output, and Processing
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Lecture 15 Chapters 6 and 7. Outline from Chapter Character String Concepts: Mapping and Casting 6.2 MATLAB Implementation Slicing and Concatenating.
Built-in Data Structures in Python An Introduction.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics.
C++ How to Program, 9/e © by Pearson Education, Inc. All Rights Reserved.
Introduction to Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational.
Covenant College November 27, Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Chapter 5: Functions.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
>> x = [ ]; y = 2*x y = Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions.
+ Structures and Unions. + Introduction We have seen that arrays can be used to represent a group of data items that belong to the same type, such as.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
C++ for Engineers and Scientists Second Edition
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Chapter 14 Using JavaBeans Components in JSP Documents.
Chapter 18 Introduction to Custom Templates C++ How to Program, 9/e ©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved. Instructor Note:
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
CHAPTER 51 LINKED LISTS. Introduction link list is a linear array collection of data elements called nodes, where the linear order is given by means of.
INTRODUCTION TO COMPUTER PROGRAMMING(IT-303) Basics.
1-2 What is the Matlab environment? How can you create vectors ? What does the colon : operator do? How does the use of the built-in linspace function.
Containers and Lists CIS 40 – Introduction to Programming in Python
Intro to PHP & Variables
Introduction to C++ Programming
Chapter 8 Data Structures: Cell Arrays and Structures
Fondamenti di informatica structures
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
Objectives You should be able to describe: Addresses and Pointers
Chapter 8 Data Structures: Cell Arrays and Structures, Sorting
Introduction to Computer Science
Presentation transcript:

Covenant College December 18, Laura Broussard, Ph.D. Visiting Professor COS 131: Computing for Engineers Chapter 7: Cell Arrays and Structures

Covenant College December 18, Introduction Addresses the nature, implementation, and behavior of collections that may contain data items of any class, size, or shape. Have applications where we must group data of different types in a common, logical structure. This is where we use cell arrays and structures. Two different heterogeneous storage mechanisms: –Those accessed by index (cell arrays) –Those accessed by name (structures) –Will also consider collecting structures into arrays of structures

Covenant College December 18, Introduction Considers data collections that are more general and flexible than the arrays we have considered thus far. These collections may contain objects of any type, rather than just numbers None of the operations defined for numerical arrays can be applied to cell arrays and structures To perform operations on their contents, the items must be extracted one at a time and replaced if necessary

Covenant College December 18, Introduction Consider three different mechanisms for building heterogeneous collections: –Cell arrays index their contents with a numerical index –Structures index their contents with a symbolic index –Structure arrays index structures with a numerical index

Covenant College December 18, Concept: Collecting Dissimilar Objects Heterogeneous collections permit objects of different data types to be grouped in a collection Collections may be of any data type severely restricts the operations that can be performed on the collections as a whole Algorithms that process heterogeneous collections must deal with the data contents one item at a time

Covenant College December 18, Cell Arrays Cell arrays, as the name suggests, have the general form of arrays and can be indexed numerically as arrays Each element of a cell array should be considered as a container in which one data object of any class can be stored Can be treated as arrays of containers for the purpose of concatenation and slicing Cells must be accessed individually

Covenant College December 18, Cell Arrays Creating Cell Arrays –Cell arrays may be constructed in the following ways: By assigning values individually to a variable indexed with braces:

Covenant College December 18, Cell Arrays Creating Cell Arrays –Cell arrays may be constructed in the following ways: By assigning container individually to a variable indexed with brackets: >> B[1] = {[4 6]} B = [ 1x2 double]

Covenant College December 18, Cell Arrays Creating Cell Arrays –Cell arrays may be constructed in the following ways: By concatenating cell contents using braces {…}:

Covenant College December 18, Cell Arrays Creating Cell Arrays –Cell arrays may be constructed in the following ways: By concatenating cell containers:

Covenant College December 18, Cell Arrays Creating Cell Arrays –From these examples we observe the following: A cell array can contain any legal MATLAB object Cell arrays can be created “ on the fly ” by assigning values to an indexed variable Note that the display of cell arrays is different from that of the contents of a number array; individual numbers are shown in brackets; larger numerical arrays display their size; character strings are displayed with the enclosing single quotes

Covenant College December 18, Cell Arrays Accessing Cell Arrays –Since cell arrays can be considered as conventional arrays of containers, the container can be accessed and manipulated normally: Note: MATLAB returns the array Dimensions and data type and NOT The contents as shown in the text. Note: Braces are used to access the Contents of the containers.

Covenant College December 18, Cell Arrays Accessing Cell Arrays –If the right-hand side of an assignment statement results in multiple cell arrays, the assignment must be to the same number of variables. –MATLAB function deal(…) is used to make these allocations –Exercise 7.1: Cell arrays –Smith text, page , bottom-top

Covenant College December 18, Cell Arrays Accessing Cell Arrays –Observations: When we extract the contents of multiple cells using A{1,2}, this results in multiple assignments being made Fundamental mechanism behind returning multiple results from a function Multiple assignments cannot be made to a single variable Cell arrays can be “ sliced ” with normal vector indexing assignments as long as the sizes match on the left and right sides of the assignment Unassigned array elements are filled with an empty vector

Covenant College December 18, Cell Arrays Accessing Cell Arrays –Observations: Consider the error here: –B{[1 3]} = A{[1 2]} –??? Cell contents assignment to a non-cell array object. Since A{[1 2]} produces two separate assignments, MATLAB will not assign the answers, even to the right number of places in another cell array Again, the deal(…) function is provided to capture these multiple results in different variables. Note difference between A{:} and A as a parameter to deal(…) When deal(…) is provided with a parameter other than a collection of cells, it copies that parameter to each variable

Covenant College December 18, Cell Arrays Accessing Cell Arrays –Observations: Assignments work normally if cell arrays are treated as vectors and the extraction of items can be indexed Notice that when accessing cell arrays, it is normal to have braces on one side or the other of an assignment; rare to have braces on both sides of an assignment; result here is that a cell array is loaded into the third container in the cell array

Covenant College December 18, Cell Arrays Using Cell Arrays –A number of uses for cell arrays in MATLAB Containing lists of possible values for switch/case statements Substituting for parameter lists in function calls –Listing 7.1: Using cell arrays of parameters Note: function largest is a hypothetical function that does not work in MATLAB. Could you write such a function? 5 points extra credit if you document the correct use of a function largest as used here by the next project due date.

Covenant College December 18, Cell Arrays Processing Cell Arrays –General template for processing cell arrays:

Covenant College December 18, Cell Arrays Processing Cell Arrays –Checking the class of the element can be achieved in one of two ways: The function class(item) returns a string specifying the item type that can be used in a SWITCH statement Individual test functions can be used in an IF… ELSEIF construct; examples of the individual test functions are –isa(item, ‘ class ’ ) –iscell(…) –ischar(…) –islogical(…) –isnumeric(…) –isstruct(…)

Covenant College December 18, Cell Arrays Processing Cell Arrays –Listing 7.2: Cell array processing example

Covenant College December 18, Cell Arrays Processing Cell Arrays –Observations on Listing 7.2 Function extracts each item in turn in line 6 Line 7 determines whether this item is of type DOUBLE We proceed to line 8 on condition –Line 8 accumulates the number of items in this array –The size(…) function returns a vector of the sizes of each dimension –The total number of numbers is then the product of these values

Covenant College December 18, MATLAB Structures Structures allow items in the collection to be indexed by a field name A field name is typically the name of a local variable or substructure Data contained in a structure is referenced by field name; example: item1 Rules for making a field name are the same as those for a variable Fields of a structure are heterogeneous like cell arrays

Covenant College December 18, MATLAB Structures Constructing and Accessing One Structure –To set the values of items in a structure A, the syntax is as follows:

Covenant College December 18, MATLAB Structures Constructing and Accessing One Structure –MATLAB displays the elements of an emerging structure by name –Fields in a structure are accessed in the same way-by using the dotted notation

Covenant College December 18, MATLAB Structures Constructing and Accessing One Structure –Can determine the names of the fields in a structure using the built-in function fieldnames(...) –Returns a cell array containing the field names as strings

Covenant College December 18, MATLAB Structures Constructing and Accessing One Structure –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:

Covenant College December 18, MATLAB Structures Constructing and Accessing One Structure –Can also remove a field from a structure using the built-in function rmfield(…) –Exercise 7.2: Building structures –Smith text, page 164, bottom –Note that since a structure may contain any object, it is quite legal to make a structure containing a date and insert that structure in the date field of the entry

Covenant College December 18, MATLAB Structures Constructor Functions –Constructor functions - Functions that assign their parameters to the fields of a structure and then return that structure –Do this rather than doing it manually for the following reasons: Manual entry can result in strange behavior due to typographical errors or having fields in the wrong order Resulting code is generally more compact and easier to understand When constructing collections of structures, it enforces consistency across the collections

Covenant College December 18, MATLAB Structures Constructor Functions –Two approaches to the use of constructor functions: Using built-in MATLAB capabilities Writing your own constructor –MATLAB built-in function, struct(…) consumes pairs of entries (each consisting of a field name as a string and a cell array of field contents) and produces a structure

Covenant College December 18, MATLAB Structures Constructor Functions –Useful in general to create structures –Repetition of the field names is annoying and potentially inefficient –Can create a special purpose function that “ knows ” the necessary field names to create multiple structures in an organized way

Covenant College December 18, MATLAB Structures Constructor Functions –Listing 7.3: Constructor function for a CD structure –Exercise 7.3: A CD structure –Smith text, page 166, middle

Covenant College December 18, Structure Arrays To be useful, collections like address books and CD collections require multiple structure entries with the same fields Accomplished by forming an array of data items, each of which contains the same fields of information Resulting data structure is termed a structure array Frequent application with database technologies

Covenant College December 18, Structure Arrays Constructing Structure Arrays –Structure arrays can be created either by Creating values for individual fields Using MATLAB ’ s struct(…) function to build the whole structure array Using a custom function to create each individual structure –Example here is our makeCD(…) function –Exercise 7.4: Building a structure array “ by hand ” –Smith text, page 167, middle

Covenant College December 18, Structure Arrays Constructing Structure Arrays –Listing 7.4: Building a structure array using struct(…)

Covenant College December 18, Structure Arrays Accessing Structure Elements –Items can be stored and retrieved by their index in the array –As structures are added to the array, MATLAB forces all elements in the structure array to implement the same field names in the same order –Elements can be accessed either manually (not recommended) or by creating new structures with a constructor and adding them (recommended)

Covenant College December 18, Structure Arrays Accessing Structure Elements –Listing 7.5: Building a structure array using a custom constructor

Covenant College December 18, Structure Arrays Accessing Structure Elements –Should you elect to manipulate them manually, you merely identify the array element by indexing, and using the.field operator

Covenant College December 18, Structure Arrays Accessing Structure Elements –Problematic if you make a typographical error:

Covenant College December 18, Structure Arrays Accessing Structure Elements –Should this happen, you can use the fieldnames(…) function to determine the situation, and then the rmfield(…) function to remove the offending entry

Covenant College December 18, Structure Arrays Accessing Structure Elements –Best to construct a complete structure and then insert it into the structure array:

Covenant College December 18, Structure Arrays Accessing Structure Elements –If you insert that new CD beyond the end of the array, as one might expect, MATLAB fills out the array with empty structures:

Covenant College December 18, Structure Arrays Manipulating Structures –Structures and structure arrays can be manipulated in the following ways: –Single values can be changed using the “. ” (dot) notation directly with a field name: –Or indirectly using the “. ” (dot) notation with a variable the field name: >>cds(5).price = >> fld = ‘ price ’ ; >> cds(5).(fld) = 19.95

Covenant College December 18, Structure Arrays Manipulating Structures –Or by using MATLAB ’ s equivalent built-in functions: nms = fieldname(str) returns a cell array containing the names of the fields in a structure or structure array –>> flds = fieldnames(cds) if = isfield(str, determines whether the given name is a field in this structure or structure array –>> if isfield(cds, ‘ price ’ ) … str = setfield(str., ) returns a new structure array with the specified field set to the specfied value – >> cds(1) = setfield(cds(1), … – ‘ price ’, 19.95); Val = getfield(str, ) returns the value of the specified field –>> disp(getfield(cds(1), ‘ price ’ ) );

Covenant College December 18, Structure Arrays Manipulating Structures –Or by using MATLAB ’ s equivalent built-in functions: Str = rmfield(str, ) returns a new structure array with the specified field removed –noprice = rmfield(cds, ‘ price ’ ); –Values across the whole array can be retrieved using the “. ” notation by accumulating them into arrays; either into cell arrays: >> title = {cds.title}; –Or if the values are all numeric, into a vector: >> prices = [cds.price]; –Exercise 7.5: The CD collection –Smith text, page 171, bottom –Note: after extracting the price values into a vector, all normal vector operations – in this case, sum(…), can be utilized

Covenant College December 18, Engineering Example: Assembling a Structure Building a structure Problem here is how to decide the sequence in which the components are delivered to the building site so that components are available when needed, but not piled up waiting to be used The construction needs to start from the fixed point A shown in the figure on the following slide We need to analyze this information and compute the order in which the components would be used to assemble the structure

Covenant College December 18, Engineering Example: Assembling a Structure Figure 7.1 from text:

Covenant College December 18, Engineering Example: Assembling a Structure Data will be organized as a structure array with one entry for each component One of the fields in that structure will be a cell array of the names of the components to which this component is connected Coding listings for this example follow on the next set of slides

Covenant College December 18, Engineering Example: Assembling a Structure Listing 7.6 Connectivity of a structure

Covenant College December 18, Engineering Example: Assembling a Structure Listing 7.6 Connectivity of a structure

Covenant College December 18, Engineering Example: Assembling a Structure Listing 7.7 Supporting Functions

Covenant College December 18, Engineering Example: Assembling a Structure Listing 7.7 Supporting Functions

Covenant College December 18, Engineering Example: Assembling a Structure Listing 7.7 Supporting Functions

Covenant College December 18, Engineering Example: Assembling a Structure Listing 7.7 Supporting Functions

Covenant College December 18, Engineering Example: Assembling a Structure Comments on example: –The structure array is constructed using the beam(…) function defined above –We traverse the components to make a structure array, FOUND, containing the components connected to the current connection point, CONN –We then go through the array FOUND, removing any component already on the connected list and append the names of those not removed to the connected list –Function BEAM is used to determine whether a component is connected to the component with the name provided –Function ISON is used to determine whether a particular string is on the connection list, a cell array of strings

Covenant College December 18, Engineering Example: Assembling a Structure Comments on example: –Function NEXTCONN is used to find the next connection to use based on the latest component found-the “ outer edges ” of the emerging structure-and its not being already on the connected list –If the component in question is not on the connected list, exit and return it; note that the return variable, nm, has been set in function NEXTCONN on lines 27-29