第五课 编制脚本程序
DESCRIPTION
第五课 编制脚本程序. M 文本编辑器、基本控制流(条件语句、循环语句)、函数调用、调试. Matlab 编程基本要素. 编辑 / 调试器 条件结构 循环结构 主程序与函数调用 全局变量和局部变量 调试 性能优化 面向对象的编程. 编辑 / 调试器. 条件结构. 其他条件结构: switch ex case test1 (cmd1) case test2 (cmd2) end. if ii>100 a = 1; else a = 0; end. 循环结构. for ii=1:100 - PowerPoint PPT PresentationTRANSCRIPT
第五课 编制脚本程序第五课 编制脚本程序
MM 文本编辑器、基本控制流(条件语文本编辑器、基本控制流(条件语句、循环语句)、函数调用、调试句、循环语句)、函数调用、调试
MatlabMatlab 编程基本要素编程基本要素 编辑编辑 // 调试器调试器 条件结构条件结构 循环结构循环结构 主程序与函数调用主程序与函数调用 全局变量和局部变量全局变量和局部变量 调试调试 性能优化性能优化 面向对象的编程面向对象的编程
编辑编辑 // 调试器调试器
条件结构条件结构
if ii>100if ii>100
a = 1;a = 1;
elseelse
a = 0;a = 0;
endend
其他条件结构:其他条件结构: sswitch exwitch ex
case test1case test1
(cmd1)(cmd1)
case test2case test2
(cmd2)(cmd2)
endend
循环结构循环结构
for ii=1:100for ii=1:100
a[ii] = (ii+1)/2;a[ii] = (ii+1)/2;
endend
其他循环结构:其他循环结构:while expressionwhile expression
(commands)(commands)
endend
主程序与函数调用主程序与函数调用
函数标志:函数标志:function fun_namefunction fun_name (( arg1arg1 ,, arg2arg2 ))
调用:调用:在主程序里,直接调用在主程序里,直接调用
全局变量和局部变量全局变量和局部变量
局部变量:仅存在于该函数的运行过程中。局部变量:仅存在于该函数的运行过程中。 全局变量:全共享的变量,用全局变量:全共享的变量,用 globalglobal 定义。定义。 在主程序中的变量与全局变量在主程序中的变量与全局变量
调试调试
设置断点
去除断点 执行直到断点
步进 步进出
步进入
停止调试
性能优化性能优化
循环向量化循环向量化 尽可能使用尽可能使用 matlabmatlab 提供的函数指令提供的函数指令 循环内数组的预配置循环内数组的预配置 采用采用 mexmex 文件执行循环文件执行循环 尽量使用尽量使用 mm 函数代替函数代替 mm 脚本脚本 JITJIT 和加速器的加速和加速器的加速
实验内容:利用实验内容:利用 mm 脚本脚本绘制特殊图形、三维图形绘制特殊图形、三维图形和显示图像和显示图像
三维图形效果三维图形效果
透视透视 镂空镂空 裁剪裁剪
透 视透 视
[X0,Y0,Z0] = sphere(30);[X0,Y0,Z0] = sphere(30); mesh(X0,Y0,Z0);mesh(X0,Y0,Z0); hidden offhidden off axis equal, axis offaxis equal, axis off
镂 空镂 空
t = linspace(0,2*pi,100); r = 1-exp(-t/2).*ct = linspace(0,2*pi,100); r = 1-exp(-t/2).*cos(4*t);os(4*t);
[x,y,z] = cylinder(r,60);[x,y,z] = cylinder(r,60); ii = find(x<0&y>0);ii = find(x<0&y>0); z(ii) = NaN;z(ii) = NaN; surf(x,y,z); colormap(spring); surf(x,y,z); colormap(spring);
裁 剪裁 剪
x = [-8:0.05:8]; y = x; x = [-8:0.05:8]; y = x; [X,Y] = meshgrid(x,y); ZZ = X.^2-Y.^2;[X,Y] = meshgrid(x,y); ZZ = X.^2-Y.^2; ii = find(abs(X)>6|abs(Y)>6);ii = find(abs(X)>6|abs(Y)>6); ZZ(ii) = zeros(size(ii));ZZ(ii) = zeros(size(ii)); surf(X,Y,ZZ), surf(X,Y,ZZ), shading interp, colormap(cocolormap(co
pper)pper)
特殊图形指令特殊图形指令
面域图 面域图 areaarea 直方图 直方图 bar barh bar3 bar3hbar barh bar3 bar3h 饼图 饼图 pie pie3pie pie3 填色图 填色图 fill fill3fill fill3
面域图 面域图 areaarea
x = -2:2x = -2:2 y = [3,5,2,4,1;3,4,5,2,1;5,4,3,2,5]y = [3,5,2,4,1;3,4,5,2,1;5,4,3,2,5] cum_sum = cumsum(y);cum_sum = cumsum(y); area(x',y',0)area(x',y',0) legend('legend(' 因素因素 A','A',' 因素因素 B','B',' 因素因素 C'),grid on,C'),grid on,
colormap(spring)colormap(spring)
直方图 直方图 bar barhbar barh
x = -2:2x = -2:2 y = [3,5,2,4,1;3,4,5,2,1;5,4,3,2,5]y = [3,5,2,4,1;3,4,5,2,1;5,4,3,2,5] subplot(1,2,1),bar(x',y','stacked')subplot(1,2,1),bar(x',y','stacked') xlabel('x'), ylabel('\Sigma y'), colormap(cool) xlabel('x'), ylabel('\Sigma y'), colormap(cool) legend('legend(' 因素因素 A','A',' 因素因素 B','B',' 因素因素 C')C') subplot(1,2,2), barh(x',y','grouped')subplot(1,2,2), barh(x',y','grouped') xlabel('y'), ylabel('x')xlabel('y'), ylabel('x') legend('legend(' 因素因素 A','A',' 因素因素 B','B',' 因素因素 C')C')
直方图 直方图 bar3 bar3hbar3 bar3h
subplot(1,2,1), bar3(x',y',1)subplot(1,2,1), bar3(x',y',1) xlabel('xlabel(' 因素因素 ABC'), ylabel('x'),zlabel('y')ABC'), ylabel('x'),zlabel('y') colormap(summer)colormap(summer) subplot(1,2,2), bar3h(x',y','grouped')subplot(1,2,2), bar3h(x',y','grouped') ylabel('y'), zlabel('x')ylabel('y'), zlabel('x')
饼图 饼图 pie pie3pie pie3
a = [1,1.6,1.2,0.8,2.1];a = [1,1.6,1.2,0.8,2.1]; subplot(1,2,1), pie(a,[1 0 1 0 0]),subplot(1,2,1), pie(a,[1 0 1 0 0]), legend({'1','2','3','4','5'});legend({'1','2','3','4','5'}); subplot(1,2,2), pie3(a),colormap(cool)subplot(1,2,2), pie3(a),colormap(cool)
填色图 填色图 fill fill
n = 10;n = 10; dt = 2*pi/n; t = 0:dt:2*pi;dt = 2*pi/n; t = 0:dt:2*pi; t = [t,t(1)];t = [t,t(1)]; x = sin(t); y = cos(t);x = sin(t); y = cos(t); fill(x,y,'c'); axis offfill(x,y,'c'); axis off gtext('gtext(' 十边行十边行 ');');
填色图 填色图 fill3fill3
xc = ones(2,4)/2;xc = ones(2,4)/2; x = [xc;[0 1 1 0]];x = [xc;[0 1 1 0]]; y = [xc;[0 0 1 1]];y = [xc;[0 0 1 1]]; z = [1 1 1 1;0 0 0 0;0 0 0 0];z = [1 1 1 1;0 0 0 0;0 0 0 0]; c = [1 0 0 1;0 1 0 1;0 0 1 0]; c = [1 0 0 1;0 1 0 1;0 0 1 0]; fill3(x,y,z,c),view([-10,56]),colormap coolfill3(x,y,z,c),view([-10,56]),colormap cool xlabel('x'), ylabel('y'), box on, grid onxlabel('x'), ylabel('y'), box on, grid on
显示图像显示图像
imreadimread imshowimshow demodemo (( imageimage 和和 videovideo )) matlabmatlab ::
I = imread(‘image path’);I = imread(‘image path’);
imshow(I);imshow(I);