Matlab Chapter 8
What is a nested structure? How can they be referenced? What happens if just the variable name for a nested structure is typed?
Nested structure: A structure in which at least one member is itself a structure -EX: A structure for a line segment might consist of fields representing the two points at the ends of the line segment. Each of these points would be represented as a structure consisting of the x and y coordinates. >> lineseg = struct('endpoint1',struct('x',2,'y',4), . . . 'endpoint2',struct('x',1,'y',6)) Referencing a point goes as such: >> lineseg.endpoint1.x Typing in just the name of the variable: >> lineseg lineseg = endpoint1: [1 x 1 struct] endpoint2: [1 x 1 struct] >> lineseg.endpoint1 = x: 2 y: 4 In the attached example, both endpoints are a structure
What are 'cell arrays' and how are they created (including methods)?
Cell Array: Found in Matlab but not many other languages. Elements in cell arrays can store different types of values (so they can store strings, integers, and characters all at once). To Create: Same idea as a regular vector, but uses { } brackets instead >>cellrowvec = {23, 'a', 1:2:9, 'hello'} %can also make it a vector cell array with semi-colons or {'a', 23}' Methods: 1. Ascribing element by element (inefficient method) 2. Preallocating vector if it's known what size it will be. Done using 'cell' function >>mycell = cell(2,2) [ ] [ ] [ ] [ ]
What are the benefits of having strings in cell arrays instead of character matrices? What do the following functions do: cellstr char iscellstr
Cell arrays can store strings of different lengths without including trailing blanks (meaning each element in a cell array can have a unique length) >> names = {'Sue', 'Cathy', 'Xavier'} >>length(names{1}) = 3 >>length(names{2}) = 6 cellstr: Converts a character array into a cell array in which the padding blanks (trailing blanks) have been removed >> greetmat = char('Hello','Goodbye'); >> cellgreets = cellstr(greetmat) cellgreets = 'Hello' 'Goodbye' char: Can convert cell array into a character matrix >> names = {'Sue', 'Cathy', 'Xavier'}; >> cnames = char(names) cnames = Sue Cathy Xavier >> size(cnames) = 3 6 iscellstr: Returns logical true if a cell array contains only strings, and logical false if it does not >> iscellstr(names) = 1
How and why would you combine vectors and nested structures?
Combining vectors and nested structures, it is possible to have a vector of struc- tures in which some fields are structures themselves. Here is an example in which a company manufactures cylinders from different materials for indus- trial use. Information on them is stored in a data structure in a program. Can be initialized with 'struct' and preallocating or with dot operator The following is an example of initializing this data structure by preallocating: >> cyls(3) = struct('code', 'c', 'dimensions',. . . struct('rad', 3, 'height', 6), 'weight', 9); >> cyls(1) = struct('code', 'x', 'dimensions',. . . struct('rad', 3, 'height', 6), 'weight', 7); or >> cyls(3).code = 'c'; >> cyls(3).dimensions.rad = 3; ************* There are several layers in this variable. For example, -cyls is the entire data structure, which is a vector of structs -cyls(1) is an individual element from the vector, which is a struct -cyls(2).code is the code field from the struct cyls(2); it is a character -cyls(3).dimensions is the dimensions field from the struct cyls(3); it is a struct itself -cyls(1).dimensions.rad is the rad field that is from the struct cyls(1).dimensions; it is a double number Example function for calling elements is attached
What is 'content indexing'? 'Cell indexing'?
Content Indexing: Referencing CONTENTS of a cell. Curly brackets must be used. Values can be assigned to cell array elements. { } Cell Indexing: Referencing the cell. When an element of a cell array is itself a data structure (like a cell that contains multiple integers), only the type of the element is displayed when the cells are shown. ( ) >> cellmat = {23 'a'; 1:2:9 'hello'} >> cellmat(2,1) <---Cell Indexing ans = [1 x 5 double] >> cellmat{2,1} <---Content Indexing ans = 1 3 5 7 9 >> cellmat{2,1}(4) ans = 7 There's also differences when referencing multiple values/cells. For content indexing, multiple values are produced, for cell indexing, both cells referenced are placed into a single variable. When content indexing, if variables to store are not given, then ans will be the last cell outputted. >>cellcolvec = {23; 'a'; 1:2:9; 'hello'}; >> [c1 c2] = cellcolvec{2:3} c1 = a c2 = 1 3 5 7 9 <---ans would be this without [c1 c2] >>cellcolvec(2:3) ans = 'a' [1 x 5 double]
What are 'Data Structures'? What are types of data structures and what would they be used for? What are similarities and differences between the types?
Data structure: Variables that store more than one value. For it to make sense to store more than one value in a variable, the values should somehow be logically related. There are many different kinds of data structures. Types: 1. Cell Array: A kind of data structure that stores values of different types. Cell arrays can be VECTORS OR MATRICES; the different values are referred to as the elements of the array. -Use: Store strings of different lengths. Cell arrays actually store pointers to the stored data. 2. Structures: A form of data structures that group together values that are logically related, but are not the same thing and not necessarily the same type. The different values are stored in separate fields of the structure -Use: One use of structures is to set up a database of information (ex: student name, ID, test scores, attendance). Each student would be a single record, but in matlab the records are called 'structs' Similarities: Both cell arrays and structures can be used to store values that are different types in a single variable. Differences: -Cell arrays are indexed, and can therefore be used with loops or vectorized code. -Structures, on the other hand, are not indexed; the values are referenced using the names of the fields, which can be more mnemonic than indexing.
What are structures? What are are advantages and disadvantages to them? How do you create them? How do you display them? How do you remove an element from them?
Structures: Data structures that group values together that are logically related in fields of the structure. -Advantage of structures is that the fields are named, which helps to make it clear what values are stored in the structure. -However, structure variables are not arrays. They do not have elements that are indexed, so it is not possible to loop through the values in a structure. 2 ways to creates structures: 1. struct function >> package = struct('item_no',123,'cost',19.99, 'price',39.95,'code','g') package = item_no: 123 cost: 19.9900 price: 39.9500 code: 'g' 2. dot operator >> package.item_no = 123; >> package.cost = 19.99; >> package.price = 39.95; >> package.code = 'g'; The variable 'package' does not need to be preallocated. The first input will create the structure 'package' Displaying structures: 1. disp function: Displays all of it 2. fprintf: Can only display a single value of the structure at a time >>disp(package) >>fprintf('this is a number %d',package.item_no) rmfield: Removes a field from a structure. It returns a new structure with the field removed, but does not modify the original structure (unless the returned structure is assigned to that variable). >>rmfield(package, 'code') ans = package.item_no = 123; package.cost = 19.99; package.price = 39.95; -But the variable package is unchanged because it was not assigned a new variable or modified ex: package = rmfield(package, 'code')
What are vector of structures? What is an efficient way to create one? What happens if you just type in the variable name of the vector of structure? How can they be displayed? How can you store a vector within a single element of a vector of struct?
Vector of Structures: Numerous rows and columns listing multiple elements/persons/objects and the fields that accompany them (similar to a typical excel layout). ************** An efficient way to do this is with 'repmat' >> packages = repmat(struct('item_no',123,'cost',19.99, 'price',39.95,'code','g'),1,3); -This would preallocate vectors to more efficiently store information. This could be followed by: packages(2) = struct('item_no',456,'cost', 5.99, 'price',49.99,'code','l'); ************** Typing in the variable name would do something like this: >> packages packages = 1 x 3 struct array with fields: item_no cost price code -'packages' is now a vector of structures. To display a single element with its' fields, the row has to be referenced >>packages(2) = item_no: etc. -To reference a specific field of a specific element: >>packages(2).code ************** fprintf can display a specific field of all elements as such: >>fprintf('%f\n',packages.cost) -This would print out every elements 'cost' -The values can also be stored in a vector as such: >>pc = [packages.cost] -This creates a vector of all 'cost' values -Because of the nature of this, functions can be used to affect all elements >>sum([packages.cost]) -Similarly, using this methodology, the entire field or element can be passed through a function See attached printpackages.m for an example ************* Sometimes, some elements in vectors of structs can be vectors themselves. One example can be a series of students with the following: Name, quiz grades, ID_no. Here, the quiz grades are treated as a single element, but are actually a vector >> student(3) = struct('name','Brownnose, Violet',. . . 'id_no',332,'quiz', [7.5 6 8.5 7.5]); >> student(1) = struct('name','C, Joe',. . . 'id_no',999,'quiz', [10 9.5 0 10]); >> student(2) = struct('name','Hernandez, Pete',. . . 'id_no',784,'quiz', [10 10 9 10]); >> student student = 1 x 3 struct array with fields: name id_no quiz >> student(1) ans = name: 'C, Joe' id_no: 999 quiz: [10 9.5000 0 10] >>student(1).name = C, Joe Keep in Mind: -student is the entire data structure, which is a vector of structs -student(1) is an element from the vector, which is an individual struct -student(1).quiz is the quiz field from the structure, which is a vector of doubles -student(1).quiz(2) is an individual double quiz grade -student(3).name(1) is the first letter of the third student's name One example of using this data structure would be to calculate and print the quiz average for each student (see attached printAves.m)
What do these functions do: celldisp cellplot
celldisp: Displays contents of all elements of a cell array cellplot: Puts a graphical display of the cell array into a Figure Window; however, it is a high-level view and basically just displays the same information as typing the name of the variable (so, for instance, it would not show the contents of the vector in the previous example). In other words, it shows the cells, not their contents. >>cellrowvec = {23, 'a', 1:2:9, 'hello'}
What do the following functions do: isstruct isfield fieldnames
isstruct: Return logical 1 for true if the variable argument is a structure variable, or 0 if not. isfield: Returns logical true if a field- name (as a string) is a field in the structure argument, or logical false if not. fieldnames: Returns names of the fields within the structure variable in the form of a cell array. Curly braces are used to refer to the elements, since pack_fields is a cell array. >> isstruct(package) ans = 1 >> isfield(package,'cost') ans = 1 >> pack_fields = fieldnames(package) pack_fields = 'item_no' 'cost' 'price' 'code' >> length(pack_fields{2}) ans = 4
