CZ1102 Computing & Problem Solving Lecture 7
CZ1102 Computing & Problem Solving Lecture 7
Week 9
By Ji Hui
Reviews of last week
• Matrix in Linear Algebra • Matrix object in Matlab
Article Cell
or books array
Word &
Sentence String
Characters Char
Characters & char
• character Matlab session
– A‐Z >> letter='#';
– a‐z >> letter
– 0‐9 letter =
– Symbols: #,@,? and etc
#
• char
– enclosing the character in single
quotation marks
– e.g. ‘z’
Internal representation of char
• Internally represented by 16 bit numerical values
– ASCII encoding scheme
– e.g. ASCII codes for the letters ‘A’ to ‘Z’ are the
consecutive integers from 65 to 90
• Function: char
– convert non‐negative integer to corresponding char
• Functon: double
– Convert char to corresponding integer
ASCII code, char and double
Matlab session
>> c=char(70) % character of ASCII code 70
c=
F
>> char(c+1) % character after ‘F’ in ASCII table
ans =
G
>> double(c) % ASCII code for ‘F’
ans =
70
>> char(65:128) % list part of ASCII table
ans =
ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvw
xyz{|}~
String
• Definition: a vector of chars
• string
– enclosing a sequence of letters in single quotation
marks
Matlab session
– e.g. s=‘Hello’ >> myname='George'
name =
George
s=
Hi, this is George’s dog!
String operations
• String is a vector, vector operations also work
for string
• Concatenation of strings
– Joining strings with square brackets
Matlab session
>> course='CZ1102'
course =
CZ1102
>> course=[course,' Problem Solving']
course =
CZ1102 Problem Solving
(cont’)
• Comparing strings
– compares the ASCII codes of the two strings
element by element
– Two strings must have the same length
Matlab session
>> univ1='nus';
univ2='ntu';
univ1 == univ2
ans =
1 0 0
Demo of applications
• Convert string to lower case
– mylower.m
Matlab session
function u = mylower(s)
u = s; % copy string to u
for i=1:length(s) % for each char in s
code = double(s(i)); % finding the ascii code
if (code >=65 && code <=90) % if code corresponds to upper case
code = code + 32; % find the code for lower case
u(i) = char(code); % convert ascii code to lower-char
end;
end;
Two dimensional strings
• Creating a two‐dimensional character array
– Each row must have the same length
– Padding shorter strings with blanks on the right.
Matlab session
>>line0=['Ji Hui '];
line1=['Department of Mathematics '];
line2=['National university of Singapore'];
address=[line0;line1;line2]
address =
Ji Hui
Department of Mathematics
National university of Singapore
Easier way to define two‐dim strings
• Function: char
– Automatically padding shorter strings with blanks
Matlab session
>> address=char('Ji Hui ',' Department of Mathematics')
address =
Ji Hui
Department of Mathematics
• Drawback
– wasting storage by saving many blanks.
Cell
• Cell
– Most general data object in Matlab
– Can be viewed as a “data container”, that can
contain any types of data:
• number, string, matrix, structure and cell itself.
• Definition of cell
– Enclosing the content by curly backets
– e.g. s={‘department’}
(cont’)
Matlab session
>> s={'department'}
s=
'department'
>> whos s
• Cell array
– Using curly brackets to enclose subscripts.
Matlab session
>>c{1,1} = rand(3);
>>c{1,2} = char(’Bongani’, ’Thandeka’);
>>c{2,1} = 13;
>>c{2,2} = student;
>>c
c=
[3x3 double] [2x5 char]
[ 13] 'student'
More on creating cell array
• You can use the curly braces to construct an
entire cell array in one statement:
Matlab session
>>b = {[1:5], rand(2); 'student', char('Jason', 'Amy')}
b=
[1x5 double] [2x2 double]
'student' [2x5 char ]
• Creating empty cell arrays
Matlab session
>>a = cell(1,3) % empty 1-by-3 cell array
a=
[] [] []
Accessing data in cell arrays
• You can access cell contents using content
indexing (curly braces):
Matlab session
>> c{1,1}
ans =
0.7431 0.1712 0.2769
0.3922 0.7060 0.0462
0.6555 0.0318 0.0971
>>
>> c{2,2}
ans =
Student
Summary of differences between
matrices and cell arrays
Operations Matrix Cell array
name 1x64 char
grade
id 1x16 char
mark 1x4 double
Define a structure
Matlab session
>> grade.name='Jason';
grade.id='u0920203a';
grade.marks=[90, 85, 86,92];
>> grade
grade =
name: 'Jason'
id: 'u0920203a'
marks: [90 85 86 92]
How to access fields.
• Using dot to separate structure name and
field name
Matlab session
>> cname=grade.name
cname =
Jason
>> mterm = grade.marks(3)
mterm =
86
Structure array
• To use array of structure, use subscripts after
the structure name
Matlab session
>>grade(2).name='Sara';
grade(2).id = 'u1020202020';
grade(2).marks = [76 64 83 72];
>> grade
grade =
ans =
Sara
>> grade(2).marks(1:3)
ans =
76 64 83
Summary
• Char
• String
• 2D string array
• Cell array
• Structure
• Structure array