4.1.1 字符串入門
【 * 例 4.1.1 -1 】先請讀者實(shí)際操作本例,以體會(huì)數(shù)值量與字符串的區(qū)別。
clear % 清除所有內(nèi)存變量
a=12345.6789 % 給變量 a 賦數(shù)值標(biāo)量
class(a) % 對變量 a 的類別進(jìn)行判斷
a_s=size(a) % 數(shù)值數(shù)組 a 的“大小”
a =
1.2346e+004
ans =
double
a_s =
1 1
b='S' % 給變量 b 賦字符標(biāo)量(即單個(gè)字符)
class(b) % 對變量 b 的類別進(jìn)行判斷
b_s=size(b) % 符號(hào)數(shù)組 b 的“大小”
b =
S
ans =
char
b_s =
1 1
whos % 觀察變量 a,b 在內(nèi)存中所占字節(jié)
Name Size Bytes Class
a 1x1 8 double array
a_s 1x2 16 double array
ans 1x4 8 char array
b 1x1 2 char array
b_s 1x2 16 double array
Grand total is 10 elements using 50 bytes
4.1.2 串?dāng)?shù)組的屬性和標(biāo)識(shí)
【 * 例 4.1.2 -1 】本例演示:串的基本屬性、標(biāo)識(shí)和簡單操作。
(1)創(chuàng)建串?dāng)?shù)組
a='This is an example.'
a =
This is an example.
(2)串?dāng)?shù)組 a 的大小
size(a)
ans =
1 19
(3)串?dāng)?shù)組的元素標(biāo)識(shí)
a14=a(1:4) % 提出一個(gè)子字符串
ra=a(end:-1:1) % 字符串的倒排
a14 =
This
ra =
.elpmaxe na si sihT
(4)串?dāng)?shù)組的 ASCII 碼
ascii_a=double(a) % 產(chǎn)生 ASCII 碼
ascii_a =
Columns 1 through 12
84 104 105 115 32 105 115 32 97 110 32 101
Columns 13 through 19
120 97 109 112 108 101 46
char(ascii_a) % 把 ASCII 碼變回字符串
ans =
This is an example.
(5)對字符串 ASCII 碼數(shù)組的操作
% 使字符串中字母全部大寫
w=find(a>='a'&a<='z'); % 找出串?dāng)?shù)組 a 中,小寫字母的元素位置。
ascii_a(w)=ascii_a(w)-32; % 大小寫字母 ASCII 值差 32. 用數(shù)值加法改變部分碼值。
char(ascii_a) % 把新的 ASCII 碼翻成字符
ans =
THIS IS AN EXAMPLE.
(6)中文字符串?dāng)?shù)組
A=' 這是一個(gè)算例。 '; % 創(chuàng)建中文字符串
A_s=size(A) % 串?dāng)?shù)組的大小
A56=A([5 6]) % 取串的子數(shù)組
ASCII_A=double(A) % 獲取 ASCII 碼
A_s =
1 7
A56 =
算例
ASCII_A =
Columns 1 through 6
54754 51911 53947 47350 52195 49405
Column 7
41379
char(ASCII_A) % 把 ASCII 碼翻譯成字符
ans =
這是一個(gè)算例。
(7)創(chuàng)建帶單引號(hào)的字符串
b='Example '' 4.1.2 -1'''
b =
Example ' 4.1.2 -1'
(8)由小串構(gòu)成長串
ab=[a(1:7),' ',b,' .'] % 這里第 2 個(gè)輸入為空格串
ab =
This is Example ' 4.1.2 -1' .
4.1.3 復(fù)雜串?dāng)?shù)組的創(chuàng)建
4.1.3.1 多行串?dāng)?shù)組的直接創(chuàng)建
【 * 例 4.1.3 .1-1 】多行串?dāng)?shù)組的直接輸入示例。
clear
S=['This string array '
'has multiple rows.']
S =
This string array
has multiple rows.
size(S)
ans =
18
4.1.3.2 利用串操作函數(shù)創(chuàng)建多行串?dāng)?shù)組
【 * 例 4.1.3 .2-1 】演示:用專門函數(shù) char , str2mat , strvcat 創(chuàng)建多行串?dāng)?shù)組示例。
S1=char('This string array','has two rows.')
S1 =
This string array
has two rows.
S2=str2mat(' 這 ',' 字符 ',' 串?dāng)?shù)組 ',' 由 4 行組成 ')
S2 =
這
字符
串?dāng)?shù)組
由4 行組成
S3=strvcat(' 這 ',' 字符 ',' 串?dāng)?shù)組 ',' ',' 由 4 行組成 ')% “空串”會(huì)產(chǎn)生一個(gè)空格行
S3 =
這
字符
串?dāng)?shù)組
由 4 行組成
size(S3)
ans =
5 5
【 * 例 4.1.3 .2-1 】的補(bǔ)充
(1) 創(chuàng)建一個(gè)二維字符數(shù)組animal
>> Animal=[‘dog’;’monkey’];
??? Error using ==> vertcat
CAT arguments dimensions are not consistent.
>> Animal=['dog ';'monkey']; %創(chuàng)建成功
說明:創(chuàng)建二維字符數(shù)組時(shí),字符數(shù)組要求每行字符含有相同的列。當(dāng)多行字符串具有不同長度時(shí),為了避免出現(xiàn)錯(cuò)誤,用戶需要在較短的字符串中添加空格,以便保證較短字符串與最長字符串等長。
(2) 用char函數(shù)創(chuàng)建字符數(shù)組,該方法不需要所有字符串等長
>> Animal = char(‘dog’,’monkey’);
4.1.3.3 轉(zhuǎn)換函數(shù)產(chǎn)生數(shù)碼字符串
【 * 例 4.1.3 .3-1 】最常用的數(shù)組 / 字符串轉(zhuǎn)換函數(shù) int2str , num2str , mat2str 示例。
(1) int2str 把整數(shù)數(shù)組轉(zhuǎn)換成串?dāng)?shù)組(非整數(shù)將被四舍五入園整后再轉(zhuǎn)換)
A=eye(2,4); % 生成一個(gè)
數(shù)值數(shù)組
A_str1=int2str(A) % 轉(zhuǎn)換成
串?dāng)?shù)組。請讀者自己用 size 檢驗(yàn)。
A_str1 =
1 0 0 0
0 1 0 0
(2) num2str 把非整數(shù)數(shù)組轉(zhuǎn)換為串?dāng)?shù)組(常用于圖形中,數(shù)據(jù)點(diǎn)的標(biāo)識(shí))
rand('state',0)
B=rand(2,4); % 生成數(shù)值矩陣
B3=num2str(B,3) % 保持 3 位有效數(shù)字,轉(zhuǎn)換為串
B3 =
0.95 0.607 0.891 0.456
0.231 0.486 0.762 0.0185
(3) mat2str 把數(shù)值數(shù)組轉(zhuǎn)換成輸入形態(tài)的串?dāng)?shù)組(常與 eval 指令配用)
B_str=mat2str(B,4) % 保持 4 位有效數(shù)字,轉(zhuǎn)換為“數(shù)組輸入形式”串
B_str =
[0.9501 0.6068 0.8913 0.4565;0.2311 0.486 0.7621 0.0185]
Expression=['exp(-',B_str,')']; % 相當(dāng)于指令窗寫一個(gè)表達(dá)式 exp(-B_str)
eval(Expression) % 把 exp(-B_str) 送去執(zhí)行
ans =
0.3867 0.5451 0.4101 0.6335
0.7937 0.6151 0.4667 0.9817
【 * 例 4.1.3 .3-2 】綜合例題:在 MATLAB 計(jì)算生成的圖形上標(biāo)出圖名和最大值點(diǎn)坐標(biāo)。
clear % 清除內(nèi)存中的所有變量
a=2; % 設(shè)置衰減系數(shù)
w=3; % 設(shè)置振蕩頻率
t=0:0.01:10; % 取自變量采樣數(shù)組
y=exp(-a*t).*sin(w*t); % 計(jì)算函數(shù)值,產(chǎn)生函數(shù)數(shù)組
[y_max,i_max]=max(y); % 找最大值元素位置
t_text=['t=',num2str(t(i_max))]; % 生成最大值點(diǎn)的橫坐標(biāo)字符串 <7>
y_text=['y=',num2str(y_max)]; % 生成最大值點(diǎn)的縱坐標(biāo)字符串 <8>
max_text=char('maximum',t_text,y_text);% 生成標(biāo)志最大值點(diǎn)的字符串 <9>
% 生成標(biāo)志圖名用的字符串
tit=['y=exp(-',num2str(a),'t)*sin(',num2str(w),'t)']; %<11>
plot(t,zeros(size(t)),'k') % 畫縱坐標(biāo)為 0 的基準(zhǔn)線
hold on % 保持繪制的線不被清除
plot(t,y,'b') % 用蘭色畫 y(t) 曲線
plot(t(i_max),y_max,'r.','MarkerSize',20) % 用大紅點(diǎn)標(biāo)最大值點(diǎn)
text(t(i_max)+0.3,y_max+0.05,max_text) % 在圖上書寫最大值點(diǎn)的數(shù)據(jù)值 <16>
title(tit),xlabel('t'),ylabel('y'),hold off% 書寫圖名、橫坐標(biāo)名、縱坐標(biāo)名

圖 4.1.3 .3-1 字符串運(yùn)用示意圖
4.1.3.4 利用元胞數(shù)組創(chuàng)建復(fù)雜字符串
【 * 例 4.1.3 .4-1 】元胞數(shù)組在存放和操作字符串上的應(yīng)用。
a='MATLAB 5 ';b='introduces new data types:'; % 創(chuàng)建單行字符串 a,b
c1=' ◆ Multidimensional array';c2=' ◆ User-definable data structure';
c3=' ◆ Cell arrays';c4=' ◆ Character array';
c=char(c1,c2,c3,c4); % 創(chuàng)建多行字符串 c
C={a;b;c}; % 利用元胞數(shù)組存放長短不同的字符串 <5>
disp([C{1:2}]) % 顯示前兩個(gè)元胞中的字符內(nèi)容 <6>
disp(' ') % 顯示一行空白
disp(C{3}) % 顯示第 3 個(gè)元胞中的字符內(nèi)容 <8>
MATLAB 5 introduces new data types:
◆ Multidimensional array
◆ User-definable data structure
◆ Cell arrays
◆ Character array
4.1.4 串轉(zhuǎn)換函數(shù)
【 * 例 4.1.4 -1 】 fprintf, sprintf, sscanf 的用法示例。
rand('state',0);a=rand(2,2); % 產(chǎn)生
隨機(jī)陣
s1=num2str(a) % 把數(shù)值數(shù)組轉(zhuǎn)換為串?dāng)?shù)組
s_s=sprintf('%.10e\n',a) %10 數(shù)位科學(xué)記述串 , 每寫一個(gè)元素就換行。
s1 =
0.95013 0.60684
0.23114 0.48598
s_s =
9.5012928515e-001
2.3113851357e-001
6.0684258354e-001
4.8598246871e-001
fprintf('% .5g \\',a) % 以 5 位數(shù)位最短形式顯示。不能賦值用
0.95013\0.23114\0.60684\0.48598\
s_sscan=sscanf(s_s,'%f',[3,2])% 浮點(diǎn)格式把串轉(zhuǎn)換成成
數(shù)值數(shù)組。
s_sscan =
0.9501 0.4860
0.2311 0
0.6068 0