且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

《MATLAB图像处理375例》——1.7 字符串

更新时间:2022-09-16 15:00:29

本节书摘来自异步社区《MATLAB图像处理375例》一书中的第1章,第1.7节,作者:MATLAB技术联盟著,更多章节内容可以访问云栖社区“异步社区”公众号查看

1.7 字符串

MATLAB图像处理375例
字符串或串(String)是由数字、字母、下划线组成的一串字符。字符串在数据的可视化、应用程序的交互方面起到非常重要的作用,创建字符串时需要使用单引号将字符串的内容包括起来,字符串一般以行向量形式存在,并且每一个字符占用两个字节的内存。

1.7.1 创建字符串
【例1-47】创建字符串时,只要将字符串的内容用单引号包括起来即可,例如:

a=126
class(a)
size(a)
b='125'
class(b)
size(b)

运行结果如下:

a =
  126
ans =
double
ans =
   1   1
b =
125
ans =
char
ans =
   1   3

若需要在字符串内容中包含单引号,则在输入字符串内容时,连续输入两个单引号即可,例如:

C='where are you?'

运行结果如下。

C =
where are you?

【例1-48】使用char函数创建一些无法通过键盘输入的字符,该函数的作用是将输入的整数参数转变为相应的字符。

S1=char('This string array','has two rows.') 
S2=char('我','你','串数组','','组成')

运行结果如下。

S1 =
This string array
has two rows.  
S2 =
我 
你 
串数组
组成

1.7.2 基本字符串操作
基本字符串操作主要有字符串元素索引、字符串的拼接、字符串和数值的转换。下面将进行介绍。

1.字符串元素索引
在MATLAB中,字符串实际上也是一种向量或者数组,一般利用索引操作数组的方法都可以用来操作字符串。

【例1-49】利用索引操作数组的方法用来操作字符串。

a='This is an Example!'
b=a(2:5)
c=a(11:14)
d=a(17:end)

运行结果如下:

a =
This is an Example!
b =
his 
c =
Exa
d =
le!

2.字符串的拼接
字符串可以利用“[]”运算符进行拼接,若使用“,”作为不同字符串之间的间隔,则相当于扩展字符串成为更长的字符串向量;若使用“;”作为不同字符串之间的间隔,则相当于扩展字符串成为二维或者多维的数组,这时不同行上的字符串必须具有同样的长度。

【例1-50】字符串的拼接示例。

a='Hello';
b='word!';
length(a)==length(b)
c=[a,' ',b]
d=[a;b]
size(c)
size(d)
e='word';

运行结果如下:

ans =
   1
c =
Hello word!
d =
Hello
word!
ans =
   1  11
ans =
   2   5

3.字符串和数值的转换
在MATLAB中,字符串和数值的转换可以使用char函数将数值转变为字符,也可以使用double函数将字符转变成数值。

【例1-51】字符串和数值的转换示例。

a='Hello word!';
b=double(a)
c='您是!';
d=double(c)
char(b)
char(d)

运行结果如下:

b =
  72  101  108  108  111  32  119  111  114  100  33
d =
    24744    26159    65281
ans =
Hello word!
ans =
您是!

1.7.3 字符串操作函数
在MATLAB中,字符串操作函数如表1-12所示。
《MATLAB图像处理375例》——1.7 字符串

【例1-52】deblank函数示例。

a='world!  '
deblank(a)
whos

运行结果如下:

a =
world!  
ans =
world!
 Name   Size      Bytes Class  Attributes
 a     1x9        18 char        
 ans    1x6        12 char

【例1-53】ischar函数示例。

a='world!'
ischar(a)
b=12;
ischar(b)

运行结果如下:

a =
world!
ans =
   1
ans =
   0

【例1-54】组合字符串strcat函数和strvcat函数示例。

a='Hello';
b='word!';
c=strcat(a,b)
d=strvcat(a,b ,c)
whos

运行结果如下:

c =
Helloword!
d =
Hello   
word!   
Helloword!
 Name   Size      Bytes Class   Attributes
 a     1x5        10 char         
 ans    1x1         1 logical       
 b     1x5        10 char         
 c     1x10        20 char         
 d     3x10        60 char

【例1-55】查寻索引findstr函数和strfind 函数示例。

S1='A brother in need is a brother indeed';
S2=' brother ';
a=findstr(S2,S1)
b=strfind(S2,S1)
c=strfind(S1,S2)

运行结果如下:

a =
   2  23
b =
   []
c =
   2  23

【例1-56】对齐排列字符串strjust函数示例。

a='Hello';
b='word!';
c=strcat(a,b)
d=strvcat(a,b,c)
e=strjust(d)

运行结果如下:

c =
Helloword!
d =
Hello   
word!   
Helloword!
e =
   Hello
   word!
Helloword!

【例1-57】替换字符串中的子字符strrep 函数示例。

S1='A fire in need is a fire indeed'
S2=strrep(S1,'fire','frie')

运行结果如下。

S1 =
A fire in need is a fire indeed
S2 =
A frie in need is a frie indeed

查寻匹配的字符串strmatch 函数示例。

a=strmatch('ma',strvcat('ma','minima','maimum'))
b=strmatch('ma',strvcat('ma','minima','maimum'),'exact')

运行结果如下。

a =
   1
   3
b =
   1

【例1-58】改变字符串中字符的大小写upper函数和lower 函数示例。

