# Matlab字符串

``my_string = 'Yiibai Yiibai'``

MATLAB執行上述語句並返回以下結果 -

``````Trial>> my_string = 'Yiibai Yiibai'

my_string =

'Yiibai Yiibai'``````

MATLAB將所有變量視爲數組，並將字符串視爲字符數組。使用`whos`命令來檢查上面創建的變量 -

``````
Trial>> whos
Name           Size            Bytes  Class    Attributes

ans            1x92              184  char
my_string      1x16               32  char
x              1x3               360  cell``````

``````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)``````

``````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'``````

## 矩形字符數組

• 使用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提供了許多字符串函數來創建，組合，解析，比較和操作字符串。

`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`

`strncmpi`

`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', ...
'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', ...
'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 列

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``````