3.字符串,元胞和构架数组

 

1  串数组的属性和标识

 1 a='hello world'
 2 
 3 %a的大小
 4 size(a)
 5 
 6 %获取前两个字符
 7 a12=a(1:2)
 8 %字符串逆转
 9 ra=a(end:-1:1)
10 
11 %char转化为ascii
12 ascii_a=double(a)
13 
14 %ascii码转化为char
15 char(ascii_a)
16 
17 %转化为大写字符
18 w=find(a>='a'&a<='z');
19 ascii_a(w)=ascii_a(w)-32;
20 char(ascii_a)
1 A='这是一个算例.'
2 A_s=size(A)
3 %获取第五个第六个字符
4 A56=A([5 6])
5 %转化为double类型
6 ASCII_A=double(A)
7 %dboule类型转化为char类型
8 char(ASCII_A)
1 %''转义字符
2 b=' Example  ''hello'''
3 %合并字符串
4 ab=[a(1:7),' ',b,' .']

2  利用串操作函数创建多行串数组

 1 clear
 2 %多行串数组的直接创建
 3 S=['This string array '
 4     'has multiple rows.']
 5 size(S)
 6 
 7 %多行串数组创建
 8 s1=char('This string array','hsa two rows.')
 9 
10 %多行串数组创建
11 s2=str2mat('','字符','串数组','','由4行组成')
12 
13 size(s2)
 1 A=eye(3,4);
 2 %矩阵转化为字符串
 3 A_str1=int2str(A)
 4 
 5 %生成随机数
 6 rand('state',0)
 7 B=rand(2,4);
 8 %随机数矩阵转化为字符串
 9 B3=num2str(B,3)
10 
11 A_str=mat2str(A,4)
12 Expression=['exp(-',A_str,')']
13 %将字符串转化为matlab可执行语句
14 eval(Expression)

matlab综合例题,在MATLAB计算生成的图形上标出图名和最大值点的坐标

运行效果

 1 clear
 2 a=2;
 3 w=3;
 4 %t的范围从0到10
 5 t=0:0.01:10
 6 %表达式 y = exp(-2t)*sin(3t)
 7 y=exp(-a*t).*sin(w*t);
 8 %求y值最大的点对应的横坐标与纵坐标
 9 [y_max,i_max]=max(y);
10 
11 %转化为字符串
12 t_text=['t=',num2str(t(i_max))];
13 y_text=['y=',num2str(y_max)];
14 %三行字符串
15 max_text=char('maximum',t_text,y_text);
16 
17 %表格的题目
18 tit=['y=exp(-',num2str(a),'t)*sin(',num2str(w),'t)'];
19 %绘制y=0的横线
20 plot(t,zeros(size(t)),'k');
21 
22 %画图
23 hold on
24 %绘制t,y
25 plot(t,y,'b')
26 %绘制最高点的红点
27 plot(t(i_max),y_max,'r.','MarkerSize',30)
28 %输出信息
29 text(t(i_max)+0.3,y_max+0.05,max_text)
30 %设置标题
31 title(tit),xlabel('t'),ylabel('y'),hold off

3  利用元胞数组创建复杂字符串

 1 %定义字符串
 2 a='MATLAB 6.x   ';b='includes new data types:';
 3 c1='Multidimensional array'
 4 c2='User-definable data structure'
 5 c3='Cell arrays'
 6 c4='Character array'
 7 c5='Function handle'
 8 %定义五个字符串为一个字符串
 9 c=char(c1,c2,c3,c4,c5)
10 %元胞数组
11 C={a;b;c};
12 %输出第一个第二个
13 disp([C{1:2}])
14 disp(' ')
15 %输出最后一个
16 disp(C{3})

4  串转换函数

1 rand('state',0);a=rand(2,2);
2 s1=num2str(a)
3 %类似C语言的printf
4 s_s=sprintf('%.10e\n',a)
5 %类似C语言的fprintf
6 fprintf('%.5g\\',a)
7 %类似C语言的sscanf
8 s_sscan = sscanf(s_s,'%f',[3,2])

 5  元胞数组的创建和显示

 1 C_str=char('这是','元胞数组创建算例')
 2 %创建矩阵
 3 R=reshape(1:9,3,3)
 4 Cn=[1+2i]
 5 S_sym=sym('sin(-3*t)*exp(-t)')
 6 
 7 %直接创建法1
 8 A(1,1)={C_str}
 9 A(1,2)={R}
10 A(2,1)={Cn}
11 A(2,2)={S_sym};
12 A
13 
14 %直接创建法2
15 B{1,1}=C_str
16 B{1,2}=R
17 B{2,1}=Cn
18 B{2,2}=S_sym

6  元胞数组的扩充

1 C = cell(2);
2 C(:,1)={char('Another','text string');10:-1:1}
3 
4 %水平加入
5 AC=[A C]
6 %垂直加入
7 A_C=[A;C]

7  cellplot能用图形形象化地表示元胞数组的内容

1 C = cell(2);
2 C(:,1)={char('Another','text string');10:-1:1}
3 
4 %绘制
5 cellplot(C,'legend')

8  元胞数组的收缩和重组

1 A_C
2 %取三行
3 A_C(3,:)=[]
4 %重组为两行三列
5 R_A_C=reshape(A_C,2,3)

9  元胞数组内容的调取

 1 %取出第一行第三列的元组
 2 f1 = R_A_C(1,3)
 3 %获得类型
 4 class(f1)
 5 
 6 %取第一行第三列所存储的数据
 7 f2=R_A_C{1,3}
 8 class(f2)
 9 
