You are on page 1of 13

Saving a program: script files

You need to save the program if you want to use it again later. To create a file with
a list of commands, save it, and then run the file you need to use a script file, or
simply a script.
Script files are also called M-files because the extension .m is used when theyare
To create M-file click the File menu, select New, and then select M-file or click
on NewM-file icon in the toolbaror simply type edit, at the command prompt.
The special significance of a script file is that, if you enter its name at the command-
line prompt, MATLAB carries out each statement in the script file as if it were
entered at the prompt.
M-file can contain comments. Any text following a percent sign (%) on a given line
is comment text. Comments can appear on lines by themselves, or you can append
them to the end of any executable line.
Function file
A function file that is created by the user can be used like a built-in function. The
function can be a simple single mathematical expression or a complicated and
involved series of calculations.
The main feature of a function file is that it has an input and an output.This means
that the calculations inside the function file are carried out using the input data, and
the results of the calculations are transferred out of the function file by the output.The
input and the output can be one or several variables, and eachcan be a scalar, vector,
or an array of any size.

Function files are created and edited like script files. This window is opene d from
the Command Window. In the File menu, selectNew, and then select M-file.
Structure of function file
Page 1 of 13
The structure of a typical function file consists of the following parts:
Function Definition Line. .1
Input and Output Arguments. .2
Function Body. .3

Function Definition Line .4

The first executable line in a function file must be the function definition line.The
form of the function definition line is:

The word function, typed in lower case letters, must be the first word in the function
definition line.

Input and Output Arguments .1

Page 2 of 13
The input and output arguments are used to transfer data into and out of the
function.The following are examples of function definition lines with different
combinations of input and output arguments.

The function body .2

The function body contains the computer program (code) that actually performs the
computations.This code includes calculations, assignments, any built-in or user-
defined functions, and interactive input and output.
A function file must be saved before it can be used. It is highlyrecommended that
the file is saved with a name that is identical to the function name in the function
definition line. In this way the function is called (used) by using the function name.
(If a function file is saved with a different name, the name it is saved under must be
used when the function is called.) Function files are saved with the extension .m.

All the variables in a function file are local (the input and output arguments andany
variables that are assigned values within the function file). This means that the

Page 3 of 13
variables are defined and recognized only inside the function file. When a function
file is executed, MATLAB uses an area of memory that is separate from the
workspace (the memory space of the Command Window and the script files).
Each function file has its own local variables which are not shared with
otherfunctions or with the workspace of the Command Window and the script files.
It is possible, however, to make a variable common (recognized) in several different
function files, and perhaps in the workspace too. This is done by declaring the
variable global with the global command.
For example: global x y z

Page 4 of 13

Page 5 of 13
Relational operators:
Relational operators in MATLAB are:

two numbers When .1

are compared, the result is 1 (logical true) if the comparison, according to the
relational operator, is true, and 0 (logical false) if the comparison is false.
If two scalars are compared, the result is a scalar 1 or 0. If two arrays are .2
compared (only arrays with the same size can be compared), the comparison
is done element-by-element, and the result is a logical array of the same size
with 1 's and 0's according to the outcome of the comparison at each address.
If a scalar is compared with an array, the scalar is compared with every .3
element of the array, and the result is a logical array with 1s and 0's
according to the outcome of the comparison of each element.

Page 6 of 13
for example:

Page 7 of 13
Logical Operators:
Logical operators in MATLAB are:

Page 8 of 13
The logical operations ANDandOR can have both operands as scalars, arrays, .1
or one array and one a scalar. If both are scalars, the result is a scalar 0 or 1.
If both are arrays they must be of the same size and the logical operation is
done element-by-element. The result is an array of the same size with l's and
0's according to the outcome of the operation at each position. If one operand
is a scalar and the other is an array, the logical operation is done between the
scalar and each of the elements in the array and the outcome is an array of the
The logical operation NOT has one operand. When it is used with a scalar the .2
outcome is a scalar 0 or 1. When it is used with an array, the outcome is an
array of the same size with 1 's in positions where the array has nonzero
numbers and O's in positions where the array has zeros.

Page 9 of 13
Page 10 of 13
Order of precedence:
The following is the order used by MATLAB:

For example:

Page 11 of 13
In MATLAB a zero of a function can be determined with the command (built- .3
in function) fzero that has the form:

To display a mix of text and a number (value of a variable), the fprintf .4

command has the form:

Page 12 of 13
The dispcommand is used to display the elements of a variable without .5
displaying the name of the variable, and to display text. The format of the disp
command is:

Page 13 of 13