SUNY Geneseo Department of Mathematics
Monday, December 7
Math 230 01
Fall 2015
Prof. Doug Baldwin
>> % Hello world
>> hello = 'Hello world'
hello =
Hello world
>> % Can do vector things to strings -- e.g., transpose, index, etc.
>> hello'
ans =
H
e
l
l
o
w
o
r
l
d
>> hello(7)
ans =
w
>> hello(2:5)
ans =
ello
>> length(hello)
ans =
11
>> % Upper vs lower case matters: various ways of comparing strings handle this differently
>> s = 'Hello World'
s =
Hello World
>> hello == s
ans =
Columns 1 through 9
1 1 1 1 1 1 0 1 1
Columns 10 through 11
1 1
>> strcmp( hello, s )
ans =
0
>> hello
hello =
Hello world
>> s
s =
Hello World
>> strcmpi( hello, s )
ans =
1
>> helloBackward = hello( end:-1:1 )
helloBackward =
dlrow olleH
>> strcmp( hello, helloBackward )
ans =
0
>> if hello == s
fprintf( 'Equal!\n' )
else
fprintf( 'Not Equal!\n' )
end
Not Equal!
>> if hello == hello
fprintf( 'Equal!\n' )
else
fprintf( 'Not Equal!\n' )
end
Equal!
>> strcmp( hello, s )
ans =
0
>> strncmp( hello, s, 5 )
ans =
1
>> hello == 'hello'
Error using ==
Matrix dimensions must agree.
>> strcmp( hello, 'hello' )
ans =
0
>> strcmp( hello, 0 )
ans =
0
>> isequal( hello, 'hello' )
ans =
0
>> isequal( hello, 0 )
ans =
0
>> isequal( 0, 0 )
ans =
1
>> % Character codes really occupy (at least) 16 bits and use Unicode rather than ASCII
>> char( 65 )
ans =
A
>> uint8( 'A' )
ans =
65
>> char( 8706 )
ans =
∂
>> uint8( '∂' )
ans =
255
>> uint16( '∂' )
ans =
8706
>> % isequal function handles arbitrary size mismatches in vectors
>> isequal( hello, 'hello' )
ans =
0
>> isequal( [1 2 3], linspace(1,10,1000) )
ans =
0