Introduction to MATLAB - Georgia Institute of Technology

Download Report

Transcript Introduction to MATLAB - Georgia Institute of Technology

Cell Arrays and Structures
Learning
Objectives
Topics
Data Types
Character Strings
Cell Arrays
Structures
Summary
Learn about
characters, cell
arrays &
structures
AE6382 Design Computing
1
Fall 2006
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
2
Fall 2006
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?
AE6382 Design Computing
3
Learn?
Fall 2006
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?
AE6382 Design Computing
4
Fall 2006
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
5
Fall 2006
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
6
Fall 2006
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
7
Fall 2006
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
8
Fall 2006
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
9
Fall 2006
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
10
Fall 2006
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{:})
AE6382 Design Computing
Lessons?
11
Fall 2006
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'}
Note 1 x 4
• Lessons?
• Exercise … Important
» help lists
AE6382 Design Computing
12
Fall 2006
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
13
Fall 2006
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
14
Fall 2006
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
15
Fall 2006
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
16
Fall 2006
Structures: A Bigger Picture…
• Structures can hold elements in fields, which in
turn contain data types.
‘Hello’
text1
numb1
[1 2 3 4]
text2
AE6382
Fall 2004
numb2
5 6
7 8
mystruc
AE6382 Design Computing
17
Fall 2006
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
18
Fall 2006
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’;
AE6382 Design Computing
19
• NOTE: There
are other
spaces to fill,
but we haven’t
assigned any
values to
these fields,
so they remain
as an empty
matrix.
Fall 2006
Picture of Student Grade Structure
student(1) student(2) student(3)
first
Joe
Anna
Jerry
last
Smith
Lee
[]
score
82.39
94.50
[]
grade
B
[]
[]
name
student
AE6382 Design Computing
20
Fall 2006
Structure Resources…
• Structures are explained in the Matlab online
documents.
• You can find tutorials on the web:
http://www.mathworks.com/
AE6382 Design Computing
21
Fall 2006
Summary
Learning
Matlab can store
and manipulate
much more than
simply numbers
AE6382 Design Computing
Action Items
• Review the lecture
• Review the material on the websites
22
Fall 2006