94.把數值數組轉換成字符數組
>> x = [77 65 84 76 65 66];
xx=char(x)
xx =
MATLAB
>> y=int2str(rand(3))
y =
1 0 0
0 1 0
1 1 1
z=mat2str([1 2 3.5])
z =
[1 2 3.5]
a=dec2bin(356)
a =
101100100
b=dec2base(356,7)
b =
1016
whos
Name Size Bytes Class
a 1x9 18 char array
b 1x4 8 char array
x 1x6 48 double array
xx 1x6 12 char array
y 3x7 42 char array
z 1x9 18 char array
Grand total is 55 elements using 146 bytes
>> 7^3+7+6
ans =
356
95.把字符數組轉換成數值數組
name = 'Thomas R. Lee';
name = double(name)
name =
84 104 111 109 97 115 32 82 46 32 76 101 101
name = char(name)
name =
Thomas R. Lee
str = '37.294e-1';
val = str2num(str)
val =
3.7294
c = {'37.294e-1'; '-58.375'; '13.796'};
d = str2double(c)
d =
3.7294
-58.3750
13.7960
whos c d
Name Size Bytes Class
c 3x1 224 cell array
d 3x1 24 double array
Grand total is 28 elements using 248 bytes
hex2dec('3ff')
ans =
1023
bin2dec('010111')
ans =
23
base2dec('212',3) %???????
ans =
23
96.創建二維字符數組
a='a';
b='bb';
c=''; %創建空字符串
[a;b] %不同長度的字符串不能用方括號縱向連接
??? Error using ==> vertcat
All rows in the bracketed expression must have the same
number of columns.
[a,' ';b] %把第一行字符串用空格擴展成和第二行等長
ans =
a
bb
sAB=strvcat(a,b) %strvcat連接,不要求每行等長
size(sAB)
sAB =
a
bb
ans =
2 2
sABC=strvcat(a,b,c) %strvcat連接,空字符串被忽略
size(sABC)
sABC =
a
bb
ans =
2 2
cABC=char(a,b,c) %char連接,空字符串也被空格填滿
size(cABC)
cABC =
a
bb
ans =
3 2
97.空格處理函數
a=blanks(4)
a =
length(a)
ans =
4
b='test ';
length(b)
ans =
8
c=deblank(b)
c =
test
length(c)
ans =
4
d=' test ';
length(d)
ans =
11
e=strtrim(d)
e =
test
length(e)
ans =
4
98.兩個字符串逐個字符的比較
a='aa';
aa='aa ';
b='abcd';
c='c';
a==b %不等長的字符串不能用==比較
??? Error using ==> eq
Array dimensions must match for binary array op.
aa==b
ans =
1 0 0 0
c==b %單個字符和字符串之間可以用==比較
ans =
0 0 1 0
99.正則表達式簡單應用
stra = 'bat cat can car COAT court cut ct CAT-scan';
regexpi(stra, 'c[aeiou]+t') %匹配c*t模式的字符串,其中*是aeiou中的某一個字符
ans =
5 17 28 35
strb = {'Madrid, Spain' 'Romeo and Juliet' 'MATLAB is great'};
s1 = regexp(strb, '[A-Z]'); %匹配大寫字母
s2 = regexp(strb, '\s'); %匹配空格字符
celldisp(s1)
s1{1} =
1 9
s1{2} =
1 11
s1{3} =
1 2 3 4 5 6
celldisp(s2)
s2{1} =
8
s2{2} =
6 10
s2{3} =
7 10
100.字符串的比較
a='abcde';
b='abcgh';
c='abcDE';
d='aBCde';
strcmp(a,b)
ans =
0
strncmp(a,b,3)
ans =
1
strcmp(a,c) %strcmp比較,有大小寫差別
ans =
0
strcmpi(a,c) %ctrcmpi比較時,忽略大小寫差別
ans =
1
strncmp(b,d,3)
ans =
0
strncmpi(b,d,3)
ans =
1
101.字符串的查找
a='Welcome to MATLAB!';
b='Welcome,Sir!';
c='Welcome';
ab=strcat(a,b)
strfind(ab,c) %strfind中長字符串在前,否則返回空數組
strfind(c,ab)
findstr(ab,c) %findstr中兩個字符串位置可變
findstr(c,ab)
strmatch(a,c) %strmatch中長字符串應在後
strmatch(c,a)
strmatch(c,a,'exact')
[wel1,rem1]=strtok(ab,'!')
[wel2,rem2]=strtok(rem1,'!')
ab =
Welcome to MATLAB!Welcome,Sir!
ans =
1 19
ans =
[]
ans =
1 19
ans =
1 19
ans =
[]
ans =
1
ans =
[]
wel1 =
Welcome to MATLAB
rem1 =
!Welcome,Sir!
wel2 =
Welcome,Sir
rem2 =
!
102.字符串的創建
a=' a';b='b b';c='c '; %用單引號創建字符串,都由四個字符組成
length(a),length(b),length(c) %獲取字符串長度
ABC=[a b c] %用方括號連接三個字符串,空格不被裁切
length(ABC)
sAC=strcat(a,c) %用strcat函數連接字符串,每個字符串最右邊的空格被裁切
length(sAC)
AC=[a,c]
length(AC)
whos %顯示工作區內所有變量,每一個字符佔用兩個字節
ans =
4
ans =
4
ans =
4
ABC =
ab bc
ans =
12
sAC =
ac
ans =
5
AC =
ac
ans =
8
Name Size Bytes Class
ABC 1x12 24 char array
AC 1x8 16 char array
a 1x4 8 char array
ans 1x1 8 double array
b 1x4 8 char array
c 1x4 8 char array
sAC 1x5 10 char array
Grand total is 38 elements using 82 bytes
103.字符串的替換
str='Error Msg';
newstr=strrep(str,'Msg','Mail')
newstrr=strrep(str,'msg','Mail') %strrep對大小寫敏感,替換沒有進行
newstr =
Error Mail
newstrr =
Error Msg
104.字符串元胞數組的操作
a={' a','bbbb';'c ',' dd '}
b={'tt ',' tt';' bb','bb '}
strcat(a,b)
strncmp(a,b,2)
c=strcat(a,b)
strrep(c,'tt','TT')
deblank(c)
strtrim(c)
upper(c)
strjust(c,'left') '
iscellstr(c)
a =
' a' 'bbbb'
'c ' ' dd '
b =
'tt ' ' tt'
' bb' 'bb '
ans =
' att ' 'bbbb tt'
'c bb' ' dd bb '
ans =
0 0
0 0
c =
' att ' 'bbbb tt'
'c bb' ' dd bb '
ans =
' aTT ' 'bbbb TT'
'c bb' ' dd bb '
ans =
' att' 'bbbb tt'
'c bb' ' dd bb'
ans =
'att' 'bbbb tt'
'c bb' 'dd bb'
ans =
' ATT ' 'BBBB TT'
'C BB' ' DD BB '
??? strjust(c,'left') '
|
Error: Missing variable or function.
105.字符歸屬測試函數
a='Building 17#';
isletter(a)
isspace(a)
ans =
1 1 1 1 1 1 1 1 0 0 0 0
ans =
0 0 0 0 0 0 0 0 1 0 0 0
106.字符數組的格式操作
a=strvcat('Welcome','to','MathWorks')
upper(a)
strjust(a,'center')
sort(a(3,:)) %對字符數組a的第三行字符串,按照升序排列
sort(a) %對字符數組a的每一列,按照字符升序排列
sort(a,2) %對字符數組a的每一行,按照字符升序排列
sort(a,2,'descend') %對字符數組a的每一行,按照字符降序排列
a =
Welcome
to
MathWorks
ans =
WELCOME
TO
MATHWORKS
ans =
Welcome
to
MathWorks
ans =
MWahkorst
ans =
Ma
WelcWme
tothoorks
ans =
Wceelmo
ot
MWahkorst
ans =
omleecW
to
tsrokhaWM
107.字符數組和字符串的元胞數組之間的轉換
data = ['Allison Jones';'Development ';'Phoenix '];
celldata = cellstr(data)
length(celldata{3})
iscellstr(celldata) %測試變量是否屬於字符串的元胞數組
strings = char(celldata)
length(strings(3,:))
celldata =
'Allison Jones'
'Development'
'Phoenix'
ans =
7
ans =
1
strings =
Allison Jones
Development
Phoenix
ans =
13