fall 2006ae6382 design computing1 cell arrays and structures learning objectives learn about...

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: marlene-curtis

Post on 13-Jan-2016

225 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Fall 2006AE6382 Design Computing1 Cell Arrays and Structures Learning Objectives Learn about characters, cell arrays & structures Topics Data Types Character

Fall 2006AE6382 Design Computing 1

Cell Arrays and Structures

Learning Objectives

Learn about characters, cell arrays & structures

Topics

Data Types

Character Strings

Cell Arrays

Structures

Summary

Page 2: Fall 2006AE6382 Design Computing1 Cell Arrays and Structures Learning Objectives Learn about characters, cell arrays & structures Topics Data Types Character

Fall 2006AE6382 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

Page 3: Fall 2006AE6382 Design Computing1 Cell Arrays and Structures Learning Objectives Learn about characters, cell arrays & structures Topics Data Types Character

Fall 2006AE6382 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?

Page 4: Fall 2006AE6382 Design Computing1 Cell Arrays and Structures Learning Objectives Learn about characters, cell arrays & structures Topics Data Types Character

Fall 2006AE6382 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?

Page 5: Fall 2006AE6382 Design Computing1 Cell Arrays and Structures Learning Objectives Learn about characters, cell arrays & structures Topics Data Types Character

Fall 2006AE6382 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?

Page 6: Fall 2006AE6382 Design Computing1 Cell Arrays and Structures Learning Objectives Learn about characters, cell arrays & structures Topics Data Types Character

Fall 2006AE6382 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?

Page 7: Fall 2006AE6382 Design Computing1 Cell Arrays and Structures Learning Objectives Learn about characters, cell arrays & structures Topics Data Types Character

Fall 2006AE6382 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

Page 8: Fall 2006AE6382 Design Computing1 Cell Arrays and Structures Learning Objectives Learn about characters, cell arrays & structures Topics Data Types Character

Fall 2006AE6382 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 { }.

Page 9: Fall 2006AE6382 Design Computing1 Cell Arrays and Structures Learning Objectives Learn about characters, cell arrays & structures Topics Data Types Character

Fall 2006AE6382 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…

Page 10: Fall 2006AE6382 Design Computing1 Cell Arrays and Structures Learning Objectives Learn about characters, cell arrays & structures Topics Data Types Character

Fall 2006AE6382 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}

Page 11: Fall 2006AE6382 Design Computing1 Cell Arrays and Structures Learning Objectives Learn about characters, cell arrays & structures Topics Data Types Character

Fall 2006AE6382 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?

Page 12: Fall 2006AE6382 Design Computing1 Cell Arrays and Structures Learning Objectives Learn about characters, cell arrays & structures Topics Data Types Character

Fall 2006AE6382 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

Page 13: Fall 2006AE6382 Design Computing1 Cell Arrays and Structures Learning Objectives Learn about characters, cell arrays & structures Topics Data Types Character

Fall 2006AE6382 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?

Page 14: Fall 2006AE6382 Design Computing1 Cell Arrays and Structures Learning Objectives Learn about characters, cell arrays & structures Topics Data Types Character

Fall 2006AE6382 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/

Page 15: Fall 2006AE6382 Design Computing1 Cell Arrays and Structures Learning Objectives Learn about characters, cell arrays & structures Topics Data Types Character

Fall 2006AE6382 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

Page 16: Fall 2006AE6382 Design Computing1 Cell Arrays and Structures Learning Objectives Learn about characters, cell arrays & structures Topics Data Types Character

Fall 2006AE6382 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’;

Page 17: Fall 2006AE6382 Design Computing1 Cell Arrays and Structures Learning Objectives Learn about characters, cell arrays & structures Topics Data Types Character

Fall 2006AE6382 Design Computing 17

Structures: A Bigger Picture…

• Structures can hold elements in fields, which in turn contain data types.

mystruc

text1

numb1

text2

‘Hello’

[1 2 3 4]

AE6382

Fall 2004

numb25 6

7 8

Page 18: Fall 2006AE6382 Design Computing1 Cell Arrays and Structures Learning Objectives Learn about characters, cell arrays & structures Topics Data Types Character

Fall 2006AE6382 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.

Page 19: Fall 2006AE6382 Design Computing1 Cell Arrays and Structures Learning Objectives Learn about characters, cell arrays & structures Topics Data Types Character

Fall 2006AE6382 Design Computing 19

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 haven’t assigned any values to these fields, so they remain as an empty matrix.

Page 20: Fall 2006AE6382 Design Computing1 Cell Arrays and Structures Learning Objectives Learn about characters, cell arrays & structures Topics Data Types Character

Fall 2006AE6382 Design Computing 20

Picture of Student Grade Structure

student

name

score

grade

first

last

Joe

Smith

82.39

B

Anna

Lee

94.50

[ ]

Jerry

[ ]

[ ]

[ ]

student(1) student(2) student(3)

Page 21: Fall 2006AE6382 Design Computing1 Cell Arrays and Structures Learning Objectives Learn about characters, cell arrays & structures Topics Data Types Character

Fall 2006AE6382 Design Computing 21

Structure Resources…

• Structures are explained in the Matlab online documents.

• You can find tutorials on the web:

http://www.mathworks.com/

Page 22: Fall 2006AE6382 Design Computing1 Cell Arrays and Structures Learning Objectives Learn about characters, cell arrays & structures Topics Data Types Character

Fall 2006AE6382 Design Computing 22

Summary

Action Items

• Review the lecture

• Review the material on the websites

Learning

Matlab can store and manipulate much more than simply numbers