state->city. Key methods for manipulating character strings, cell arrays and structures were also presented.">state->city. Key methods for manipulating character strings, cell arrays and structures were also presented.">
Nothing Special   »   [go: up one dir, main page]

Cell Arrays and Structures: Learning Objectives Topics

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 22

Cell Arrays and Structures

Learning
Objectives
Learn about
characters, cell
arrays &
structures

Topics
Data Types
Character Strings
Cell Arrays
Structures
Summary

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

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
whos

32
% What do you observe?

Learn?

How are Characters Stored?


Character arrays are similar to vectors, except:
Each cell contains a single digit

Example
u = double(T)
char(u)

% double is a dedicated function.


% performs the opposite function.

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

Questions: What is the numerical value of a and


what does it mean?

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?

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?

String Construction / Manipulation


>> t = How about this character string?
>> size (t) % Whats 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

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 { }.

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

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}

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?

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'} %
A = {'My'; 'name'; 'is' ; Burdell'}

Lessons?
Exercise Important
help lists

Note 1 x 4

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?

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/

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

Creating a Structure
Lets 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;

Structures: A Bigger Picture


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

text1

Hello

numb1

[1 2 3 4]

text2

AE6382
Fall 2004

numb2

5 6
7 8

mystruc

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.

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.

Picture of Student Grade Structure


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

first

Joe

Anna

Jerry

last

Smith

Lee

[]

score

82.39

94.50

[]

grade

[]

[]

name
student

Structure Resources
Structures are explained in the Matlab online
documents.
You can find tutorials on the web:
http://www.mathworks.com/

Summary

Learning
Matlab can store
and manipulate
much more than
simply numbers

Action Items
Review the lecture
Review the material on the websites

You might also like