📅  最后修改于: 2020-11-03 09:51:34             🧑  作者: Mango
创建一个是在MATLAB很简单。实际上,我们已经使用了很多次。例如,您在命令提示符下键入以下内容-
my_string = 'Tutorials Point'
MATLAB将执行上述语句并返回以下结果-
my_string = Tutorials Point
MATLAB将所有变量视为数组,而字符串则视为字符数组。让我们使用whos命令检查上面创建的变量-
whos
MATLAB将执行上述语句并返回以下结果-
Name Size Bytes Class Attributes
my_string 1x16 32 char
有趣的是,你可以像使用UINT8或UINT16数值转换函数的字符的字符串转换自己的数字代码英寸char函数将整数向量转换回字符-
创建一个脚本文件并在其中键入以下代码-
my_string = 'Tutorial''s Point';
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 =
84 117 116 111 114 105 97 108 39 115 32 80 111 105 110 116
str_back_to_char = Tutorial's Point
str_16bit =
84 117 116 111 114 105 97 108 39 115 32 80 111 105 110 116
str_back_to_char = Tutorial's Point
到目前为止,我们讨论的字符串是一维字符数组。但是,我们需要存储更多。我们需要在程序中存储更多维度的文本数据。这是通过创建矩形字符数组来实现的。
创建矩形字符数组的最简单方法是根据需要垂直或水平连接两个或多个一维字符数组。
您可以通过以下两种方式垂直组合字符串-
使用MATLAB串联运算符[],并用分号(;)分隔每一行。请注意,在此方法中,每一行必须包含相同数量的字符。对于不同长度的字符串,应根据需要使用空格字符填充。
使用char函数。如果字符串的长度不同,则用较短的字符串末尾用空格填充字符,以使每行具有相同数量的字符。
创建一个脚本文件并在其中键入以下代码-
doc_profile = ['Zara Ali '; ...
'Sr. Surgeon '; ...
'R N Tagore Cardiology Research Center']
doc_profile = char('Zara Ali', 'Sr. Surgeon', ...
'RN Tagore Cardiology Research Center')
运行文件时,它显示以下结果-
doc_profile =
Zara Ali
Sr. Surgeon
R N Tagore Cardiology Research Center
doc_profile =
Zara Ali
Sr. Surgeon
RN Tagore Cardiology Research Center
您可以通过以下两种方式水平组合字符串-
使用MATLAB串联运算符[],并用逗号或空格分隔输入字符串。此方法保留输入数组中的所有尾随空格。
使用字符串连接函数strcat 。此方法删除输入中的尾随空格。
创建一个脚本文件并在其中键入以下代码-
name = 'Zara Ali ';
position = 'Sr. Surgeon ';
worksAt = 'R N Tagore Cardiology Research Center';
profile = [name ', ' position ', ' worksAt]
profile = strcat(name, ', ', position, ', ', worksAt)
运行文件时,它显示以下结果-
profile = Zara Ali , Sr. Surgeon , R N Tagore Cardiology Research Center
profile = Zara Ali,Sr. Surgeon,R N Tagore Cardiology Research Center
从前面的讨论中可以明显看出,合并长度不同的字符串可能会很麻烦,因为数组中的所有字符串都必须具有相同的长度。我们在字符串的末尾使用空格来使它们的长度相等。
但是,组合字符串的更有效方法是将结果数组转换为单元格数组。
MATLAB单元数组可以在数组中保存不同大小和类型的数据。单元阵列提供了一种更灵活的方式来存储长度可变的字符串。
cellstr函数将字符数组转换为字符串的单元格数组。
创建一个脚本文件并在其中键入以下代码-
name = 'Zara Ali ';
position = 'Sr. Surgeon ';
worksAt = 'R N Tagore Cardiology Research Center';
profile = char(name, position, worksAt);
profile = cellstr(profile);
disp(profile)
运行文件时,它显示以下结果-
{
[1,1] = Zara Ali
[2,1] = Sr. Surgeon
[3,1] = R N Tagore Cardiology Research Center
}
MATLAB提供了许多字符串函数,用于创建,组合,解析,比较和处理字符串。
下表简要介绍了MATLAB中的字符串函数-
Function | Purpose |
---|---|
Functions for storing text in character arrays, combine character arrays, etc. | |
blanks | Create string of blank characters |
cellstr | Create cell array of strings from character array |
char | Convert to character array (string) |
iscellstr | Determine whether input is cell array of strings |
ischar | Determine whether item is character array |
sprintf | Format data into string |
strcat | Concatenate strings horizontally |
strjoin | Join strings in cell array into single string |
Functions for identifying parts of strings, find and replace substrings | |
ischar | Determine whether item is character array |
isletter | Array elements that are alphabetic letters |
isspace | Array elements that are space characters |
isstrprop | Determine whether string is of specified category |
sscanf | Read formatted data from string |
strfind | Find one string within another |
strrep | Find and replace substring |
strsplit | Split string at specified delimiter |
strtok | Selected parts of string |
validatestring | Check validity of text string |
symvar | Determine symbolic variables in expression |
regexp | Match regular expression (case sensitive) |
regexpi | Match regular expression (case insensitive) |
regexprep | Replace string using regular expression |
regexptranslate | Translate string into regular expression |
Functions for string comparison | |
strcmp | Compare strings (case sensitive) |
strcmpi | Compare strings (case insensitive) |
strncmp | Compare first n characters of strings (case sensitive) |
strncmpi | Compare first n characters of strings (case insensitive) |
Functions for changing string to upper- or lowercase, creating or removing white space | |
deblank | Strip trailing blanks from end of string |
strtrim | Remove leading and trailing white space from string |
lower | Convert string to lowercase |
upper | Convert string to uppercase |
strjust | Justify character array |
以下示例说明了上述一些字符串函数-
创建一个脚本文件并在其中键入以下代码-
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 = {'Zara Ali', 'Neha Bhatnagar', ...
'Monica Malik', 'Madhu Gautam', ...
'Madhu Sharma', 'Bhawna Sharma',...
'Nuha Ali', 'Reva Dutta', ...
'Sunaina 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] = Poulomi Dutta
}
first_names =
{
[1,1] = Zara
[1,2] = Neha
[1,3] = Monica
[1,4] = Madhu
[1,5] = Madhu
[1,6] = Bhawna
[1,7] = Nuha
[1,8] = Reva
[1,9] = Sunaina
[1,10] = 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