Matlab Programming
MATLAB is an integrated environment that is used for solving
many problems in scientific domain. Matlab is an abbreviation of the word –
MATrix LABoratory. It is designed to perform matrix operations. Since images
are 2D and 3D in nature, Matlab is suitable for performing image manipulations.
It is a powerful tool and can be used to implement imaging projects
effectively. MATLAB package comes with several functions
that facilitate the image processing. The power of Matlab comes with a set of
functions called toolboxes. A toolbox is a collection of functions that are
designed to do image processing. Using these functions it is easier to load, save, and perform custom functions on
images. The image processing toolbox allows us to do various tasks such as
1. Reading/ Writing of images
2. Colour space and format conversions
3. Visualization of images
4. Image filtering
5. Image Transforms
6. Image arithmetic
7. Morphological operations
Matlab also comes with toolboxes for
statistics, Wavelet, Neural networks. Apart from the official toolboxes, many
public domain toolboxes are available to perform various tasks.
The subsequent sections discuss some of
the basic programming capability of Matlab.
Basics of Matlab
Based on the versions of Matlab, the visual
appearance of Matlab varies. But essentially the matlab environment has three
basic windows.
1. Command Windows – In this main window,
the matlab command prompt is >>. Matlab is an interactive environment.
The commands can be given one by one and can see the execution
interactively. While this acceptable for simple scripts, for length programs
this is not acceptable. To facilitate the project development, matlab provides
an interactive environment window called Edit Window.
2.
Edit Window – In this window, the programs can be written. The programs can be
entered, edited and executed in this window. One can create programs with .m
extension, called ‘M-files’. One can compare this with C program or Java
program. A M-file is a collection of programming commands just like C programs
to accomplish a task.
3.
Graphics Window – This is the window used to visualize the graphics that are
created in the matlab programs. This window also provides help to manipulate
the figures such as changing the labels for axes, titles and for exporting the
graphics.
Matlab
comes with lot of on-line help – Commands such as helpdesk, help, lookfor and
helpwin to locate the context oriented help.
Data
Types
The
Fundamental data type of Matlab is an array. Since images are matrices, Matlab
is a perfect fit for manipulating images. Apart from array, the matlab also
supports many data types such as integers, double (including floating point
numbers), character strings, structures and cells.
File
Types
Matlab provides three types of files.
M-Files – These are flat Ascii files that
can be created using Matlab environment or any other text editors. M-files are
collection of matlab commands. There are two types of M-files.
1. Script Files
2. Function Files
Mat-Files
– These are native data files with .mat extension. These are binary data that
is created with the command save in the command prompt. In the next session,
the data can be loaded with the command load. The advantage is that this is a
special format that can be read only with Matlab.
Mex-File
– These are programs that can call other programming files that are written in
programming languages like C or Fortran.
Matlab
can be launched by double-clicking the Matlab icon or by navigating the matlab
program.
Some
simple commands like this can be tested
>> 2 + 4
Matlab
promptly returns the result. Instead of typing the Matlab commands one by one,
a set of valid Matlab commands can be put into a file with .m extension. This
is called script file. The script file can be executed in the command prompt by
typing the file name. Caution should be taken that the scripts that are created
by us should have a name that is different from the key words of matlab.
Function
files are similar to script files. The main difference is all the variables
defined in a function file are local. One can compare the function file with
the function or subroutine of a programming language.
The
structure of a function file is given
as
function
[outvariables] = functionname (Inputparameters)
% First
comment – This appears when the on-line help is sought called h1 line
% This
symbol is an indication of comments that are used for documentation
Statement(s)
As
the structure is given above, functions are created with the keyword function followed by the output variables. The
definition of a function includes function name also with the necessary input
arguments. The first line is called h1 line which is used to provide the
comments that are useful to understand the reason for creating that function.
This can be followed by many comments. Comments are useful to document the
problem so that the third person can understand it. The core part of the
function is a set of commands that are necessary to execute it.
Once
a function is defined, the function can be executed by invoking it either by
calling the function with or without the output variables. If the objective of
the function is to display a graph, then storing the resultant may not be
necessary. In that case, the output variables can be ignored.
The functions can be called within another
function also as
t1
= @samplefunction
Here a handle t1 is created for a sample
function. This can be used inside for another function as
t2
= samplefunction2(t1,inputvariables)
or
t2 = samplefunction2(@samplefunction,inputvariables)
This
command is very useful in image processing for block processing.
To
increase the speed of execution, the functions can be compiled readily. So the
function is interpreted and translated to an intermediate form and stored for
later uses. This approach increased the fastness. Also parsing creates a file
that is in protected form. This is immensely useful to protect the identity of
the programming code so that no one else can see the code and alter it.
Matlab
Programming language
Like any
programming language like C or C++, Matlab uses a set of programming
constructs. This covers all the programming aspects like sequence, branching
and repetition. Like other programming languages the global variables can be
created with the command global.
For example, the
statement
global m,n
creates two
global variables.
Matlab
provides branching using if-elseif-else statements as
if
condition1
statement(s)
else if
condition2
statement(s)
else
statement(s)
end
Like C
language, the conditions are executed and a logical branching is done. Based on
the truth of the conditions the statements are executed. Else- Part is executed
when both the conditions are false. When conditions become more, writing a
recursive if statements with more conditions makes program difficult to
understand. In that case the ‘switch’ statement is very helpful. The syntax for
switch is given as
switch
indicator
case value1
Statement(s)
case value2
Statement(s)
otherwise
Defaultstatement(s)
end
Similarly
matlab provides two repetitive statements for and while. The syntax for the
statement for is given as
for
counter:=start:Increment:End
statement(s)
End
The increment
can be positive or negative also. Initially the counter value is the start.
then based on the increment , the counter value is incremented. Once it reaches
the end value, the program control comes out of the loop. The statements are
thus executed as per the start, end and increment value.
Similarly the
statement ‘while’ can be used to execute a set of statements till the condition
specified by the while is no longer satisfied. The syntax for ‘while’ statement
is given as
while Condition
Statement(s)
end
Other
statements that are useful are
1. Break – This statement is useful
break out of a loop in a loop. This is applicable even if the condition of the
execution is true.
2. Return – This statement simply
returns the program control to the function that invokes it.
3. error – When there is a problem
inside a function or script, this statement is useful for returning the control
to the keyboard.
4. pause – This statement is useful
to temporarily pause or halt the current process and waits for the user
response. pause(n) cause the ‘pause’ command
to last n seconds and resume after that.
Input
Commands
The syntax of
the input command is
output_variable =
input(‘String’)
For example,
the command n = input(‘Enter the value
of n’) display the string in the command prompt expecting the user response.
The user response is collected and stored in the variable ‘n’.
When the
choices are more, the command ‘menu’ can be used. The syntax of te command
‘menu’ is given as follows.
menu(‘Menu name’,’choice 1’, ‘choice 2’,….’choice n’)
This command creates
an onscreen menu. Based on the response given by the user either by mouse or
keyboard, the respective commands can be used. Matlab provides advanced
versions of GUI. One can use ‘menu’ command for some primitive level usage.
When the data
becomes more, the files can be used. One can compare this with C programming
language and some of its commands are shown below
1. fpen – Open a file or create a new file.
2. fclose – Close the file
3. fread – read the value from the file
4. fwrite – write the value into the file
5. fscanf – read the formatted data
6. fwritef – write the formatted data
Some of the
advanced matlab usages are structures and cells that can be referred in Matlab
manuals.
************
No comments:
Post a Comment