next up previous contents
Next: Programming Primitives Up: Programming in MATLAB Previous: Creating Your Own

Creating Your Own Functions

A better way to do the clip operation is to create a function M-file that takes two input arguments (a signal vector and a scalar threshold) and returns an output signal vector. We will show that most functions can be written according to a standard format. In order to continue with the clip example, use your editor to create an ASCII file clip.m that contains the following statements:

=0.9 clip.FH.eps

We can break down the M-file clip.m into four elements:

  1. Definition of Input/Output: Function M-file s must have the word function as the very first thing in the file. The information that follows function on the same line is a declaration of how the function is to be called and what arguments are to be passed. The name of the function should match the name of the M-file ; if there is a conflict, it is the name of the M-file which is known to the MATLAB command environment.

    Input arguments are listed inside the parentheses following the function name. Each input is a matrix. The output argument (a matrix) is on the left side of the equals sign. Multiple output arguments are also possible, e.g., notice how the function size(x) in clip.m returns the number of rows and number of columns into separate output variables. Square brackets surround the list of output arguments. Finally, observe that there is no explicit return of the outputs; instead, MATLAB returns whatever value is contained in the output matrix when the function completes. The MATLAB function return just leaves the function, it does not take an argument. For clip the last line of the function assigns the clipped vector to y.

    The essential difference between the function M-file and the script M-file is dummy variables versus permanent variables. The following statement creates a clipped vector wclipped from the input vector win.

    The arrays win and wclipped are permanent variables in the workspace. The temporary arrays created inside clip (i.e., y, nrows, ncols, Lx and i) exist only while clip runs; then they are deleted. Furthermore, these variable names are local to clip.m so the name x could also be used in the workspace as a permanent name. These ideas should be familiar to anyone with experience using a high-level computer language like C, FORTRAN or PASCAL.

  2. Self-Documentation: A line beginning with the % sign is a comment line. The first group of these in a function are used by MATLAB 's help facility to make M-file s automatically self-documenting. That is, you can now type help clip and the comment lines from your M-file will appear on the screen as help information!! The format suggested in clip.m follows the convention of giving the function name, its calling sequence, definition of arguments and a brief explanation.

  3. Size and Error Checking: The function should determine the size of each vector/matrix that it will operate on. This information does not have to be passed as a separate input argument, but can be extracted on the fly with the size function. In the case of the clip function, we want to restrict the function to operating on vectors, but we would like to permit either a row or a column . Therefore, one of the variables nrows or ncols must be equal to one; if not we terminate the function with the bail out function error which prints a message to the command line and quits the function.

  4. Actual Function Operations: In the case of the clip function, the actual clipping is done by a for loop which examines each element of the x vector for its size versus the threshold Limit. In the case of negative numbers the clipped value must be set to -Limit, hence the multiplication by sign(x(n)). This assumes that Limit is passed in as a positive number, a fact that might also be tested in the error checking phase.





next up previous contents
Next: Programming Primitives Up: Programming in MATLAB Previous: Creating Your Own



William Simcoe
Tue Apr 15 18:51:39 EDT 1997