10 %获得第一行第一列的数据,取出所有行,第1列第二列,第五列,第六列的数据
11 f3=R_A_C{1,1}(:,[1 2 5 6])
12 
13 %获取从上到下,第一个,第三个,第4个数据
14 [f4,f5,f6]=deal(R_A_C{1,3,4})

10  元胞数组的转换函数

 1 rand('state',0)
 2 A=rand(2,3,2)
 3 %num2cell把数值数组转成元胞数组
 4 C1=num2cell(A)
 5 
 6 %每一层转换为一个元组
 7 C2=num2cell(A,1)
 8 
 9 %每一面转换为一个元组
10 C3=num2cell(A,[2,3])
11 
12 clear
13 %初始化矩阵
14 x=zeros(4,5)
15 x(:)=1:20
16 %分成两行两列     第一列每行三个元素,第二列每行2个元素
17 C4=mat2cell(x,[2 2],[3 2])
18 %显示元组
19 celldisp(C4)
20 
21 %转成矩阵
22 D=cell2mat(C4(1,:))

11  构架数组

 1 green_house(1,1).name='一号房'
 2 green_house(1,1).volume='2000立方米'
 3 green_house(1,1).parameter.temperature=[31.2 30.4 31.6 28.7 
 4                                    29.7 3131 30.9 29.6]
 5 green_house(1,1).parameter.humidity=[62.1 59.5 57.7 61.5 
 6                                 62.0 61.9 59.2 57.5]
 7 
 8 %显示"单构架"结构和内容
 9 green_house(1,1)
10 %显示数据类型
11 green_house(1,1).parameter
12 %显示具体数据
13 green_house(1,1).parameter.temperature
14 
15 green_house(1,2).name='六号房'
16 
17 green_house(1,2)

12  利用构造函数创建构架数组

 1 a=cell(2,3)
 2 green_house_1=struct('name',a,'volume',a,'parameter',a(1,2))
 3 
 4 green_house_2=struct('name',a,'volume',[],'parameter',[])
 5 
 6 green_house_3(2,3)=struct('name',[],'volume',[],'parameter',[])
 7 
 8 a1={'六号房'}
 9 a2={'3200立方米'}
10 green_house_4(2,3)=struct('name',a1,'volume',a2,'parameter',[]);
11 T6=[31.2,30.4,31.6,28.7,29.7,31.1,30.9,29.6];
12 green_house_4(2,3).parameter.temperature=T6;
13 green_house_4

13  增加域和删除域

 1 clear
 2 for k=1:10
 3 department(k).number=['No.',int2str(k)];
 4 end
 5 department
 6 
 7 %增加域
 8 department(1).teacher=40
 9 department(1).student=300
10 department(1).PC_computer=40
11 department
12 
13 %增加域
14 department(2).teacher.male=35
15 department(2).teacher.female=13
16 D2T=department(2).teacher%第2个构架teacher域包含两个子域
17 D1T=department(1).teacher%第1构架teacher域仅是一个数
18 
19 %删除域20 department(2).teacher=refield(department(2).teacher,'male')
21 department(2).teacher
22 
23 department=rmfield(department,'student')
24 department=rmfield(department,{'teacher';'PC_computer'})

14  数值运算操作和函数对构架数组的应用

 1 n_ex=5
 2 %初始化矩阵
 3 for  k=1:n_ex
 4    ex(k).f=(k-1)*n_ex+[1:5]
 5 end
 6 
 7 %显示构架中的内容
 8 disp([blanks(10) '构架中内容'])
 9 for k=1:n_ex
10     disp(ex(k).f)
11 end
12 
13 %统计每一列的和,f中有多少循环,外部就有多少循环
14 sum_f=zeros(1,5)
15 for k=1:n_ex
16     sum_f = sum_f+ex(k).f
17 end
18 
19 %求平方根,f中有多少循环,外部就有多少循环
20 disp([blanks(20) 'ex.f的平方根值'])
21 for k=1:n_ex
22     disp(sqrt(ex(k).f))
23 end

 

15  架构数组和元胞数组之间的转换

 1 for k=1:5
 2     %第一个域
 3     ex(k).s=['No.' int2str(k)]
 4     %第二个域
 5     ex(k).f=(k-1)*5+[1:5]
 6     
 7 end
 8 
 9 %输出第一个域
10 fprintf('%s\n','ex.s域的内容  ')
11 fprintf('%s\',blanks(4))
12 for k=1:5
13     fprintf('%s\\',[ex(k).s blanks(1)])
14 end
15 
16 %输出第二个域
17 fprintf('%s\n',blanks(1),fprintf('%s\n','ex.f域的内容   '))
18 for k=1:5
19     disp(ex(k).f)
20 end
21 
22 %架构数组转元胞数组
23 C_ex=struct2cell(ex)
24 size(C_ex)
25 %输出域
26 fprintf('%s\',[C_ex{2,1,1},blanks(3)])
27 fprintf('%5g',C_ex{1,1,1})
28 
29 %元胞数组转架构数组
30 FS={'S_char';'F_num'}
31 EX1=cell2struct(C_ex,FS,1)
32 EX1(1)
33 
34 %第2列F_num域名被换成xx
35 %EX2=cell2struct(C_ex,'xx',2)
36 
37 %域名变成y1,y2,y3,y4,y5
38 YY=strvcat('y1','y2','y3','y4','y5');
39 EX3=cell2struct(C_ex,YY,3)
40 EX3(1)
41 EX3(2)

 

转载于:https://www.cnblogs.com/xiaochi/p/8872729.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值