Matlab Function

MATLAB syntax is quite peculiar compared to other programming languages. We can return one or more values from a function. We can also pass one or more arguments/variables while calling a function. MATLAB functions must be defined in separate files and function name must match with the file name. I'm writing 2 functions in matlab, an initialize function and a function to insert items into an array treating it like a doubly-linked list. However, my initialize function only returns 'ans =' and the initialized array. To determine which function to call when multiple functions in the current scope have the same name, MATLAB uses function precedence order. Update Code for R2019b Changes to Function Precedence Order; Add Help for Your Program. Help text appears in the Command Window when you use the help function. Create help text for your program by inserting. The 'recipe' for a function (the function's code) is always stored in a '.m' file. In Matlab, there should only be one function per file. Important: The name of the.m file and the function should be the same! A Very Simple Function File. Here is an example of a function that computes the average of two numbers.

MATLAB Function Reference
function_handle (@)

Handle used in calling functions indirectly

Syntax

Description

handle = @functionnamereturns a handle to the specified MATLAB function.

A function handle is a MATLAB value that provides a means of calling a function indirectly. You can pass function handles in calls to other functions (often called function functions). You can also store function handles in data structures for later use (for example, as Handle Graphics callbacks). A function handle is one of the standard MATLAB data types.

At the time you create a function handle, the function you specify must be on the MATLAB path and in the current scope. This condition does not apply when you evaluate the function handle. You can, for example, execute a subfunction from a separate (out-of-scope) M-file using a function handle as long as the handle was created within the subfunction's M-file (in-scope).

handle = @(arglist)anonymous_functionconstructs an anonymous function and returns a handle to that function. The body of the function, to the right of the parentheses, is a single MATLAB statement or command. arglist is a comma-separated list of input arguments. Execute the function by calling it by means of the function handle, handle.

Remarks

The function handle is a standard MATLAB data type. As such, you can manipulate and operate on function handles in the same manner as on other MATLAB data types. This includes using function handles in structures and cell arrays:

However, standard matrices or arrays of function handles are not supported:

For nonoverloaded functions, subfunctions, and private functions, a function handle references just the one function specified in the @functionname syntax. When you evaluate an overloaded function by means of its handle, the arguments the handle is evaluated with determine the actual function that MATLAB dispatches to.

Use isa(h, 'function_handle') to see if variable h is a function handle.

Examples

Example 1 -- Constructing a Handle to a Named Function

The following example creates a function handle for the humps function and assigns it to the variable fhandle.

Pass the handle to another function in the same way you would pass any argument. This example passes the function handle just created to fminbnd, which then minimizes over the interval [0.3, 1].

The fminbnd function evaluates the @humps function handle. A small portion of the fminbnd M-file is shown below. In line 1, the funfcn input parameter receives the function handle @humps that was passed in. The statement, in line 113, evaluates the handle.

Example 2 -- Constructing a Handle to an Anonymous Function

The statement below creates an anonymous function that finds the square of a number. When you call this function, MATLAB assigns the value you pass in to variable x, and then uses x in the equation x.^2:

The @ operator constructs a function handle for this function, and assigns the handle to the output variable sqr. As with any function handle, you execute the function associated with it by specifying the variable that contains the handle, followed by a comma-separated argument list in parentheses. The syntax is

To execute the sqr function defined above, type

Because sqr is a function handle, you can pass it in an argument list to other functions. The code shown here passes the sqr anonymous function to the MATLAB quad function to compute its integral from zero to one:

See Also

str2func, func2str, functions, isa


functionfunctions

© 1994-2005 The MathWorks, Inc.


MATLAB Function Reference
dir

Display directory listing

Graphical Interface

As an alternative to the dir function, use the Current Directory browser.

Function

Syntax

Description

dirlists the files in the current working directory.

Matlab function call

dir namelists the specified files. The name argument can be a pathname, filename, or can include both. You can use absolute and relative pathnames and wildcards (*).

files = dir('directory')returns the list of files in the specified directory (or the current directory, if dirname is not specified) to an m-by-1 structure with the fields
name
Filename
date
Modification date
bytes
Number of bytes allocated to the file
isdir
1 if name is a directory; 0 if not

Examples

List Directory Contents

To view the contents of the matlab/audio directory, type

Matlab Function Syntax

Using Wildcard and File Extension

To view the MAT files in your current working directory that include the term java, type

MATLAB returns

Using Relative Pathname

To view the M-files in the MATLAB audio directory, type

MATLAB returns

Matlab Function Block

Returning File List to Structure

To return the list of files to the variable audio_files, type

MATLAB returns the information in a structure array.

Matlab Function List

Index into the structure to access a particular item. For example,

See Also
cd, copyfile, delete, fileattrib, filebrowser, ls, mkdir, movefile, rmdir, type, what

Matlab Function Ga


diffdisp