As usual the if statement
evaluates a logical expression and executes a group of statements
when the expression is true. The optional elseif and else keywords provide for
the execution of alternate groups of statements. An end keyword terminates the last group of statements. The
groups of statements are delineated by the four keywords no braces
or brackets are involved.
>> T = [1 1 0 0];
>> if T == 1
disp('Vector of ones.')
elseif T == 0
disp('Vector of zeros.')
disp('Vector of ones and zeros.')
Vector of ones and zeros.
Note that for two matrices A and B , A == B
does not test if they are equal, it tests where they are equal; the
result is another matrix of 0's and 1's showing element-by-element
equality. Only if T == 1 is true for all elements the if clause is executed.
The switch statement executes groups of
statements based on the value of a variable or expression. The
keywords case and otherwise delineate the groups. Only the first
matching case is executed.
>> method = 'Bilinear';
>> switch method
disp('Method is linear')
disp('Method is cubic')
Method is linear
The for loop repeats a group of statements a
fixed, predetermined number of times. A matching end delineates the
>> b = 0; y = hardlim(w*P+b);
>> for i=1:4
if y(i) == 1
The while loop repeats a group of statements an indefinite number
of times under control of a logical condition. A matching end
delineates the statements.
The continue statement
passes control to the next iteration of the for
or while loop in which it appears, skipping any
remaining statements in the body of the loop. In nested loops,
continue passes control to the next iteration
of the for or while loop
The break statement lets you exit early from a
for or while loop. In
nested loops, break exits from the innermost
Files that contain code in the Matlab language
are called M-files. You create M-files using a text editor, then
use them as you would any other Matlab function or command. You can
organize them into other directories and personal toolboxes that
you can add to your Matlab search path. There are two kinds of
Scripts do not accept input arguments or
return output arguments. They can operate on existing data in the
workspace, or they can create new data on which to operate.
Although scripts do not return output arguments, any variables that
they create remain in the workspace, to be used in subsequent
computations. An example is the M-file matlab_intro.m.
Functions, which can accept input arguments
and return output arguments. Functions operate on variables within
their own workspace, separate from the workspace you access at the
Matlab command prompt. As example see the file perc.m.
% PERC(P,T) Train a threshold unit using the
% Delta-Rule until a separating Hyperplane is found.
The first line of a function M-file starts
with the keyword function. It gives the function name and order of
arguments. In this case, there are up to two input arguments and
one output argument. The next several lines, up to the first blank
or executable line, are comment lines that provide the help text.
These lines are printed when you type
>> help perc
The first line of the help text is the H1
line, which Matlab displays when you use the lookfor command or request help on a
An aspect of Matlab functions that is not ordinarily found in other
programming languages is a variable number of arguments. Not all
listed input arguments have to specified during a function call,
>> r = rank(A);
>> r = rank(A,1.e-6);
are both valid commands. Within the body of
the function, two quantities named nargin and
nargout are available which tell you the number
of input and output arguments involved in each particular use of
If you want more than one function to share a single copy of a
variable, simply declare the variable as global
in all the functions, e.g. by typing
>> global P T
Do the same thing at the command line if you
want the base workspace to access the variable. The global declaration must occur before the variable is
actually used in a function.