Editing Files, Script Files, Function Files, and Inline Functions
1. Editing files
2. Script M-files
3. Function M-files
4. Inline functions
1. Editing files
MATLAB offers the possibility to store commands in special files - called
M-files – with the ending .m. There are two types of M-files: Scripts and
Functions. To edit an M-file, select ‘New’ or ‘Open’ from the file-menue.
In each case the MATLAB Editor window will be opened and you can continue
by editing either a new file or, after opening, an existing file. When
the MATLAB Editor is open, you can load further files or save and close
opened files by selecting the appropriate item from the file menue of the
Editor. Select ‘Save As’ and the browser if you save a new file or if you
want to change the name of an existing file, or ‘Save’ if you have edited
an existing file whose name you want to leave unchanged. The directory
in which the files should be saved is usually the working directory: in
our case either the temp-directory or the chosen directory on your floppy.
2. Script M-files
In a Script M-file you just write a sequence of commands separated by semicolons (result not displayed) or commas (result displayed). A Script M-file is treated as if the commands were written in the Command window. All variables defined in the workspace are accessed and the computations are carried out in the workspace.
Example: Assume you wish to multiply a complex number c by eiq and extract magnitude and polar angle of the product. If both c and q are defined in the workspace under the variable names c and theta, type the following commands into a file,
save the file as product.m and call it at appropriate stages of the calculation. You then have access in the workspace to the variables magn and phi which are assigned to the computed values. For calling the Script M-file there are three options:
disp('Plotting the sine
If this file is saved as sinescript.m
and executed, you are informed that Plotting
the sine is fun in the command window, and two sine curves
in the interval -2p £
x £ 2p
are plotted. Here the command hold
on has the effect that previous plots in the figure window
are kept when a new plotting command is exectuted. Type hold
off if you want to clear the window for the next plot.
The command fplot is
reserved for Matlab functions which can be buildt in functions such as
as well as user defined function M-files (see below). Note the apostrophes:
the first argument passed to fplot
is the filename and therefore must be a character string.
3. Function M-files
These files differ from Script M-files in so far that the commands are not executed in the present workspace, but in a separate workspace which is created whenever the function is called. As a consequence, you have to transfer variables from your workspace to the function workspace and specify what has to be returned, i.e you have to specify input and output arguments.
The first (command) line of a Function M-file MUST be a function declaration line specifying input and ouput variables.Below this line any sequence of MATLAB commands can appear, in which the inputs are processed and the outputs should be defined. You have to make sure that no undefined variable occurs as input to these commands!
You can use Function M-files for programming in MATLAB as well as in the mathematical sense, i.e. for numerical implementation of formulae.
Example: Let’s start with a simple example, the (mathematical) function y(x)=x2. To implement this function through a Function M-file, simply type
in a file and save the file as square.m. In the workspace (or a Script M-file) you can then evaluate the function by
or by square(varname) if varname is assigned to a numerical value. By writing .^ instead of just ^, the function is "array smart", i.e. one can pass arrays as argument as well. For example,
» square([1 2 3])
1 4 9
returns the array consisting of the squares of the the input array [1 2 3]. The command
» fplot('square',[-1 1])
will generate a graph of y(x)=x2 in the interval -1 £ x £ 1.
Instead of the simple output above, one can perform any sequence of arithmetic operations which may also involve functions buildt into MATLAB or user defined functions.
Example: y(x)=[e-x/(x2+1) + sin2(x)]2 + 0.2 can be implemented array smart as
After saving the file as fun.m you can evaluate it again via fun(var) where var is either a number, an array, or a variable assigned to a numerical value or an array. Note that the variables u,v,w– defined internally in the function workspace – are not returned to your workspace.
A Function M-file can accept any kind of input variables, generate any desired type of output variables, and may contain any sequence of commands, including plots and control flow commands such as for, if else, while etc.
4. Inline Functions
An alternative to function M-files is provided by objects called inline functions. Inline functions are like function M-files, i.e. they accept (usually numerical) input and return output. The difference to function M-files is that the function evaluation takes place in the current workspace, whereas function M-files operate in separate workspaces.
Example: Let's define the function y(x)=(1+x)2/(1+x2) as inline function:
y(x) = (1+x).^2./(1+x.^2)
In the first command the formula is edited as a string, i.e. it is quoted by apostrophes. In the second command the string is evaluated and converted to an inline function. With the third argument we fix the parameter of the inline function. Note that all arguments passed to inline must be strings. Since the answer of the first command is already a string, the apostrophes must be omitted in the argument ans, but the apostrophe quotation must be used in the variable declaration 'x'. The above two commands can also be combined in the single command
The definition of the inline function was made array smart:
» y(1),y([1 2 3])
2.0000 1.8000 1.6000
Inline functions can be exploited in a similar manner as Function M-files. Specifically we can apply the fplot command:
» fplot(y,[-1 1])
will generate a graph of the function in the range -1 £ x £ 1. Note that here y must not be quoted by apostrophes, in contrast to plots of functions defined in Function M-files.
Inline functions can depend on several arguments.
Example: Let's implement the mathematical function y(x,a,b)=(1+ax)2/[b(1+x2)] as inline function:
y(x,a,b) = (1+a*x).^2/b./(1+x.^2)
This function is array smart with respect to x (but not with respect to a,b):
» y([1 2],1,1)
provide information about the variables on which y
depends and the formula through which y