# Introduction to M Function Programming

M-Files
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
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
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
path.
12. Edit function can also be used to create or edit an M-File.

Operators
1. Arithmetic operators:
a. Perform numeric computations.
b. Matrix arithmetic operations are defined by the rules of linear
algebra.
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
operations.
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
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
dimension.

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
numbers.
[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
function.
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.

Structures
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.

# Data Structures

Introduction

1. Data structures are variables that store more than one value.
2. An array is a data structure in which all of the values are logically related.
3. A cell array is a kind of data structure that stores values of different types.
4. Cell arrays can be vectors or matrices; the different values are referred to as the elements of the array.
5. Structures are data structures that group together values that are logically related, but are not the same thing and not necessarily the same type.

Creating cell arrays

1. To create cell arrays curly braces are used instead of square brackets.
2. To create a row vector cell array, the values are separated by commas.
3. To create a column vector cell array, the values are instead separated by semicolons.
4. The type of cell arrays is cell.
5. Another method of creating a cell array is to simply assign values to specific array elements and build it up element by element.

Referring to and display elements

1. Using curly braces for the subscripts will reference the contents of a cell; called content indexing.

2. Using parentheses for the subscripts references the cells; this is called cell indexing.

3. The celldisp function displays the contents of all elements of the cell array.

4. To delete an element from a vector cell array or to delete an entire row or column, cell indexing is used.

Storing strings in cell arrays

1. As cell arrays can store different types of values, strings of different lengths can be stored in the elements.
2. The length of each string can be displayed using a for loop to loop through the elements of the cell array.

3. The function cellstr converts from a character array padded with blanks to a cell array in which the trailing blanks have been removed.

4. The char function can convert from a cell array to a character matrix.
5. The function iscellstr will return logical true if a cell array is a cell array of all strings or logical false if not.

Create and Modify Structure Variables

1. Creating structure variables can be accomplished by using the struct function.

2. An alternative method of creating structures is by using the dot operator to refer to the fields within the structure.

3. The disp function will display either the entire structure or an individual field.
4. By using fprintf, only individual fields can be printed.

5. The function rmfield removes a field from a structure.

Passing Structures to Functions

Related structure functions

1. The function isstruct will return logical 1 for true if the variable argument is a structure variable or 0 if not.
2. The isfield function returns logical true if a field name is a field in the structure argument or logical false if not.

3. The fieldnames function will return the names of the fields that are contained in a structure variable.

Vectors of Structures

1. In many applications, including database applications, information would normally be stored in a vector of structures, rather than in individual structure variables.

2. For loop can also be used to display each element in the packages vector.