S1='frie';
S2=upper(S1)
S3=lower(S2)

运行结果如下。

S2 =
FRIE
S3 =
frie

1.字符串转换函数
使用不同的函数可以允许不同类型的数据和字符串类型的数据之间进行转换,在MATLAB中直接提供了相应的函数对同样类型的数据进行数制的转换,如表1-13、表1-14所示。
《MATLAB图像处理375例》——1.7 字符串
《MATLAB图像处理375例》——1.7 字符串

函数str2num在使用时需要注意:被转换的字符串仅能包含数字、小数点、字符“e”或者“d”、数字的正号或者负号、复数的虚部字符“i”或者“j”,使用时要注意空格,例如。

A=str2num('1+3i')
B=str2num('1 +3i')
C=str2num('1 + 3i')
whos

运行结果如下:

A =
  1.0000 + 3.0000i
B =
  1.0000 + 0.0000i  0.0000 + 3.0000i
C =
  1.0000 + 3.0000i
 Name   Size      Bytes Class   Attributes
 A     1x1        16 double  complex  
 B     1x2        32 double  complex  
 C     1x1        16 double  complex  
 S1    1x4         8 char        
 S2    1x4         8 char        
 S3    1x4         8 char        
 a     2x1        16 double       
 b     1x1         8 double       
 c     1x10        20 char        
 d     3x10        60 char        
 e     3x10        60 char

【例1-59】str2num函数示例。

S=['1 2';'2 3']
A=str2num(S)
whos

运行结果如下:

S =
1 2
2 3
A =
   1   2
   2   3
Name   Size      Bytes Class   Attributes
 A     2x2        32 double       
 B     1x2        32 double  complex  
 C     1x1        16 double  complex  
 S     2x3        12 char        
 S1    1x4         8 char        
 S2    1x4         8 char        
 S3    1x4         8 char        
 a     2x1        16 double       
 b     1x1         8 double       
 c     1x10        20 char        
 d     3x10        60 char        
 e     3x10        60 char

【例1-60】使用函数num2str将数字转换成为字符串时,指定字符串所表示的有效数字位数。

C=num2str(rand(2,2),4)
D=num2str(rand(2,2),6)

运行结果如下:

C =
0.8147   0.127
0.9058   0.9134
D =
0.632359   0.278498
0.0975404   0.546882

【例1-61】其他的转换函数示例。

a=188;
h=dec2hex(a)
b=dec2bin(a)
c=dec2base(a,5)
b(end)='0'
bin2dec(b)
whos

运行结果如下:

h =
BC
b =
10111100
c =
1223
b =
10111100
ans =
  188
 Name   Size      Bytes Class   Attributes
 A     2x2        32 double       
 B     1x2        32 double  complex  
 C     2x17        68 char        
 D     2x22        88 char        
 S     2x3        12 char        
 S1    1x4         8 char        
 S2    1x4         8 char        
 S3    1x4         8 char        
 a     1x1         8 double       
 ans    1x1         8 double       
 b     1x8        16 char        
 c     1x4         8 char        
 d     3x10        60 char        
 e     3x10        60 char        
 h     1x2         4 char

2.格式化输入输出
MATLAB可以进行格式化的输入、输出,格式化字符串都可以用于MATLAB的格式化输入输出函数,如表1-15所示。
《MATLAB图像处理375例》——1.7 字符串
《MATLAB图像处理375例》——1.7 字符串

在MATLAB中,有sscanf和sprintf这样两个函数进行格式化的输入和输出,调用方法如下:

A=sscanf(s,format,size) %读取格式化字符串
S=sprintf(format,A,······) %格式化输出数据到命令行窗口

【例1-62】sscanf函数示例。

S1='2.6183 3.1415';
S2='2.7183e3 3.1416e3';
S3='0 2 4 8 16 32 64 128';
A=sscanf(S1,'%f')
B=sscanf(S2,'%e')
C=sscanf(S3,'%d')

运行结果如下:

A =
  2.6183
  3.1415
B =
  1.0e+03 *
  2.7183
  3.1416
C =
   0
   2
   4
   8
  16
  32
  64
  128

【例1-63】sscanf函数示例(A=sscanf(s,format,size))。

S1='0 1 3 8 16 32 64 128';
A=sscanf(S3,'%d')
B=sscanf(S3,'%d',1)

运行结果如下:

A =
   0
   2
   4
   8
  16
  32
  64
  128
B =
   0

在MATLAB中,input函数用来完成获取用户输入数据的功能,以满足能够和用户的输入进行交互的需要,该函数的调用方法如下:

A=input(prompt)
A=input(prompt,’s’)

其中,第一个参数prompt为提示用的字符串;第二个参数s,若有s,则输入的数据为字符串,没有s,则输入的数据为双精度数据。

A=input('请输入数字:')
B=input('请输入数字:','s')
whos

运行结果如下:

请输入数字:123
A =
  123
B =
123
 Name   Size      Bytes Class   Attributes
 A     1x1         8 double       
 B     1x3         6 char        
 C     1x4        32 double       
 D     1x4        32 double       
 S     2x3        12 char        
 S1    1x23        46 char        
 S2    1x12        24 char        
 S3    1x11        22 char        
 S4    1x23        46 char        
 a     1x1         8 double       
 ans    1x1         8 double       
 b     1x8        16 char        
 c     1x4         8 char        
 d     3x10        60 char        
 e     3x10        60 char        
 h     1x2         4 char