Introduction to M Function Programming

1. M-files can be scripts that simply execute the series of statements.
2. These can also be functions that can accept arguments and can produce
one or more outputs.
3. A text editor is used to create an M-File, and this file can be stored in the
form filename.m.
4. The components of function M-File are
a. The function definition line
b. The H1 line
c. Help text
d. The function body
e. Comments
5. The function definition line has the form
function [outputs] = name (inputs)
6. The H1 line is the first text line which is a single comment line that
follows the function definition line.
7. There can be no blank lines or leading spaces between the H1 line and
the function definition line.
8. Help text is a text block that follows the H1 line, without any blank lines
between the two which is used to provide comments and online help for
the function.
9. The function body contains all the MATLAB code that performs
computations and assigns values to output arguments.
10. The function comment lines are the line preceded by the symbol “%”.
11. M-Files can be created and edited using any text editor and saved with
extension .m in a specified directory, typically in the MATLAB search
12. Edit function can also be used to create or edit an M-File.

1. Arithmetic operators:
a. Perform numeric computations.
b. Matrix arithmetic operations are defined by the rules of linear
c. Array arithmetic operations are carried out element by element and
can be used with multidimensional arrays.
d. The period character “.” distinguishes array operations from matrix
2. Relational operators: compare operands qualitatively.
3. Logical operators: perform the functions AND, OR, and NOT.

Code Optimization
1. Vectorizing Loops
a. Vectorizing simply means converting for and while loops to equivalent
vector or matrix operations.
b. It can result in increase in computation speed as well as helps to
improve the code readability.
2. Preallocating arrays
a. To improve code execution time, the size of arrays can be preallocated.
b. Preallocation simply consists of creating arrays of 0s with proper

Interactive I/O
1. To display information on the screen, disp function is used.
disp (argument)
2. To input data into an M-function, input function is used.
t = input (‘message’)
3. Function strread is used if the entries are a mixture of characters and
[a, b, c, . . .] = strread (cstr, ‘format’, ‘param’, ‘value’)
4. To compare strings, strcmp function is used.
5. The function can be modified to accept lowercase characters by
converting any input to lowercase using lower function.
y = lower (y)
6. Any input character string can be converted to uppercase using upper
y = upper (y)

Cell Arrays
1. A cell array is a multidimensional array whose elements are copies of
other arrays.
2. To select the contents of cell arrays, an integer address is enclosed in
curly braces.
3. The most important property of cell arrays is that they contain copies of
the arguments and not the pointers to the arguments.

1. Structures are similar to cell arrays as they allow grouping of a collection
of dissimilar data into a single variable.
2. The elements of structures are addressed by names called fields.
3. Fields adds clarity and readability to an M-function.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s