Thursday, 25 July 2013

Basics of Matlab & its 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 [out_variables] = function_name (Input_parameters)
% 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 = @sample_function
Here a handle t1 is created for a sample function. This can be used inside for another function as
            t2 = sample_function2(t1,input_variables)
                                    or
            t2 = samplefunction2(@sample_function,input_variables)
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
                                           Default statement(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 the 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  



2 comments:

Unknown said...

Anyone here interested in Wavelets?

Follow my dissertation about PCBs Fast Wavelet Transform.

Please, download and read it!

Link:

http://repositorio.unicamp.br/jspui/bitstream/REPOSIP/338849/1/Bonello_DanielKatz_M.pdf

vstcyberpc said...

I am very impressed with your post because this post is very beneficial for me and provide a new knowledge to me
https://vstcyberpc.com/v-collection-7-vst-crack/
https://vstcyberpc.com/scheps-omni-channel-crack/
https://vstcyberpc.com/output-thermal-vst-crack/
https://vstcyberpc.com/heat-up-3-vst-crack/

Post a Comment