size(S3)
ans =
5 5
3.1.3.3 转换函数产生数码字符串
【例 3.1.3.3-1】最常用的数组/字符串转换函数 int2str , num2str , mat2str 示例。
(1)
A=eye(2,4);
A_str1=int2str(A)
A_str1 =
1 0 0 0
0 1 0 0
(2)
rand('state',0)
B=rand(2,4);
B3=num2str(B,3)
B3 =
0.95 0.607 0.891 0.456
0.231 0.486 0.762 0.0185
(3)
B_str=mat2str(B,4)
B_str =
[0.9501 0.6068 0.8913 0.4565;0.2311 0.486 0.7621 0.0185]
Expression=['exp(-',B_str,')'];
eval(Expression)
ans =
0.3867 0.5451 0.4101 0.6335
0.7937 0.6151 0.4667 0.9817
【例 3.1.3.3-2】综合例题:在 MATLAB 计算生成的图形上标出图名和最大值点坐标。
clear
a=2;
w=3;
t=0:0.01:10;
y=exp(-a*t).*sin(w*t);
[y_max,i_max]=max(y);
t_text=['t=',num2str(t(i_max))]; % <7>
y_text=['y=',num2str(y_max)]; % <8>
max_text=char('maximum',t_text,y_text); % <9>
%
tit=['y=exp(-',num2str(a),'t)*sin(',num2str(w),'t)']; %<11>
plot(t,zeros(size(t)),'k')
hold on
plot(t,y,'b')
plot(t(i_max),y_max,'r.','MarkerSize',20)
text(t(i_max)+0.3,y_max+0.05,max_text) % <16>
title(tit),xlabel('t'),ylabel('y'),hold off
评论0