在MATLAB中創(chuàng)建一個(gè)字符串非常簡(jiǎn)單。 事實(shí)上,在前面的示例中我們已經(jīng)使用了很多次。 例如,在命令提示符下鍵入以下內(nèi)容:
my_string = 'Yiibai Yiibai'
MATLAB執(zhí)行上述語句并返回以下結(jié)果 -
Trial>> my_string = 'Yiibai Yiibai'
my_string =
'Yiibai Yiibai'
MATLAB將所有變量視為數(shù)組,并將字符串視為字符數(shù)組。使用whos
命令來檢查上面創(chuàng)建的變量 -
Trial>> whos
Name Size Bytes Class Attributes
ans 1x92 184 char
my_string 1x16 32 char
x 1x3 360 cell
有趣的是,可以使用uint8
或uint16
等數(shù)字轉(zhuǎn)換函數(shù)將字符串中的字符轉(zhuǎn)換為數(shù)字代碼。 char
函數(shù)將整數(shù)向量轉(zhuǎn)換回到字符 -
示例
創(chuàng)建腳本文件并在其中鍵入以下代碼 -
my_string = 'Yiibai''s Tutorial';
str_ascii = uint8(my_string) % 8-bit ascii values
str_back_to_char= char(str_ascii)
str_16bit = uint16(my_string) % 16-bit ascii values
str_back_to_char = char(str_16bit)
執(zhí)行上面示例代碼,得到以下結(jié)果 -
str_ascii =
1×17 uint8 行向量
1 至 15 列
89 105 105 98 97 105 39 115 32 84 117 116 111 114 105
16 至 17 列
97 108
str_back_to_char =
'Yiibai's Tutorial'
str_16bit =
1×17 uint16 行向量
1 至 15 列
89 105 105 98 97 105 39 115 32 84 117 116 111 114 105
16 至 17 列
97 108
str_back_to_char =
'Yiibai's Tutorial'
到目前為止,我們討論的字符串是一維字符數(shù)組; 然而,我們需要存儲(chǔ)更多維度的數(shù)據(jù)。在程序中存儲(chǔ)更多的維度文本數(shù)據(jù)。這是通過創(chuàng)建矩形字符數(shù)組來實(shí)現(xiàn)的。
創(chuàng)建矩形字符陣列的最簡(jiǎn)單的方式是根據(jù)需要垂直或水平連接兩個(gè)或更多個(gè)一維字符數(shù)組。
通過以下任一方式垂直組合字符串 -
使用MATLAB連接運(yùn)算符[]
并用分號(hào)(;
)分隔每一行。 請(qǐng)注意,在這種方法中,每行必須包含相同數(shù)量的字符。對(duì)于不同長(zhǎng)度的字符串,應(yīng)該根據(jù)需要填充空格字符。
使用char
函數(shù)。如果字符串的長(zhǎng)度不同,則char
將較短的字符串填充到尾部空白處,以使每行具有相同的字符數(shù)。
創(chuàng)建腳本文件并在其中鍵入以下代碼 -
doc_profile = ['Bara Tli '; ...
'Sr. Surgeon '; ...
'R N Tagore Cardiology Research Center']
doc_profile = char('Bara Tli', 'Sr. Surgeon', ...
'RN Tagore Cardiology Research Center')
運(yùn)行文件時(shí),會(huì)顯示以下結(jié)果 -
Trial>> doc_profile = ['Bara Tli '; ...
'Sr. Surgeon '; ...
'R N Tagore Cardiology Research Center']
doc_profile = char('Bara Tli', 'Sr. Surgeon', ...
'RN Tagore Cardiology Research Center')
doc_profile =
3×37 char 數(shù)組
'Bara Tli '
'Sr. Surgeon '
'R N Tagore Cardiology Research Center'
doc_profile =
3×36 char 數(shù)組
'Bara Tli '
'Sr. Surgeon '
'RN Tagore Cardiology Research Center'
可以通過以下任一方式水平組合字符串 -
[]
并用逗號(hào)(;
)或空格分隔輸入字符串。該方法保留輸入數(shù)組中的任何尾隨空格。strcat
。 此方法會(huì)刪除輸入中的尾隨空格。示例
創(chuàng)建腳本文件并在其中鍵入以下代碼 -
name = 'Myra Yli ';
position = 'Sr. Surgeon ';
worksAt = 'R N Tagore Cardiology Research Center';
profile = [name ', ' position ', ' worksAt]
profile = strcat(name, ', ', position, ', ', worksAt)
執(zhí)行上面示例代碼,得到以下結(jié)果 -
Trial>> name = 'Myra Yli ';
position = 'Sr. Surgeon ';
worksAt = 'R N Tagore Cardiology Research Center';
profile = [name ', ' position ', ' worksAt]
profile = strcat(name, ', ', position, ', ', worksAt)
profile =
'Myra Yli , Sr. Surgeon , R N Tagore Cardiology Research Center'
profile =
'Myra Yli,Sr. Surgeon,R N Tagore Cardiology Research Center'
從前面的學(xué)習(xí)中,很明顯,組合不同長(zhǎng)度的字符串可能會(huì)很痛苦,因?yàn)閿?shù)組中的所有字符串都必須具有相同的長(zhǎng)度。在字符串的末尾使用了空格,使其長(zhǎng)度相等。
然而,組合字符串的更有效的方法是將生成的數(shù)組轉(zhuǎn)換為單元格數(shù)組。
MATLAB單元格數(shù)組可以在數(shù)組中保存不同大小和類型的數(shù)據(jù)。單元格數(shù)組提供了一種更靈活的方法來存儲(chǔ)不同長(zhǎng)度的字符串。
cellstr
函數(shù)將字符數(shù)組轉(zhuǎn)換為字符串的單元格數(shù)組。
創(chuàng)建腳本文件并在其中鍵入以下代碼 -
name = 'Myra Tli ';
position = 'St. Sargeon ';
worksAt = 'R N Tagore Cardiology Research Center';
profile = char(name, position, worksAt);
profile = cellstr(profile);
disp(profile)
執(zhí)行上面示例代碼,得到以下結(jié)果 -
Trial>> name = 'Myra Tli ';
position = 'St. Sargeon ';
worksAt = 'R N Tagore Cardiology Research Center';
profile = char(name, position, worksAt);
profile = cellstr(profile);
disp(profile)
'Myra Tli'
'St. Sargeon'
'R N Tagore Cardiology Research Center'
MATLAB提供了許多字符串函數(shù)來創(chuàng)建,組合,解析,比較和操作字符串。
下表簡(jiǎn)要介紹了MATLAB中的字符串函數(shù)。
用于存儲(chǔ)字符數(shù)組中的文本,組合字符數(shù)組等的函數(shù) -
函數(shù) | 描述 |
---|---|
blanks |
創(chuàng)建空白字符串 |
cellstr |
從字符數(shù)組創(chuàng)建字符串?dāng)?shù)組 |
char |
轉(zhuǎn)換為字符數(shù)組(字符串) |
iscellstr |
確定輸入是字符串的單元格數(shù)組 |
ischar |
確定項(xiàng)目是否是字符數(shù)組 |
sprintf |
將數(shù)據(jù)格式化為字符串 |
strcat |
水平連接字符串 |
strjoin |
將單元格中的字符串連接到單個(gè)字符串中 |
識(shí)別字符串部分,查找和替換子串的函數(shù) -
函數(shù) | 描述 |
---|---|
ischar |
確定項(xiàng)目是否是字符數(shù)組 |
isletter |
數(shù)組元素是否為字母 |
isspace |
數(shù)組元素是空格 |
isstrprop |
確定字符串是否是指定的類別 |
sscanf |
從字符串讀取格式化數(shù)據(jù) |
strfind |
在另一個(gè)字符串中查找一個(gè)字符串 |
strrep |
查找并替換子串 |
strsplit |
在指定的分隔符處拆分字符串 |
strtok |
字符串的選定部分 |
validatestring |
檢查文本字符串的有效性 |
symvar |
確定表達(dá)式中的符號(hào)變量 |
regexp |
匹配正則表達(dá)式(區(qū)分大小寫) |
regexpi |
匹配正則表達(dá)式(不區(qū)分大小寫) |
regexprep |
用正則表達(dá)式替換字符串 |
regexptranslate |
用正則表達(dá)式替換字符串 |
字符串比較的函數(shù) -
函數(shù) | 描述 |
---|---|
strcmp |
比較字符串(區(qū)分大小寫) |
strcmpi |
比較字符串(不區(qū)分大小寫) |
strncmp |
比較字符串的前n 個(gè)字符(區(qū)分大小寫) |
strncmpi |
比較字符串的前n 個(gè)字符(不區(qū)分大小寫) |
將字符串更改為大寫或小寫,創(chuàng)建或刪除空格的函數(shù) -
函數(shù) | 描述 |
---|---|
deblank |
從字符串末尾剝?nèi)ノ搽S空格 |
strtrim |
從字符串中刪除前導(dǎo)和尾隨的空格 |
lower |
將字符串轉(zhuǎn)換為小寫 |
upper |
將字符串轉(zhuǎn)換為大寫字母 |
strjust |
對(duì)齊字符數(shù)組 |
以下示例說明了一些上述字符串函數(shù) -
格式化字符串
創(chuàng)建腳本文件并在其中鍵入以下代碼 -
A = pi*1000*ones(1,5);
sprintf(' %f \n %.2f \n %+.2f \n %12.2f \n %012.2f \n', A)
執(zhí)行上面示例代碼,得到以下結(jié)果 -
ans = 3141.592654
3141.59
+3141.59
3141.59
000003141.59
字符串連接
創(chuàng)建腳本文件并在其中鍵入以下代碼 -
%cell array of strings
str_array = {'red','blue','green', 'yellow', 'orange'};
% Join strings in cell array into single string
str1 = strjoin(str_array, "-")
str2 = strjoin(str_array, ",")
執(zhí)行上面示例代碼,得到以下結(jié)果 -
str1 = red-blue-green-yellow-orange
str2 = red,blue,green,yellow,orange
查找和替換字符串
創(chuàng)建腳本文件并在其中鍵入以下代碼 -
students = {'Bara Ali', 'Neha Bhatnagar', ...
'Nonica Malik', 'Madhu Gautam', ...
'Nadhu Sharma', 'Bhawna Sharma',...
'Muha Ali', 'Reva Dutta', ...
'Tunaina Ali', 'Sofia Kabir'};
% The strrep function searches and replaces sub-string.
new_student = strrep(students(8), 'Reva', 'Poulomi')
% Display first names
first_names = strtok(students)
執(zhí)行上面示例代碼,得到以下結(jié)果 -
Trial>> students = {'Bara Ali', 'Neha Bhatnagar', ...
'Nonica Malik', 'Madhu Gautam', ...
'Nadhu Sharma', 'Bhawna Sharma',...
'Muha Ali', 'Reva Dutta', ...
'Tunaina Ali', 'Sofia Kabir'};
% The strrep function searches and replaces sub-string.
new_student = strrep(students(8), 'Reva', 'Poulomi')
% Display first names
first_names = strtok(students)
new_student =
1×1 cell 數(shù)組
{'Poulomi Dutta'}
first_names =
1×10 cell 數(shù)組
1 至 7 列
{'Bara'} {'Neha'} {'Nonica'} {'Madhu'} {'Nadhu'} {'Bhawna'} {'Muha'}
8 至 10 列
{'Reva'} {'Tunaina'} {'Sofia'}
比較字符串
創(chuàng)建腳本文件并在其中鍵入以下代碼 -
str1 = 'This is test'
str2 = 'This is text'
if (strcmp(str1, str2))
sprintf('%s and %s are equal', str1, str2)
else
sprintf('%s and %s are not equal', str1, str2)
end
執(zhí)行上面示例代碼,得到以下結(jié)果 -
str1 = This is test
str2 = This is text
ans = This is test and This is text are not equal