在MATLAB中創建一個字串非常簡單。 事實上,在前面的示例中我們已經使用了很多次。 例如,在命令提示符下鍵入以下內容:
my_string = 'zaixian zaixian'
MATLAB執行上述語句並返回以下結果 -
Trial>> my_string = 'zaixian zaixian'
my_string =
'zaixian zaixian'
MATLAB將所有變數視為數組,並將字串視為字元數組。使用whos
命令來檢查上面創建的變數 -
Trial>> whos
Name Size Bytes Class Attributes
ans 1x92 184 char
my_string 1x16 32 char
x 1x3 360 cell
有趣的是,可以使用uint8
或uint16
等數字轉換函數將字串中的字元轉換為數字代碼。 char
函數將整數向量轉換回到字元 -
示例
創建腳本檔並在其中鍵入以下代碼 -
my_string = 'zaixian''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)
執行上面示例代碼,得到以下結果 -
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 =
'zaixian'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 =
'zaixian's Tutorial'
矩形字元數組
到目前為止,我們討論的字串是一維字元數組; 然而,我們需要存儲更多維度的數據。在程式中存儲更多的維度文本數據。這是通過創建矩形字元數組來實現的。
創建矩形字元陣列的最簡單的方式是根據需要垂直或水準連接兩個或更多個一維字元數組。
通過以下任一方式垂直組合字串 -
使用MATLAB連接運算符
[]
並用分號(;
)分隔每一行。 請注意,在這種方法中,每行必須包含相同數量的字元。對於不同長度的字串,應該根據需要填充空格字元。使用
char
函數。如果字串的長度不同,則char
將較短的字串填充到尾部空白處,以使每行具有相同的字元數。
示例
創建腳本檔並在其中鍵入以下代碼 -
doc_profile = ['Bara Tli '; ...
'Sr. Surgeon '; ...
'R N Tagore Cardiology Research Center']
doc_profile = char('Bara Tli', 'Sr. Surgeon', ...
'RN Tagore Cardiology Research Center')
運行檔時,會顯示以下結果 -
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 數組
'Bara Tli '
'Sr. Surgeon '
'R N Tagore Cardiology Research Center'
doc_profile =
3×36 char 數組
'Bara Tli '
'Sr. Surgeon '
'RN Tagore Cardiology Research Center'
可以通過以下任一方式水準組合字串 -
- 使用MATLAB連接運算符 -
[]
並用逗號(;
)或空格分隔輸入字串。該方法保留輸入數組中的任何尾隨空格。 - 使用字串連接函數 -
strcat
。 此方法會刪除輸入中的尾隨空格。
示例
創建腳本檔並在其中鍵入以下代碼 -
name = 'Myra Yli ';
position = 'Sr. Surgeon ';
worksAt = 'R N Tagore Cardiology Research Center';
profile = [name ', ' position ', ' worksAt]
profile = strcat(name, ', ', position, ', ', worksAt)
執行上面示例代碼,得到以下結果 -
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'
將字串組合成單元格數組
從前面的學習中,很明顯,組合不同長度的字串可能會很痛苦,因為數組中的所有字串都必須具有相同的長度。在字串的末尾使用了空格,使其長度相等。
然而,組合字串的更有效的方法是將生成的數組轉換為單元格數組。
MATLAB單元格數組可以在數組中保存不同大小和類型的數據。單元格數組提供了一種更靈活的方法來存儲不同長度的字串。
cellstr
函數將字元數組轉換為字串的單元格數組。
示例
創建腳本檔並在其中鍵入以下代碼 -
name = 'Myra Tli ';
position = 'St. Sargeon ';
worksAt = 'R N Tagore Cardiology Research Center';
profile = char(name, position, worksAt);
profile = cellstr(profile);
disp(profile)
執行上面示例代碼,得到以下結果 -
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中的字串函數
MATLAB提供了許多字串函數來創建,組合,解析,比較和操作字串。
下表簡要介紹了MATLAB中的字串函數。
用於存儲字元數組中的文本,組合字元數組等的函數 -
函數 | 描述 |
---|---|
blanks |
創建空白字串 |
cellstr |
從字元數組創建字串數組 |
char |
轉換為字元數組(字串) |
iscellstr |
確定輸入是字串的單元格數組 |
ischar |
確定專案是否是字元數組 |
sprintf |
將數據格式化為字串 |
strcat |
水準連接字串 |
strjoin |
將單元格中的字串連接到單個字串中 |
識別字串部分,查找和替換子串的函數 -
函數 | 描述 |
---|---|
ischar |
確定專案是否是字元數組 |
isletter |
數組元素是否為字母 |
isspace |
數組元素是空格 |
isstrprop |
確定字串是否是指定的類別 |
sscanf |
從字串讀取格式化數據 |
strfind |
在另一個字串中查找一個字串 |
strrep |
查找並替換子串 |
strsplit |
在指定的分隔符號處拆分字串 |
strtok |
字串的選定部分 |
validatestring |
檢查文本字串的有效性 |
symvar |
確定運算式中的符號變數 |
regexp |
匹配正則運算式(區分大小寫) |
regexpi |
匹配正則運算式(不區分大小寫) |
regexprep |
用正則運算式替換字串 |
regexptranslate |
用正則運算式替換字串 |
字串比較的函數 -
函數 | 描述 |
---|---|
strcmp |
比較字串(區分大小寫) |
strcmpi |
比較字串(不區分大小寫) |
strncmp |
比較字串的前n 個字元(區分大小寫) |
strncmpi |
比較字串的前n 個字元(不區分大小寫) |
將字串更改為大寫或小寫,創建或刪除空格的函數 -
函數 | 描述 |
---|---|
deblank |
從字串末尾剝去尾隨空格 |
strtrim |
從字串中刪除前導和尾隨的空格 |
lower |
將字串轉換為小寫 |
upper |
將字串轉換為大寫字母 |
strjust |
對齊字元數組 |
例子
以下示例說明了一些上述字串函數 -
格式化字串
創建腳本檔並在其中鍵入以下代碼 -
A = pi*1000*ones(1,5);
sprintf(' %f \n %.2f \n %+.2f \n %12.2f \n %012.2f \n', A)
執行上面示例代碼,得到以下結果 -
ans = 3141.592654
3141.59
+3141.59
3141.59
000003141.59
字串連接
創建腳本檔並在其中鍵入以下代碼 -
%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, ",")
執行上面示例代碼,得到以下結果 -
str1 = red-blue-green-yellow-orange
str2 = red,blue,green,yellow,orange
查找和替換字串
創建腳本檔並在其中鍵入以下代碼 -
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)
執行上面示例代碼,得到以下結果 -
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 數組
{'Poulomi Dutta'}
first_names =
1×10 cell 數組
1 至 7 列
{'Bara'} {'Neha'} {'Nonica'} {'Madhu'} {'Nadhu'} {'Bhawna'} {'Muha'}
8 至 10 列
{'Reva'} {'Tunaina'} {'Sofia'}
比較字串
創建腳本檔並在其中鍵入以下代碼 -
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
執行上面示例代碼,得到以下結果 -
str1 = This is test
str2 = This is text
ans = This is test and This is text are not equal