1 / 22

Cell Arrays and Structures

Learning Objectives Learn about characters, cell arrays & structures. Topics Data Types Character Strings Cell Arrays Structures Summary. Cell Arrays and Structures. Storing More Than Numbers. MATLAB matrices store only numeric results What about words, names, strings?

jsonja
Download Presentation

Cell Arrays and Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Storing More Than Numbers • MATLAB matrices store only numeric results • What about words, names, strings? • What about arrays of arrays? • MATLAB provides three more containers to store data • Character arrays • Cell arrays • Structures • Examples • Gradebooks • Hierarchical geographic data AE6382 Design Computing

  3. 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 Computing

  4. How are Characters Stored? • Character arrays are similar to vectors, except: • Each cell contains a single digit • Example » 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 Computing

  5. Manipulating String Arrays • Strings 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 arrays • Lessons? AE6382 Design Computing

  6. 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 strvcat • Lesson? AE6382 Design Computing

  7. String Construction / Manipulation >> t = ‘How about this character string?’ >> size (t) % What’s this? >> whos • Lesson? >> 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 Computing

  8. 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 Computing

  9. Cell Array Access • Cell 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 INDEXING A{i,j} = x this is called CONTENT ADDRESSING • either can be used, but be careful… AE6382 Design Computing

  10. Cell Array Examples first = ‘Hello’; second = {‘hello’,’world’,’from’,’me’}; third (1,1) = {‘happy’}; % Cell indexing third {2,1} = ‘birthday’; % Content addressing third {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 Computing

  11. Cell Arrays of Strings • All 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 Computing

  12. 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 contents • Lesson? » 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 Computing

  13. 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 array • Lesson? • 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 Computing

  14. 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 Computing

  15. 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 Computing

  16. Creating a Structure… • Let’s 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 Computing

  17. Structures: A Bigger Picture… • Structures can hold elements in fields, which in turn contain data types. text1 ‘Hello’ numb1 [1 2 3 4] mystruc AE6382 Fall 2004 text2 5 6 7 8 numb2 AE6382 Design Computing

  18. More on Structures… • A 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 Computing

  19. 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 haven’t assigned any values to these fields, so they remain as an empty matrix. Example of a Structure ...: AE6382 Design Computing

  20. Picture of Student Grade Structure student(2) student(3) student(1) first Joe Anna Jerry name last Smith Lee [ ] student score 82.39 94.50 [ ] grade B [ ] [ ] AE6382 Design Computing

  21. Structure Resources… • Structures are explained in the Matlab online documents. • You can find tutorials on the web: http://www.mathworks.com/ AE6382 Design Computing

  22. Summary Learning Matlab can store and manipulate much more than simply numbers Action Items • Review the lecture • Review the material on the websites AE6382 Design Computing

More Related