MATLAB Functions

From ControlTheoryPro.com

Jump to: navigation, search
Symbol.gif
MATLAB Functions
Green carrot left.gif
All MATLAB Articles m-files
Green carrot.jpg
In order to prevent spam, users must register before they can edit or create articles.

Contents

1 Introduction

Like any other programming language, functions are at the heart of reusability and by extension quick turn around. They provide a means for automating repetitive tasks such as Bode plots, curve fits, and controller creation.

2 Requirements for a function

Custom functions follow this syntax in their most basic form:

function [output1, output2, ...]= function_name(input_arg1,input_arg2)
        statements
return;

In current versions of MATLAB the return; line is not required. The function_name can be anything you like but it is best if the m-file name is function_name.m. Calling the function from the command line or another m-file is done by invoking the m-file name of the function with the necessary input and output arguments.

Within the function itself, there must be a statement that defines each of the output arguments (output1, output2, etc.). Without some declaration the variable for the output argument doesn't exist in the function's workspace. This will cause an error about "one or more output arguments". It is good practice to initialize the output arguments are the beginning of the function.

Typically output arguments are initialized to empty ([]) or 0 or -1 or something equivalent for other data types. The reason is that if the function encounters an error you've anticipated then the function can return (via the return command) with those default values. If the initialization value is an invalid value then it can easily be checked by the calling function for any errors which may not throw a MATLAB error.

2.1 Path

In order to invoke a function that function's m-file must be in the current path. There is a default path that can be setup through the File menu or the addpath command. The order of the path is important as MATLAB searches the path in order and stops searching after it finds the 1st instance of that m-file name.

The current path is

  • the current directory (which can be seen at the top of the MATLAB window or by typing pwd at the command prompt
  • the default path

Note that MATLAB will always search the current directory before searching any of the rest of the path.

3 nargin & nargout

The nargin and nargout commands are only valid inside functions since scripts are not passed any arguments. The nargin command returns the number of passed input arguments. This is useful in conjunction with nargchk

 nargchk(min, max, nargin)

where min is the minimum number of arguments necesary for the function to operate and max is the maximum number of valid input arguments.

The nargout command is useful for determining which output arguments to return. Typically, the outputs are the end results of some algorithm and they are easily calculated. However, in some instances secondary output arguments can be time consuming to calculate or require more input arguments than the primary output arguments do. So the function can check the number of output arguments being requested through the nargout command. If the caller isn't saving the secondary output arguments then they do not need to be calculated.

4 varargin & varargout

When using MATLAB objects and functions they often allow the user to set properties. The functions and objects come with default values for these properties but the user is allowed to override these defaults. This is accomplished through the use of varargin. varargin is a cell array that is usually parsed where varargin{i} is a property and varargin{i+1} is the value the user wishes for that property. The parsing is done with a for or while loop and a switch statement.

 function [out] = myFunc(in, varargin)

The varargout output argument option allows for a variable number of output arguments just as varargin allows for a variable number of input arguments. From the MATLAB site

 function [s,varargout] = mysize(x)
 nout = max(nargout,1)-1;
 s = size(x);
 for k=1:nout, varargout(k) = {s(k)}; end
   returns the size vector and, optionally, individual sizes. So
 [s,rows,cols] = mysize(rand(4,5));
   returns s = [4 5], rows = 4, cols = 5.

5 assignin

TODO

TODO
Fill in details on using assignin


6 evalin

TODO

TODO
Fill in details on using evalin