Matlab Tutorial Session 4
This tutorial session covers the following topics
1.
Variable number of arguments in functions
2.
Return
,
break
and
continue
commands
3.
Advanced input and output including file input and output
4.
find
and
findstr
commands
5.
Example programs:
Output for Tecplot from Matlab, Reading a Tecplot datafile
in Matlab
Next tutorial will cover (i) cell arrays and structures,
(ii) differential equation solvers,
and
(iii) introduction to symbolic math toolbox.
Review of Session 3
Functions in Matlab are written in a separate M-file.
This M-file begins with the
header
function
output
=
functionname
(
input1
,
input2
, …,
inputN
)
The
output
may be a single variable (such as
function
T
=
trace
(
M
)
), or there
may be multiple output variables specified as a vector (such as
function
[r,
theta]
=
polar
(
cpx
)
).
The input and output variables are not pre-typed to be integers, strings etc.
The
function is written assuming proper types of variables and it is up to the user (or
calling program) to provide the function with proper arguments and collect the
outputs.
Otherwise the user will experience error in execution.
A function has no access to any variables outside it.
Everything required must be
passed as arguments.
There is an exception to this, which is to specifically define
certain variables in the workspace as
global
using the
global
command.
This is
discussed in this session.
Function M-file must be in the current directory or in matlab path so that matlab
can find it.
Otherwise matlab issues an error that such a function does not exist.
Subfunction may be placed in a function M-file at the end.
But they cannot
access the main function’s variables.
They must again be passed everything as
input arguments.
A function may be executed by the following syntax in the program:
[out1, out2 , …, outM] = functionname ( inp1, inp2, …, inpN)
or by using the feval command
[out1, out2 , …, outM] =
feval
(
‘functionname’
,inp1,inp2,…,inpN)