搜索
您的当前位置:首页正文

第4章Matlab简易使用教程(二)

来源:小奈知识网
第4章Matlab简易使⽤教程(⼆)

第4章M a t l a b简易使⽤(⼆)

本期教程主要是讲解Matlab的简易使⽤⽅法,有些内容跟上⼀节相同,但是⽐上⼀些更详细。 4.1 Matlab的脚本⽂件.m的使⽤4.2 Matlab中的条件和循环函数4.3 绘图功能4.4总结

4.1M a t l a b的脚本⽂件.m的使⽤

在matlab上创建和使⽤.m⽂件跟在MDK或者IAR上⾯创建和使⽤.C或者.ASM⽂件是⼀样的。创建⽅法如下:

点击上图中的⼩图标,打开编辑窗⼝后,输⼊以下函数:% Generate random data from a uniform distribution% and calculate the mean. Plot the data and the mean.n = 50; % 50 data pointsr = rand(n,1);plot(r)

Draw a line from(0,m)to(n,m)m = mean(r);hold onplot([0,n],[m,m])hold off

title('Mean of Random Uniform Data')

编辑好函数后需要将当前⽂件进⾏保存,点击File—>Save as即可,然后点击如下图标即可运⾏(或者按F5):

显⽰效果如下:

4.2M a t l a b中的条件和循环函数

matlab也⽀持类似C语⾔中的条件和循环语句:for, while, if, switch。但在matlab中使⽤⽐在C 中使⽤更加随意。⽐如在.M⽂件中输⼊以下函数:nsamples = 5;npoints = 50;for k = 1:nsamples

currentData = rand(npoints,1);sampleMean(k) = mean(currentData);end

overallMean = mean(sampleMean)在命令窗⼝得到输出结果:

>> Untitled2 %这个是保存的⽂件名,运⾏相应⽂件会⾃动打印出overallMean =

0.4477

为了将上⾯函数每次迭代的结果都进⾏输出可以采⽤如下⽅法:for k = 1:nsamples

iterationString = ['Iteration #',int2str(k)];

disp(iterationString) %注意这⾥没有分号,这样才能保证会在命令窗⼝输出结果 currentData = rand(npoints,1);sampleMean(k) = mean(currentData) %注意这⾥没有分号end

overallMean = mean(sampleMean) %注意这⾥没有分号在命令窗⼝得到输出结果:>> Untitled2Iteration #1sampleMean =

0.4899 0.4642 0.5447 0.4808 0.4758Iteration #2sampleMean =

0.4899 0.4959 0.5447 0.4808 0.4758Iteration #3sampleMean =

0.4899 0.4959 0.4977 0.4808 0.4758Iteration #4sampleMean =

0.4899 0.4959 0.4977 0.5044 0.4758Iteration #5sampleMean =

0.4899 0.4959 0.4977 0.5044 0.5698overallMean =0.5115

●如果在上⾯的函数下⾯加上如下语句:if overallMean < .49

disp('Mean is less than expected')elseif overallMean > .51

disp('Mean is greater than expected')else

disp('Mean is within the expected range')end

命令窗⼝输出结果如下:

overallMean = %这⾥仅列出了最后三⾏0.5381

Mean is greater than expected4.3绘图功能

4.3.1基本的p l o t函数

●根据plot不同的输⼊参数,主要有两种⽅式:

plot(y),这种⽅式的话,主要是根据y的数据个数产⽣⼀个线性曲线。plot(x,y)以x轴为坐标进⾏绘制。⽐如在命令窗⼝或者.m⽂件中写如下函数:x = 0:pi/100:2*pi;y = sin(x);plot(x,y)

xlabel('x = 0:2\\pi') ylabel('Sine of x')title('Plot of the Sine Function','FontSize',12)

下⾯这个函数可以实现在⼀个图⽚上显⽰多个曲线。 x = 0:pi/100:2*pi; y = sin(x); y2 = sin(x-.25); y3 = sin(x-.5);plot(x,y,x,y2,x,y3)

legend('sin(x)','sin(x-.25)','sin(x-.5)')x = 0:2S i n e o f x

另外曲线的样式和颜⾊都可以进⾏配置的,命令格式如下: plot(x, y, 'color_style_marker')

下⾯通过⼏个实例看⼀下实际显⽰效果。 x = 0:pi/100:2*pi;y = sin(x);plot(x,y,'ks')显⽰效果如下:

下⾯函数的显⽰效果:x = 0:pi/100:2*pi;y = sin(x);plot(x,y,'r:+')

下⾯函数的显⽰效果如下:

●复数绘图

默认情况下函数plot只绘制数据的实部,如果是下⾯这种形式,实部和虚部都会进⾏绘制。plot(Z)也就是plot(real(Z),imag(Z))。下⾯我们在命令窗⼝中实现如下函数功能:t = 0:pi/10:2*pi;plot(exp(i*t),'-o')axis equal

显⽰效果如下:

●在当前的绘图中添加新的plot函数

使⽤函数hold on即可实现,这个函数我们在上⼀期中已经使⽤过,作⽤就是在当前绘图的基础上加上⼀个新的绘图。% Obtain data from evaluating peaks function [x,y,z] = peaks;% Create pseudocolor plotpcolor(x,y,z)

% Remove edge lines a smooth colors shading interp% Hold the current graphhold on

% Add the contour graph to the pcolor graph contour(x,y,z,20,'k')% Return to defaulthold off显⽰效果如下:

●Axis设置可见性设置axis on %设置可见axis off %设置不可见⽹格线设置

grid on %设置可见grid off %设置不可见长宽⽐设置

axis square %设置X,Y轴等长axis equal %设置X,Y相同的递增。axis auto normal %设置⾃动模式。设置轴界限

axis([xmin xmax ymin ymax]) %⼆维

axis([xmin xmax ymin ymax zmin zmax]) %三维axis auto %设置⾃动4.3.2绘制图像数据

下⾯通过⼀个简单的实例说明⼀下图像数据的绘制,在命令窗⼝下操作即可。 >> load durer>> whos

Name Size Bytes Class AttributesX 648x509 2638656 doubleans 648x509 2638656 doublecaption 2x28 112 charmap 128x3 3072 double>> image(X) %显⽰图⽚100200300400500600

50100150200250300350400450500 >>colormap(map) %上⾊100200300400500600

50100150200250300350400450500>> axis image %设置坐标

使⽤相同的⽅法,⼤家可以加载图⽚detail进⾏操作。另外⽤户可以使⽤函数imwrite和imread操作 标准的JPEG,BMP,TIFF等类型的图⽚。4.4总结

本期主要跟⼤家讲解了Matlab的简单使⽤⽅法,后⾯复杂的使⽤需要⼤家多查⼿册,多练习。

因篇幅问题不能全部显示,请点此查看更多更全内容

Top