MATLAB函数简介 到目前为止,我们看到的所有的M文件都是脚本文件。脚本文件只是用于存储MATLAB语句。当一个脚本文件被执行时,和直接在命令窗口中直接键入MATLAB语句所产生的结果是一样的。脚本文件分享命令窗口中的工作区,所以所有的在脚本文件运行之前定义的变量都可以在脚本文件中运行,所有在脚本文件中创建的变量在脚本文件运行之后仍然存在工作区。一个脚本文件没有输入参数,也不返回结果。但是所有脚本文件可以通过存于工作区中的数据进行交互。 相对地,MATLAB函数是一种特殊形式的M文件,它运行在独立的工作区。它通过输入参数列表接受输入数据,它通过输出参数列表返回结果给输出参数列表。MATLAB函数的基本形式如下: function [outarg1, outarg2, ...] = fname(inarg1,inarg2, ...) %H1 comment line %Other comment lines ... (Executable code) ... (return)
function语句标志着这个函数的开始。它指定了函数的名称和输入输出列表。输入函数列表显示在函数名后面的括号中。输出函数列表显示在等号左边的中括号中。(如果只有一个输出参数,中括号可以省略。) 输入参数列表是名字的列表,这些名字代表从调用者到函数的值。这些名字被称作形参。当函数被调用时,它们只是从调用者得来实际变量的占位符而已。相似地,输出参数列表也形参组成,当函数结束运行时,这些形参是返回到调用者的值的占位符。 在一个表达式中,调用一个函数需要用到实参列表。在命令窗口直接(或在脚本文件中,另一个函数中)键入函数的名字就可以调用这个函数了。当调用一个函数时,第一个实参的值用在第一个形参的位置,而且其余的形参和实参都一一对应。 函数的执行从函数的顶部开始,结束于return语句或函数的终点。因为在函数执行到结尾就会结束,所以return语句在大部分的程序中没有必要使用。在输出参数列表中每一个项目都必须出现在function语句中的左边。当函数返回时,存储于输出函数列表的值就会返回给调用者,用于下一步的运算。 在一个函数中的初始注释行有特定的目的。在function语句的第一个行注释被称为H1注释行。它应当是对本函数功能的总结。这一行的重要性在于,通过lookfor命令它能被搜索到并显示出来。从H1注释行到第一个空行或第一个可执行性语句可以通过help命令或帮助窗口搜索到。它们则应包含如何使用这个函数的简单总结。 下面是一个自定义函数的简单例子。函数dist2用于计算笛卡尔坐标系中点(x1,y1)与点(x2,y2)之间的距离。(把以下代码保存成dist2.m文件) function distance = dist2 (x1, y1, x2, y2) %DIST2 Calculate the distance between two point % Function DIST2 calculates the distance between % two points (x1, y1) and (x2,y2) in a cartesian % coordinate system. % % Calling sequence: % res =dist2(x1, y1, x2, y2) % % Define variables: % x1 --x-positionof point 1 % y1 --y-positionof point 1 % x2 --x-positionof point 2 % y2 --y-positionof point 2 % distance --Distancebetween points % % Record of revisions: % Date Pragrammer Description of change % ======== ========== ================ % 12/15/98 S.J.Chapman Original code % % Calculate distance. distance = sqrt((x2-x1).^2 + (y2-y1).^2);
这个函数有4个输入参数各和1个输出参数。一个简单的利用这个函数的例子显示如下: % Scriptfile: test_dist2.m % % Purpose: % Thisprogram test2 function dist2. % % Recordof revisions: % Date Pragrammer Description of change % ======== ========== ================ % 12/15/98 S.J.Chapman Original code % % Definevariables: % ax --x-position of point a % ay --y-position of point a % bx --x-position of point b % by --x-position of point b % % Getinput data. disp('Calculate the distance between twopoints:'); ax = input ('Enter x value of point a:'); ay = input ('Enter y value of point a:'); bx = input ('Enter x value of point b:'); by = input ('Enter y value of point b:'); % Evaluatefunction result = dist2 (ax, ay, bx, by); % Writeout result. fprintf('The distance between points a and b is %f\n', result);
当脚本文件被执行时,它的结果显示如下: >> test_dist2 Calculate the distance between two points: Enter x value of point a:1 Enter y value of point a:1 Enter x value of point b:4 Enter y value of point b:5 The distance between points a and b is 5.000000
通过手动运算我们可知程序运算的结果是正确的。 函数dist2也支持MATLAB帮助子系统。如果你键入“help dist2”,将会得到的结果是: >> help dist2 DIST2Calculate the distance between two point FunctionDIST2 calculates the distance between two points(x1, y1) and (x2,y2) in a cartesian coordinatesystem. Callingsequence: res =dist2(x1, y1, x2, y2) Definevariables: x1 --x-position of point 1 y1 --y-position of point 1 x2 --x-position of point 2 y2 --y-position of point 2 distance --Distance between points Record ofrevisions: Date Pragrammer Descriptionof change ======== ========== ================ 12/15/98 S.J.Chapman Originalcode Calculatedistance.
相似地,键入“lookfor dist2”后将会产生如下的结果: >>lookfor dist2 DIST2 Calculate the distance between two point test_dist2.m: % Scriptfile: test_dist2.m >>lookfor distance DIST2 Calculate the distance between two point
为了仔细观察工作区在函数执行前后的变化,我们将在MATLAB调试器中加载函数dist2和脚本文件test_dist2。在函数加载前,加载中,加载后设置断点(如图5.1所示)。 当程序中止在函数调用之前的断点,它的工作区如图5.2(a)所示。注意工作区中只有变量ax,ay,bx和by。当程序中止在函数调用过程中的断点,它的工作区如图5.2(b)所示。注意工作区中只有变量x1,x2,y1,y2和distance。当程序中止在函数调用后的断点,它的工作区如图5.2(c)所示。注意工作区中原来的变量又重复出现,再加上函数返回的变量result。这些图显示了MATLAB调用M文件的过程中工作区的变化。
图5.1 M文件和函数dist2将会被加载到调试器,在函数调用前,调用过程中,调用后设置合适断点
图5.2(a)
图5.2(b)
图5.2(c) 图5.2(a)在函数调用之前的工作区(b)函数调用过程中的工作区(c)函数调用之后的工作区
|