《第4章Matlab 简易使用教程(二)》由会员分享,可在线阅读,更多相关《第4章Matlab 简易使用教程(二)(13页珍藏版)》请在金锄头文库上搜索。
1、安安富富莱莱 U U U UM M M M4 4 4 40 0 0 03 3 3 3D DS SP P 教教程程 S S S ST T T TM M M M3 3 3 32 2 2 2- - - -V V V V5 5 5 5 开开发发板板系系统统篇篇手手册册 2 20 01 15 5年年0 01 1月月1 15 5日日 版版本本:1 1. .0 0 第第 1 1 页页 共共 1 13 3 页页 第第4 4章章 MMa at tl la ab b 简简易易使使用用(二二) 本期教程主要是讲解 Matlab 的简易使用方法,有些内容跟上一节相同,但是比上一些更详细。 4.1 Matlab 的脚本
2、文件.m 的使用 4.2 Matlab 中的条件和循环函数 4.3 绘图功能 4.4 总结 4 4. .1 1 MMa at tl la ab b 的的脚脚本本文文件件. .mm 的的使使用用 在 matlab 上创建和使用.m 文件跟在 MDK 或者 IAR 上面创建和使用.C 或者.ASM 文件是一样的。 创建方法如下: 点击上图中的小图标,打开编辑窗口后,输入以下函数: % Generate random data from a uniform distribution % and calculate the mean. Plot the data and the mean. n = 5
3、0; % 50 data points r = rand(n,1); plot(r) Draw a line from (0,m) to (n,m) m = mean(r); hold on plot(0,n,m,m) hold off title(Mean of Random Uniform Data) 安安富富莱莱 U U U UM M M M4 4 4 40 0 0 03 3 3 3D DS SP P 教教程程 S S S ST T T TM M M M3 3 3 32 2 2 2- - - -V V V V5 5 5 5 开开发发板板系系统统篇篇手手册册 2 20 01 15 5年年0
4、 01 1月月1 15 5日日 版版本本:1 1. .0 0 第第 2 2 页页 共共 1 13 3 页页 编辑好函数后需要将当前文件进行保存,点击 FileSave as 即可,然后点击如下图标即可运行(或者按 F5) : 显示效果如下: 4 4. .2 2 MMa at tl la ab b 中中的的条条件件和和循循环环函函数数 matlab 也支持类似 C 语言中的条件和循环语句:for, while, if, switch。但在 matlab 中使用比在 C中使用更加随意。 比如在.M 文件中输入以下函数: nsamples = 5; npoints = 50; 05101520253
5、03540455000.10.20.30.40.50.60.70.80.91安安富富莱莱 U U U UM M M M4 4 4 40 0 0 03 3 3 3D DS SP P 教教程程 S S S ST T T TM M M M3 3 3 32 2 2 2- - - -V V V V5 5 5 5 开开发发板板系系统统篇篇手手册册 2 20 01 15 5年年0 01 1月月1 15 5日日 版版本本:1 1. .0 0 第第 3 3 页页 共共 1 13 3 页页 for k = 1:nsamples currentData = rand(npoints,1); sampleMean(k
6、) = mean(currentData); end overallMean = mean(sampleMean) 在命令窗口得到输出结果: Untitled2 %这个是保存的文件名,运行相应文件会自动打印出 overallMean = 0.4477 为了将上面函数每次迭代的结果都进行输出可以采用如下方法: for k = 1:nsamples iterationString = Iteration #,int2str(k); disp(iterationString) %注意这里没有分号,这样才能保证会在命令窗口输出结果 currentData = rand(npoints,1); samp
7、leMean(k) = mean(currentData) %注意这里没有分号 end overallMean = mean(sampleMean) %注意这里没有分号 在命令窗口得到输出结果: Untitled2 Iteration #1 sampleMean = 0.4899 0.4642 0.5447 0.4808 0.4758 Iteration #2 sampleMean = 0.4899 0.4959 0.5447 0.4808 0.4758 Iteration #3 sampleMean = 安安富富莱莱 U U U UM M M M4 4 4 40 0 0 03 3 3 3D
8、DS SP P 教教程程 S S S ST T T TM M M M3 3 3 32 2 2 2- - - -V V V V5 5 5 5 开开发发板板系系统统篇篇手手册册 2 20 01 15 5年年0 01 1月月1 15 5日日 版版本本:1 1. .0 0 第第 4 4 页页 共共 1 13 3 页页 0.4899 0.4959 0.4977 0.4808 0.4758 Iteration #4 sampleMean = 0.4899 0.4959 0.4977 0.5044 0.4758 Iteration #5 sampleMean = 0.4899 0.4959 0.4977 0
9、.5044 0.5698 overallMean = 0.5115 如果在上面的函数下面加上如下语句: if overallMean .51 disp(Mean is greater than expected) else disp(Mean is within the expected range) end 命令窗口输出结果如下: overallMean = %这里仅列出了最后三行 0.5381 Mean is greater than expected 4 4. .3 3 绘绘图图功功能能 4 4. .3 3. .1 1 基基本本的的 p pl lo ot t 函函数数 根据 plot 不
10、同的输入参数,主要有两种方式: plot(y),这种方式的话,主要是根据 y 的数据个数产生一个线性曲线。 plot(x,y)以 x 轴为坐标进行绘制。 比如在命令窗口或者.m 文件中写如下函数: x = 0:pi/100:2*pi; y = sin(x); 安安富富莱莱 U U U UM M M M4 4 4 40 0 0 03 3 3 3D DS SP P 教教程程 S S S ST T T TM M M M3 3 3 32 2 2 2- - - -V V V V5 5 5 5 开开发发板板系系统统篇篇手手册册 2 20 01 15 5年年0 01 1月月1 15 5日日 版版本本:1 1. .0 0 第第 5 5 页页 共共 1 13 3 页页 plot(x,y) xlabel(x = 0:2pi) ylabel(Sine of x) title(Plot of the Sine Function,FontSize,12) 下面这个函数可以实现在一个图片上显示多个曲线。 x = 0:pi/100:2*pi; y