1371 Review Test 2 Rev 3

1371 Review Test 2 Rev 3 - 1371 Review Test #2 1....

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1371 Review Test #2 1. Structures a. Complex data type that allows multiple kinds of data to be stored in them. a.k.a it does not have to be a homogeneous data type like all letters or all numbers, the structure can take any kind of data type in it. b. Two ways to make structures: i. Direct : student.name = Michael student.age = 20 student.major= Mechanical Engineering ii. Using struct function: student = struct(name , Michael , age , 20 , major , Mechanical Engineering) 1. The way the struct function works is that it works byt two inputs at a time. The first input is the field name and the second input is what value to put into the field. Make sure that you use quotations when necessary on strings. iii. To get stuff out of the structure, you can use the period operator. If there is already a structure called student: student.name will return whatever the name stored is. c. Be sure to use how to the getfield(), isfield(), rmfield(), setfield(), and fieldnames() functions t hey can come in handy when you iterate through a structure array and have to check a field and/or change them. **be careful with setfield, and rmfield.. you still need to set it equal to something for example rmfield(student, age) does not change the st udent structure. d. Dynamic Variable Indexing (best name I think I can give this idea in structures): i. Given a student structure, st, that has fields name, age, and major, lets define the variable age = 'major'. To clarify this, age is a variable sepearate from the structure that has the string, 'major' as its value. 1. If we use st.age , we will get the age that is associated with the structure. The fact that age is also a variable has no impact here. 2. Now, using st.(age) will return what the students major is. The use of the parenthesis tells matlab that whatever is inside them is a variable. So, for MATLAB, st.(age) is just like saying st.major 3. If you dont like the whole when do I use ()'s things when a fieldname is stores as a variable, you can also just use the get field function. From the given example, all of the following will return the same thing: a=st.major , a =st.(age), a=getfield(st,'major'), a=getfield(st,age) ii. See the swapFields function that was in this .zip file to see an example of dynamic variables in action. It's also a very good piece of code to know how/why its working if you are asked to create something similar to it 2. Arrays a. So yeah, now you get a long array with structures in them which pretty much means that you have two of the most complicated/annoying data types put together. But it isnt as bad as you would think. All you have to do is iterate through the length of the structure array and then check whatever field/fields you are interested in....
View Full Document

Page1 / 8

1371 Review Test 2 Rev 3 - 1371 Review Test #2 1....

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online