Page 76 - A Guide to MATLAB for Beginners and Experienced Users
P. 76

More about M-Files        57


                     If you have not previously defined u, then typing scriptex2 will produce an
                     error message. However, if you type scriptex2 after running scriptex1,
                     then the definition of u from the first script will be used in the second script
                     and the output n=4 will be displayed.
                       If you don’t want the output of a script M-file to depend on any earlier compu-
                     tations in your MATLAB session, put the line clear all near the beginning
                     of the M-file, as we suggested in Structuring Script M-files in Chapter 3.


           Variables in Function M-Files

                     The variables used in a function M-file are local, meaning that they are un-
                     affected by, and have no effect on, the variables in your Workspace. Consider
                     the following function M-file, called sq.m:

                       function z = sq(x)
                       % sq(x) returns the square of x.
                       z = x.ˆ2;

                     Typing sq(3) produces the answer 9, whether or not x or z is already defined
                     in your Workspace, and neither defines them, nor changes their definitions, if
                     they have been previously defined.


           Structure of Function M-Files
                     The first line in a function M-file is called the function definition line; it defines
                     the function name, as well as the number and order of input and output argu-
                     ments. Following the function definition line, there can be several comment
                     lines that begin with a percent sign (%). These lines are called help text and
                     are displayed in response to the command help. In the M-file sq.m above,
                     there is only one line of help text; it is displayed when you type help sq.
                     The remaining lines constitute the function body; they contain the MATLAB
                     statements that calculate the function values. In addition, there can be com-
                     ment lines (lines beginning with %) anywhere in an M-file. All statements in
                     a function M-file that normally produce output should end with a semicolon
                     to suppress the output.
                       Function M-files can have multiple input and output arguments. Here is
                     an example, called polarcoordinates.m, withtwo input and two output
                     arguments:

                       function [r, theta] = polarcoordinates(x, y)
                       % polarcoordinates(x, y) returns the polar coordinates
                       % of the point with rectangular coordinates (x, y).
   71   72   73   74   75   76   77   78   79   80   81