MATLAB Functions

Functions are M-files that can obtain input arguments and return output arguments. The names of the M-file and the function should be the same. Functions perform on variables within their own workspace, which is also called the local workspace, separate from the workspace you access at the MATLAB command prompt, which is known as the base workspace.

Function files are like programs or subroutine in FORTRAN, operations in PASCAL, and functions in C.

A function file starts with a function definition line, which has a well-defined record of inputs and outputs. Without this line, the file develops into a script file.

Syntax

where function_name must be the same as the filename (without .m extension) in which the functions are written. For example, if the name of the functions is projectile, it must be written and stored in a file with the name projectile.m. The function definition line looks slightly different, depending on whether there is no output, a single output, or multiple outputs.

Example

Anatomy of an M-File function

This simple function presents the basic parts of an M-file.

The table below shortly defines each of these M-file part. Both function and script can have all of these elements except for the function definition line which, applies to functions only.

M-File ElementDescription
Function definition line (functions only)It defines the function name, and the number and the order of input and output argument.
H1 lineA one-line summary definition of the program, displayed when you request help on an entire directory, or when you use lookfor.
Help textA more detailed definition of the program, displayed together with the H1 line when you request help on a specific function
Function or script bodyProgram code that executes the actual evaluation and assigns values to any output argument.
CommentsText in the body of the programs that describe the internal working of the program.

Function Definition Line

The function definition line inform MATLAB that the M-file includes a function, and specifies the argument calling sequence of the function. The function definition line for the fact functions are

MATLAB Functions

Function Name

Function names start with a letter, and it includes alphanumeric characters and underscores, or it should be no longer than the highest length (returned by the function namelengthmax). Because variables must same rules, we can use the isvarname functions to analysis whether the function name is valid:

The name of the text file that includes a MATLAB function consists of the function names with the extension .m appended. For example,

If the filenames and the function definition line names are different, the internal (function) name is ignored. Thus, if the average.m is the file that define a function named computeAverage, we would invoke the function by typing

Function Arguments

If the function has multiple output values, enclosed the output argument list in square brackets. Input arguments are enclosed in parentheses following the function name. Use commas to break multiple inputs or output arguments. Here is the declaration for the function name sphere that has three input and three output:

H1 Line

The H1 line is the first help text line, is a comment line immediately following the function definition line. Because it consist of comment text, the H1 line starts with a percent sign, %. For the average functions, H1 line is

This is the first line of the text that appears when a user types help function_name at the MATLAB prompt. Further, the lookfor function searches on and shows only the H1 line. Because this line provides essential summary information about the M-file, it is necessary to make it as descriptive as possible.

Help Text

We can create online help for our M-files by entering help text on one or more consecutive comment lines at the start of our M-file programs. MATLAB examines the first group of consecutive lines immediately following the H1 line that begins with % to be the online help text for the function. The first line without % as the leftmost character ends the help.

The help text for the average functions are

When we type help function_name at the command prompt, MATLAB shows the H1 line followed by the online help text for those functions. The help system ignores any comment line that occurs after this help block.

Function or Script Body

The function body includes all the MATLAB code that performs calculations and assign values to output arguments. The statements in function body can subsist of the function call, programming methods like flow control statement and interactive input and output, evaluations, assignment, comments, and blank-lines.

For example, the body of the average functions includes a number of simple programming statements:

Comments

Comment lines start with a percent sign (%). Comment lines can occur anywhere in an M-file, and we can append comments to the end of a line of code. For example,

% Add up all the vector elements.
y = sum(x)       % Use the sum function.

In addition to comment lines, we can insert blank lines anywhere in an M-file. Blank lines are ignored. However, a blank line can denote the end of the help text entry for an M-file.






Latest Courses