Scripts and Functions
Although a lot can be done by writing instructions / steps on the command window in MATLAB, often one needs to experiment with a sequence of commands (for example, to see what change in one of the inputs does to a output - say, call option value on change of volatility or interest rate). Also, it would be very inefficient if every time one has to re-enter a particular sequence of commands in course of a project (an example could be comparing short term movements to a long term average by, say, estimating a rolling moving average).
All of this, and more, can be achieved by what are called M-files. An M-file is just a text-editor (like a notepad) where one can record/type a set of commands and call from the command line. All M-files have a .m extension (just like a notepad has a .txt extension) and they are of two types:
Script M-files (or scripts): They have no input / output arguments and operate on variables on the existing workspace
Function M-files (or functions): They can accept input arguments and return output arguments. They are differentiated from the scripts in that a function contains an explicit function definition at the top of the M-file ( of the kind function [output] = myFunctionName(input) ). Also, internal variable declared within a function are local to the function (unless declared global).
Both a script and a function enable one to store a sequence of commands / instructions that one may need frequently. Also, in general M-files are a more efficient and neat way of passing instructions as they help keep the command window uncluttered while one fiddles around with inputs and the sequence of commands.
As an example, let's say we want a function that can find out the location of the maximum value in a matrix:
function [row, col, val] = maxVal(A)
% maxVal(x)
% Finds the location(s) and value of the maximum entry in a matrix
%
% [row, col, val] = maxVal(A)
%
% 'A' is the input matrix
% 'row' gives the row indices of the maxima (in case not unique)
% 'col' gives the column indices of the minima (in case not unique)
% 'val' gives the maximum value
val = max(max(A));
[row, col] = find(A == val);
In this function there are three output arguments and a single input argument. We'll discuss more on multiple inputs and output later.
It's always advisable to include comment lines after the function definition. This not only makes sure you remember what you were trying to do when you wrote the function in the first place, it also allows for more effective sharing of codes across users.
To see if a function name or a variable already exists as an existing M-file or a MATLAB built-in function, use the command:
>> exist('functionName')
If the function or file doesn't exist, the above command would give an output 0, else a value between 1 - 8. See help exist for details.
One should also avoid using MATLAB keywords for a function name. To see the list of keywords, use the following command:
>> iskeyword
Scripts vs Functions
Scripts, simply stated are just a set of commands written in an M-file instead of the command window. Commands written in a script works on the variables existing in the workspace. Functions in that sense are different. For one, the same variable names as in the workspace can be used within a function without risking any modification of the existing variables in the workspace. Also, any variables created when the function is running are lost when the function completes. [HT: Kevin Sheppard]
No comments:
Post a Comment