The Wayback Machine - https://web.archive.org/web/20110419103545/http://www.mathworks.com:80/help/techdoc/matlab_prog/f10-60352.html
Skip to Main Content Skip to Search
Product Documentation

Functions

Function Structure

An MATLAB function consists of the components shown here:

function [x, y] = myfun(a, b, c)   % Function definition line
% H1 line -- A one-line summary of the function's purpose.
% Help text -- One or more lines of help text that explain
%   how to use the function. This text is displayed when
%   the user types "help functionname".

% The Function body normally starts after the first blank line.
% Comments -- Description (for internal use) of what the
%   function does, what inputs are expected, what outputs
%   are generated. Typing "help functionname" does not display 
%   this text.

x = prod(a, b);                    % Start of Function code

For more information: See Basic Parts of a Function in the MATLAB Programming Fundamentals documentation.

Using Lowercase for Function Names

Function names appear in uppercase in MATLAB help text only to make the help easier to read. In practice, however, it is usually best to use lowercase when calling functions.

Case requirements depend on the case sensitivity of the operating system you are using. As a rule, naming and calling functions using lowercase generally makes them more portable from one operating system to another.

Getting a Function's Name and Path

To obtain the file name for the function currently being executed, use the following function in your code.

mfilename

To include the path along with the file name, use:

x = mfilename('fullpath')

For more information: See the mfilename function reference page.

What Files Does a Function Use?

For a simple display of all functions referenced by a particular function, follow the steps below:

  1. Type clear functions to clear all functions from memory (see Note below).

  2. Execute the function you want to check. Note that the function arguments you choose to use in this step are important, since you can get different results when calling the same function with different arguments.

  3. Type inmem to display all MATLAB function files that were used when the function ran. If you want to see what MEX-files were used as well, specify an additional output, as shown here:

       [mfiles, mexfiles] = inmem

Dependent Functions, Built-Ins, Classes

For a much more detailed display of dependent function information, use the depfun function. In addition to MATLAB function files, depfun shows which built-ins and classes a particular function depends on.

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A; sessions led by MATLAB experts.

 © 1984-2011- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS