1、006. 字符串 字母、数字、特殊符号都是字符;一串连续的字符就是字符串。 一般认为字符串是由空格和多个字符构成,记为 str = ’a1 a2 …an’ (n≥0) ai可以是字母、数字、特殊符号、空格,每个字符占1位存放成一个行向量(1×n矩阵),从而可以通过下标访问字符串的元素。 多个字符串也可以构成字符矩阵,但必须长度相同。 一、 字符串的创建 1. 将字符串的字符放在一组英文单引号中间即可,例如 str1 = 'We''re going to study Matlab!' % 必须英文状态下的单引
2、号,单引号元素用两个单引号 l = length(str1) % l返回列数,即字符串包含字符的个数 zhstr1 = '中文字符串示例!' % 中文字符串,也是英文单引号 size(zhstr1) 运行结果:str1 = We're going to study Matlab! l = 28 zhstr1 = 中文字符串示例! ans = 1 8 2. 用元胞数组存放复杂字符串,或cellstr()函数 C1 = {'Matlab 2010b includes data types:'; 'Double
3、 array'; 'Character array'; 'and so on'} class(C1) % 返回 C1 的数据类型 size(C1) C2 = char('Matlab 2010b includes data types:', ... 'Double array', ... 'Character array',... 'and so on') class(C2) size(C2) C3 = cellstr(C2); % 同 C1 运行结果:C1 = 'Matlab 2010b includes da
4、ta types:' 'Double array' 'Character array' 'and so on' ans = cell ans = 4 1 C2 = Matlab 2010b includes data types: Double array Character array and so on ans = char ans = 4 33 注:这里C1也可
5、以用strvcat(str1, str2,…) 实现类似的存储效果,见下文【字符串的连接】。 二、 字符串的访问和操作 字符串是以行向量形式存储的,可通过下标访问。 1. 替换字符串中的元素 str1 = 'We''re going to study Matlab!'; str1(16:20) = 'learn' % 将study替换为learn,注意study是从第16个位置开始的 运行结果:str1 = We're going to learn Matlab! 2. 取出字符串的子串 str1 = 'We''re going to s
6、tudy Matlab!'; str2 = str1(16:20) 运行结果:str2 = learn 3. 字符串顺序的倒排 str1 = 'We''re going to study Matlab!'; str3 = str1(end:-1:1) 运行结果:str3 = !baltaM nrael ot gniog er'eW 4. 字符串字符的ASCII值与字符相互转换 字符串的元素存放的是字符的ASCII码值,显示在屏幕上的是字符本身。 从字符到ASCII码:double( ) 从ASCII码到字符:char( ) st
7、r1 = 'We''re going to study Matlab!'; ustr1 = double(s1) str4 = char(us1) zhstr1 = '中文字符串示例!'; double(zhstr1) 运行结果:ustr1 = Columns 1 through 15 87 101 39 114 101 32 103 111 105 110 103 32 116 111 32 Columns 16 through 28 108 1
8、01 97 114 110 32 77 97 116 108 97 98 33 str4 = We're going to learn Matlab! ans = 20013 25991 23383 31526 20018 31034 20363 65281 5. 字符串英文字母转换大小写 str1 = 'We''re going to study Matlab!'; upper(str1) %
9、 全变为大写 lower(str1) % 全变为小写 运行结果:ans = WE'RE GOING TO LEARN MATLAB! ans = we're going to learn matlab! 6. 字符串的连接 strcat(str1, str2,…)——将字符串str1, str2,…连接合并为一个长字符串; strvcat(str1, str2,…)——将字符串str1, str2,…连接成字符串向量(n×1的字符串矩阵,或n×mi字符矩阵); str1 = 'abcdefg'; str2 = 'hij
10、klmnopq'; str3 = strcat(str1,str2) % 将两个字符串合并为一个长字符串 str4 = strvcat(str1,str2) % 将两个字符串连接成字符串向量 whos str3 % 返回str3的结构形式 whos str4 % 2 x 1的字符串矩阵 运行结果: str3 = abcdefghijklmnopq str4 = abcdefg hijklmnopq Name Size Bytes Class Attributes
11、str3 1x14 28 char Name Size Bytes Class Attributes str4 2x7 28 char 7. 字符串的比较 strcmp(str1,str2)——str1与str2相等返回1,否则返回0(区分大小写); strcmpi(str1,str2)——str1与str2相等返回1,否则返回0(不区分大小写); strncmp(str1,str2,n)——比较str1与str2前n个字符是否相等(区分大小写)
12、 strncmpi(str1,str2,n)——比较str1与str2前n个字符是否相等(不区分大小写) strcmp(str1,str2) strcmpi('abCD','abcd') strncmp('Abcde',str3,5) strncmpi('Abcde',str3,5) 运行结果: ans = 0 ans = 1 ans = 0 ans = 1 8. 字符串的查找与替换 findstr(str1,str2)——在较长
13、字符串中查找较短字符串出现的各个位置(str1,str2谁先谁后都没关系); strfind(str1,key)——在字符串str1中查找字符串key出现的位置; strmatch(key,strs)——检查多行的字符串strs,以列向量形式返回各行以字符串key开始的各个行号; strrep(str1,str2,str3)——把str1中含有str2位置用str3替换; str = 'Find the starting indices of the shorter string.'; findstr(str, 'the') findstr('the', str) strfi
14、nd(str,'the') strfind('the',str) strmatch('hij',str4) % 字符串str4第2行是以’hij’开始的 strrep(str,'the','a') 运行结果: ans = 6 30 ans = 6 30 ans = 6 30 ans = [] ans = 2 ans = Find a starting indices of a shorter string. 9. 数值矩阵与字符串转换 num2str(A)——数值矩阵转换为字符串向
15、量(字符矩阵); str2num(str)——字符串向量(字符矩阵)转换为数值矩阵; mat2str(A)——将数值矩阵A的表达式转换为字符串“[” “]”“;”等都保留; int2str(A)——整数矩阵转换为字符串向量(字符矩阵)。 A = [1,2,3;4,5,6;77,88,99] whos A str = num2str(A) whos str num = str2num(str); % num等于A str1 = mat2str(A) whos str1 str2 = int2str(A); % 其值等于前面的str, % 若A不是
16、整数矩阵先四舍五入为整数矩阵 运行结果: A = 1 2 3 4 5 6 77 88 99 Name Size Bytes Class Attributes A 3x3 72 double str = 1 2 3 4 5 6 77 88 99 Name
17、 Size Bytes Class Attributes str 3x10 60 char str1 = [1 2 3;4 5 6;77 88 99] Name Size Bytes Class Attributes str1 1x22 44 char 10. 其他函数 blanks(n)——返回n个空格组成的字符串; deblank(str)——删除字符串末尾的空格; strtrim(st
18、r)——删除字符串开头、结尾的空格、制表符、换行符; isspace(str)——返回和str同样大小的向量,空格、制表符、换行符的位置是1,其他位置是0; lasterr——返回上一个错误信息的字符串。 三、 字符串的应用 1. eval()函数——将括号内的字符串视为执行代码并运行 例如,eval('y=sin(pi/2)') 和y = sin(pi/2) 等价 运行结果都是:y = 1 Eval()函数多在循环中搭配num2str()函数一起使用,可以对多个名字有规则的变量或文件进行操作,例如, for k=1:3; eval(['y' num2st
19、r(k) '=' num2str(k^2)]) % 实现yk = k^2 end D = {'odedemo'; 'sunspots'; 'fitdemo'}; % 三个m文件名 n = input('Select a demo number: '); % 提示输入要选择执行的某个m文件,比如输入2 eval(D{n}) % 运行sunspots.m 运行结果: y1 = 1 y2 = 4 y3 = 9 运行Matlab自带的sunspots.m文件,输出结果是一个图(略) 2. 用字符串命令控制格式化输出
20、 例,fprintf,sprintf,sscanf用法 a = rand(2,2) % 生成 2×2 随机矩阵 ss = sprintf('%.10e\n',a) % 按10位科学计数法输出, % 每输一个换行,注意:ss是字符矩阵 fprintf('%.4g\n',a) % 按 4 位数输出 sscanf(ss, '%f', [2,2]) % 按浮点格式转换成 2×2 数值矩阵 运行结果: a = 0.8147 0.1270 0.9058 0.9134 ss =
21、 8.1472368639e-001 9.0579193708e-001 1.2698681629e-001 9.1337585614e-001 ans = 0.8147 0.9058 0.127 0.9134 ans = 0.8147 0.1270 0.9058 0.9134 3. 用字符串给图形标记文字说明 a=2; w=3; t=0:0.01:10; y=exp(-a*t).*sin(w*t); [
22、y_max,i_max]=max(y); % 找出y的最大值元素位置 t_text=['t=',num2str(t(i_max))]; % 生成最大值点横坐标字符串 y_text=['y=',num2str(y_max)]; % 生成最大值点纵坐标字符串 max_text=char('maximum',t_text,y_text); tit=['y=exp(-',num2str(a),'t)*sin(',num2str(w),'t)']; % 生成标记最大值点的字符串 plot(t,zeros(size(t)),'k') % 画纵坐标为
23、0的基准线 hold on % 保持已绘制图形不被清除 plot(t,y,'b') % 用蓝色线画函数 y(t) plot(t(i_max),y_max,'r.','MarkerSize',20) % 用大红点标记最大值点 text(t(i_max)+0.3,y_max+0.05,max_text) % 显示最大值点的数值 title(tit) % 标记图名 xlabel('t') % 标记横坐标名 ylabel('y') % 标记纵坐标名 hold off % 清除原来的图 运行结果:






