MATLAB Programming

MATLAB Programming MATLAB Programming

cda.psych.uiuc.edu
from cda.psych.uiuc.edu More from this publisher
23.06.2015 Views

4 M-File Programming Function Definition Line The function definition line informs MATLAB that the M-file contains a function, and specifies the argument calling sequence of the function. The function definition line for the fact function is All MATLAB functions have a function definition line that follows this pattern. Function Name. Function names must begin with a letter, may contain any alphanumeric characters or underscores, and must be no longer than the maximum allowed length (returned by the function namelengthmax). Because variables must obey similar rules, you can use the isvarname function to check whether a function name is valid: isvarname myfun Although function names can be of any length, MATLAB uses only the first N characters of the name (where N is the number returned by the function namelengthmax) and ignores the rest. Hence, it is important to make each function name unique in the first N characters: N = namelengthmax N = 63 Note Some operating systems may restrict file names to shorter lengths. The name of the text file that contains a MATLAB function consists of the function name with the extension .m appended. For example, average.m 4-10

Working with M-Files If the filename and the function definition line name are different, the internal (function) name is ignored. Thus, if average.m is the file that defines a function named computeAverage, you would invoke the function by typing average Note While the function name specified on the function definition line does not have to be the same as the filename, it is best to use the same name for both to avoid confusion. Function Arguments. If the function has multiple output values, enclose the output argument list in square brackets. Input arguments, if present, are enclosed in parentheses following the function name. Use commas to separate multiple input or output arguments. Here is the declaration for a function named sphere that has three inputs and three outputs: function [x, y, z] = sphere(theta, phi, rho) Ifthereisnooutput,leavetheoutputblank function printresults(x) or use empty square brackets: function [] = printresults(x) The variables that you pass to the function do not need to have the same name as those in the function definition line. The H1 Line The H1 line, so named because it is the first help text line, is a comment line immediately following the function definition line. Because it consists of comment text, the H1 line begins with a percent sign, %. Fortheaverage function, the H1 line is % AVERAGE Mean of vector elements. 4-11

Working with M-Files<br />

If the filename and the function definition line name are different, the<br />

internal (function) name is ignored. Thus, if average.m is the file that defines<br />

a function named computeAverage, you would invoke the function by typing<br />

average<br />

Note While the function name specified on the function definition line does<br />

not have to be the same as the filename, it is best to use the same name for<br />

both to avoid confusion.<br />

Function Arguments. If the function has multiple output values, enclose<br />

the output argument list in square brackets. Input arguments, if present, are<br />

enclosed in parentheses following the function name. Use commas to separate<br />

multiple input or output arguments. Here is the declaration for a function<br />

named sphere that has three inputs and three outputs:<br />

function [x, y, z] = sphere(theta, phi, rho)<br />

Ifthereisnooutput,leavetheoutputblank<br />

function printresults(x)<br />

or use empty square brackets:<br />

function [] = printresults(x)<br />

The variables that you pass to the function do not need to have the same<br />

name as those in the function definition line.<br />

The H1 Line<br />

The H1 line, so named because it is the first help text line, is a comment<br />

line immediately following the function definition line. Because it consists<br />

of comment text, the H1 line begins with a percent sign, %. Fortheaverage<br />

function, the H1 line is<br />

% AVERAGE Mean of vector elements.<br />

4-11

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!