ae6382 9 strings.cellarrays.structures

22
Fall 2006 AE6382 Design Computing 1 Cell Arrays and Structures Learning Objectives Learn about characters, cell arrays & structures Topics Data Types Character Strings Cell Arrays Structures Summary

Upload: manishtopsecrets

Post on 17-Nov-2015

218 views

Category:

Documents


0 download

DESCRIPTION

matlab

TRANSCRIPT

  • Cell Arrays and Structures

    Learning ObjectivesLearn about characters, cell arrays & structuresTopics

    Data TypesCharacter Strings Cell ArraysStructuresSummary

    AE6382 Design ComputingIt is very important that students understand the need for Cell Arrays and Structures in engineering.

    I found that getting the conceptual need for cell arrays and structures across AND showing them the relevancy to what thet are and will be doing in the NEAR future with them is difficult. They are at a point only tell me what I need to know not what I might need in the future.

    I also found that the students found it difficult to recognize how easy it is to carry over what they already know about manipulating numerical arrays.

    Pace yourself so that you get to the Exercise on Slide 21. Class time on Thursday is meant to be used as a Tutorial. On Thursday you solve Assignment 4 or the Exercise that I have given on Slide 21.

    Alternately, you can go much slower and cover this material in 2 lectures

  • Storing More Than NumbersMATLAB matrices store only numeric resultsWhat about words, names, strings?What about arrays of arrays?MATLAB provides three more containers to store dataCharacter arraysCell arraysStructuresExamplesGradebooksHierarchical geographic data

    AE6382 Design ComputingLet students discover the need for different data types.Relate to something that students are likely to know about.

    All of you have Palm Pilots. Think about the address book and the scheduler. Ask students how many have Palm Pilots / electronic address books / electronic schedulers.

    Ask students to give you information that will need to go into the Palm pilot directory. Write this information on the board.

    Ask students what features they would like this diary / scheduler to have. Need to cover examples of all data types. Guide them them to saying that they would like to use the wireless feature to dial the telephone.

    Write this on the board.

    You will use this information later.

  • What are Character Arrays?Examples:

    C = 'Hello'; %C is a 1x5 character array. D = 'Hello there; %D is a 1x11 character array. A = 43; %A is a 1x1 double array. T = 'How about this character string? size(T)ans =

    1 32 whos % What do you observe? Learn?

    AE6382 Design ComputingNote the difference between how information is stored in numeric and alphanumeric form.

  • How are Characters Stored?Character arrays are similar to vectors, except:Each cell contains a single digitExample

    u = double(T) % double is a dedicated function. char(u) % performs the opposite function.

    Exercise

    a = double('a') char(a)

    Questions: What is the numerical value of a and what does it mean?

    AE6382 Design ComputingIf you are going to use this as the basis for the tutorial on Thursday ask the students to come prepared for it. Aside: You too will need to prepare!

  • Manipulating String ArraysStrings can be manipulated like arrays.

    Exercises

    u = T(16:24) u = T(24:-1:16) u = T(16:24) v = 'I can''t find the manual! % Note quote in string u ='If a woodchuck could chuck wood,'; v = 'how much wood could a woodchuck chuck?'; w = [u,v] % string concatenation in Matlab disp(u) % works just like for arraysLessons?

    AE6382 Design ComputingRemind students that manipulations for numerical arrays transfer over to character arrays. Go through the following by way of reminding them (page 66):A(r,c)A(r,:)A(:,c)A(:)A(k)A(x)

  • Character Strings in Multiple Rows v = ['Character strings having more than' 'one row must have the same number 'of columns just like arrays! ']Exercise

    lawyers = char('Cochran','Shapiro','Clark','Darden'); lawyers(3,:)Lesson?Exercise

    help char help str2mat help strvcatLesson?

    AE6382 Design ComputingLength of row in each row of character array needs to be the same. This is akin to number of columns in a numerical matrix being the same.

    Take students through the help command comment on them.

    Remind students that manipulations for numerical arrays transfer over to character arrays. Go through the following by way of reminding them (page 66):A(r,c)A(r,:)A(:,c)A(:)A(k)A(x)

  • String Construction / Manipulation>> t = How about this character string?>> size (t) % Whats this?>> whosLesson?

    >> u = double (t)>> char(u)Lesson?

    >>u = t(16:24)>>u = t(24:-1:16)>>u = t(16:24)Lesson?

    Page 129-131

    AE6382 Design ComputingThe lessons are well described on the pages in the text.

  • What are Cell Arrays?Cell arrays are containers for collections of data of any type stored in a common container.

    Cell arrays are like a wall of PO boxes, with each PO box containing its own type of information.

    When mail is sent to a PO box the PO box number is given. Similarly each cell in a cell array is indexed.

    Cell arrays are created using cell indexing in the same way that data in a table or an array is created and referenced,.

    The difference is the use of curly braces { }.

    AE6382 Design ComputingStress the importance of curly braces.

  • Cell Array AccessCell arrays look a lot like Matlab arrays but they cannot generally be manipulated the same way.Cell arrays should be considered more as data containers and must be manipulated accordingly. (Cell arrays cannot be used in arithmetic computations like arrays can, e.g., + - * / ^)

    Addressing Cell Arrays:

    A(i,j) = {x} this is called CELL INDEXINGA{i,j} = x this is called CONTENT ADDRESSINGeither can be used, but be careful

    AE6382 Design Computing

  • Cell Array Examplesfirst = Hello;second = {hello,world,from,me};third (1,1) = {happy}; % Cell indexingthird {2,1} = birthday; % Content addressingthird {3,1} = 40;What do you observe? Lesson?

    >> third>> third (1,1), third {1,1}>> third (2,1), third {2,1}>> third (3,1), third {3,1}

    AE6382 Design ComputingThis is an important concept. It is well explained in the text.

    Students have a difficult time figuring out the relevancy. Let them discover it in the context of the electronic address book.

  • Cell Arrays of StringsAll rows in a string array MUST have the same number of columns this is a pain. Solution?Cell arrays!!!! Next slide but just try the following!Exercise

    C = {'How';'about';'this for a';'cell array of strings?'}Question: What is different from what you have been doing before?Exercises

    size(C) C(2:3) C([4,3,2,1]) [a,b,c,d] = deal(C{:})Lessons?

    AE6382 Design ComputingMake case for Structures.

    Use the example of the electronic address book as an example to make this point.

    deal is an important command. Demonstrate and emphasize its importance.

    In context of the electronic address book ask students to tell you the characteristic of a Cell Array. You can take a peak on the next slide.

  • Cell Array Examples contd.Exercise

    C = cell(2,3) % Defines C to be a cell array C(1,1) = {'This does work'} % ( ) refer to PO Box C{2,3} = 'This works too % { } refers to contentsLesson?

    A = cell(1,3) % Note 1 x 3 A = {'My' , 'name', 'is' , Burdell'} % Note 1 x 4 A = {'My'; 'name'; 'is' ; Burdell'}Lessons?Exercise Important

    help lists

    AE6382 Design ComputingLists is a very important concept and command.

    Take them through it they will be glassy eyed about it all. Set it as an exercise for them to absorb the material at home.

  • Cell Array Examples contd.Exercise

    A = {'My'; 'name'; 'is' ; 'Farrokh'} iscellstr(A) % logical test for a cell array of strings ischar(A) % logical test for a string arrayLesson?

    Exercise

    help cell help cellstr help celldisp B = cell(2,4) B = {'My', 'name', 'is', Burdell; 10, 20, 30, 40} celldisp(B)Lesson?

    AE6382 Design ComputingMake case for the need to be able to figure out what data type resides in a cell.

    What is the point? The computer stores information in the form of ones and zeros. Depending on an indicator the information is displayed in alphanumeric or numerical form.

    Aside: But what is the difference between a numerical one and and alphanumerical one? Where is this relevant? Tie to Excel.

  • Cell Arrays are Simple

    The basics of working with cell arrays are well explained in the Matlab documentation.

    Additional information can be found at the Mathworks web site:http://www.mathworks.com/

    AE6382 Design ComputingPlease go through these tutorials prior to class. I am partial to the one from UT Austin. Show them the Tutorial and what you think is nifty.

  • What are Structures?Numeric, character and cell arrays all reference the individual elements by number.

    Structures reference individual elements within each row (called fields) by name.

    To access these fields, the dot . notation is used.

    Assignment is as follows:

    structurename.fieldname = datatype;Text Page 115

    AE6382 Design ComputingYou are setting the students up to discover structures (take a peak at the next slide).

    In the context of the electronic address book ask for volunteers to give particulars about their mothers, special friends, etc. Get names, addresses, birth days, anniversaries, telephone numbers, etc. In effect get a mixed bag of information. Be sure to cover all data types.

    Then work the information into a form that appears on the next few slides.

  • Creating a StructureLets create a simple structure:

    person.firstname = George;person.lastname = Burdell;person.address1 = 803 Tech Parkway;person.city = Atlanta;person.state = GA;person.zip = 30332-0001;

    AE6382 Design ComputingGet student to relate the preceding to an email address.

    Extend them to think about email alias. The structure is analogous to an email alias.

  • Structures: A Bigger PictureStructures can hold elements in fields, which in turn contain data types.

    mystructext1numb1text2Hello[1 2 3 4]AE6382Fall 2004numb25 67 8

    AE6382 Design ComputingIf you have collected good data from the students map that into the structure shown above.

    Ask them where in their lab work would this be relevant today? You will get some very interesting answers. If you do you know at least some are getting the idea.

  • More on StructuresA structure can have a field that is a structure itself.

    A structure array is that which contains more than one record for each field name.

    As the structure array is expanded (more records are created), all unassigned fields are filled with an empty matrix.

    All structures have the same number of fields and elements in each field.

    AE6382 Design ComputingBy now students are getting glassy eyed.

    Make these points. If you notice they are glassy eyed inform them that in this lecture you are going from the particular to the general.

    On Thursday you will be tying it all together by working an example with them in class. Tell them that they MUST come prepared for this. You are covering the basics and they will need to put in the effort to learn on the material to create their own knowledge. Spend a moment talking about scaffolding.

  • Example of a Structure ...:student.name.first = Joe;student.name.last = Smith;student.score = 82.39;student.grade = B;student(2).name.first = Anna;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.

    AE6382 Design ComputingThis is an example from a grade book. What happens? The cells are created but they are empty at first. What makes a cell empty?

  • Picture of Student Grade StructurestudentnamescoregradefirstlastJoeSmith82.39BAnnaLee94.50[ ]Jerry[ ][ ][ ]student(1)student(2)student(3)

    AE6382 Design ComputingExplain what [] means the cell content is null. But the cell exists. Tie back to content addressing and cell indexing (Slide 10).

  • Structure ResourcesStructures are explained in the Matlab online documents.You can find tutorials on the web:

    http://www.mathworks.com/

    AE6382 Design ComputingI am partial to the University of Texas tutorial. Before the class go through it. In class get students to open it and give them your views about some nifty and useful stuff.

  • Summary

    Action ItemsReview the lectureReview the material on the websites

    LearningMatlab can store and manipulate much more than simply numbers

    AE6382 Design ComputingGet the students to articulate what they picked up and to write it down in the left panel.

    Stress the importance of the tutorial and coming prepared for it.

    It is very important that students understand the need for Cell Arrays and Structures in engineering.

    I found that getting the conceptual need for cell arrays and structures across AND showing them the relevancy to what thet are and will be doing in the NEAR future with them is difficult. They are at a point only tell me what I need to know not what I might need in the future.

    I also found that the students found it difficult to recognize how easy it is to carry over what they already know about manipulating numerical arrays.

    Pace yourself so that you get to the Exercise on Slide 21. Class time on Thursday is meant to be used as a Tutorial. On Thursday you solve Assignment 4 or the Exercise that I have given on Slide 21.

    Alternately, you can go much slower and cover this material in 2 lecturesLet students discover the need for different data types.Relate to something that students are likely to know about.

    All of you have Palm Pilots. Think about the address book and the scheduler. Ask students how many have Palm Pilots / electronic address books / electronic schedulers.

    Ask students to give you information that will need to go into the Palm pilot directory. Write this information on the board.

    Ask students what features they would like this diary / scheduler to have. Need to cover examples of all data types. Guide them them to saying that they would like to use the wireless feature to dial the telephone.

    Write this on the board.

    You will use this information later.Note the difference between how information is stored in numeric and alphanumeric form.If you are going to use this as the basis for the tutorial on Thursday ask the students to come prepared for it. Aside: You too will need to prepare! Remind students that manipulations for numerical arrays transfer over to character arrays. Go through the following by way of reminding them (page 66):A(r,c)A(r,:)A(:,c)A(:)A(k)A(x)Length of row in each row of character array needs to be the same. This is akin to number of columns in a numerical matrix being the same.

    Take students through the help command comment on them.

    Remind students that manipulations for numerical arrays transfer over to character arrays. Go through the following by way of reminding them (page 66):A(r,c)A(r,:)A(:,c)A(:)A(k)A(x)The lessons are well described on the pages in the text.Stress the importance of curly braces.

    This is an important concept. It is well explained in the text.

    Students have a difficult time figuring out the relevancy. Let them discover it in the context of the electronic address book.Make case for Structures.

    Use the example of the electronic address book as an example to make this point.

    deal is an important command. Demonstrate and emphasize its importance.

    In context of the electronic address book ask students to tell you the characteristic of a Cell Array. You can take a peak on the next slide. Lists is a very important concept and command.

    Take them through it they will be glassy eyed about it all. Set it as an exercise for them to absorb the material at home. Make case for the need to be able to figure out what data type resides in a cell.

    What is the point? The computer stores information in the form of ones and zeros. Depending on an indicator the information is displayed in alphanumeric or numerical form.

    Aside: But what is the difference between a numerical one and and alphanumerical one? Where is this relevant? Tie to Excel. Please go through these tutorials prior to class. I am partial to the one from UT Austin. Show them the Tutorial and what you think is nifty.You are setting the students up to discover structures (take a peak at the next slide).

    In the context of the electronic address book ask for volunteers to give particulars about their mothers, special friends, etc. Get names, addresses, birth days, anniversaries, telephone numbers, etc. In effect get a mixed bag of information. Be sure to cover all data types.

    Then work the information into a form that appears on the next few slides.Get student to relate the preceding to an email address.

    Extend them to think about email alias. The structure is analogous to an email alias.If you have collected good data from the students map that into the structure shown above.

    Ask them where in their lab work would this be relevant today? You will get some very interesting answers. If you do you know at least some are getting the idea.By now students are getting glassy eyed.

    Make these points. If you notice they are glassy eyed inform them that in this lecture you are going from the particular to the general.

    On Thursday you will be tying it all together by working an example with them in class. Tell them that they MUST come prepared for this. You are covering the basics and they will need to put in the effort to learn on the material to create their own knowledge. Spend a moment talking about scaffolding.

    This is an example from a grade book. What happens? The cells are created but they are empty at first. What makes a cell empty?Explain what [] means the cell content is null. But the cell exists. Tie back to content addressing and cell indexing (Slide 10).I am partial to the University of Texas tutorial. Before the class go through it. In class get students to open it and give them your views about some nifty and useful stuff.Get the students to articulate what they picked up and to write it down in the left panel.

    Stress the importance of the tutorial and coming prepared for it.