final matlab report

25
Department of Electronics & communication Page 1 Table of Contents: S. no Topic Page No. 1. About the organization 02 2. Introduction to MATLAB 02-03 3. Basic Mathematical functions in MATLAB. 04-07 4. MATLAB Toolboxes 08 5. Image processing toolbox 08-15 6. Graphical User Interface (GUI) 15-21 7. Adder using GUI 21-25 8. Conclusion 25 9. References 25

Upload: aparna-jain

Post on 28-Apr-2015

44 views

Category:

Documents


5 download

DESCRIPTION

matlab basics

TRANSCRIPT

Page 1: Final Matlab Report

Department of Electronics & communication Page 1

Table of Contents:

S. no Topic Page No.

1. About the organization 02

2. Introduction to MATLAB 02-03

3. Basic Mathematical functions in MATLAB. 04-07

4. MATLAB Toolboxes 08

5. Image processing toolbox 08-15

6. Graphical User Interface (GUI) 15-21

7. Adder using GUI 21-25

8. Conclusion 25

9. References 25

Page 2: Final Matlab Report

Department of Electronics & communication Page 2

1. About the organization: New Horizons Computer Learning Centers is the world's largest independent IT training company with over 300 locations in 70 countries. It is a subsidiary of New Horizons Worldwide, Inc. New Horizons operates as a franchise model to support the worldwide expansion of their training centers.

New Horizons computer learning centers provides instruction in the use of personal computers, computer software and business skills. New Horizons offers vendor-authorized training and certifications for Microsoft, Cisco, CompTIA and VMware. Training methods include instructor-led courses, both in person and online, as well as self-directed labs. Students are provided with courseware developed in collaboration with Axzo Press.

It was Founded in 1982 and headquartered in Anaheim, California, USA, New Horizons provide a broad range of computer training—including technical and desktop applications—to individual learners and companies, as well as complementary programs in Business Skills, Healthcare Information Management, and English as a second language.

2. Introduction to MATLAB: The name MATLAB stands for MATrix LABoratory. MATLAB was written originally to provide easy access to matrix software developed by the LINPACK (linear system package) and EISPACK (Eigen system package) projects.

MATLAB is a high-performance language for technical computing. It integrates computation, visualization, and programming environment. Furthermore, MATLAB is a modern programming language environment: it has sophisticated data structures, contains built-in editing and debugging tools, and supports object-oriented programming. These factors make MATLAB an excellent tool for teaching and research.

Important features of MATLAB

1. MATLAB, which stands for MATrix LABoratory, is a product of The Mathworks, Inc. 2. MATLAB is a platform independent (meaning it should work equally well on a PC, a MAC, or a mainframe system), interactive environment for computation, visualization, and animation.

Page 3: Final Matlab Report

Department of Electronics & communication Page 3

3. MATLAB has a “natural language” interface that is easy to use. It also has built-in functions and toolboxes that help facilitate problem solution and graphical visualization of problems. 4. MATLAB is especially powerful in the solution of linear algebra (matrix) problems. 5. It is Case Sensitive.

Page 4: Final Matlab Report

Department of Electronics & communication Page 4

3. Basic Mathematical Functions in MATLAB: MATLAB offers many predefined mathematical functions for technical computing which contains a large set of mathematical functions.

There is a long list of mathematical functions that are built into MATLAB. These functions are called built-ins. Many standard mathematical functions, such as sin(x), cos(x), tan(x), ex, ln(x), are evaluated by the functions sin, cos, tan, exp, and log respectively in MATLAB.

Table 2.1 lists some commonly used functions, where variables x and y can be numbers, vectors, or matrices.

Desktop Tools and Development Environment Command Work exit Terminates the current MATLAB session. quit Terminates the current MATLAB session clc Clear Command Window commandhistory Open Command History window, or select it if already open. diary Save session to file. clear Removes all variables from the workspace. This frees up

system memory. help Help lists all primary help topics in the Command Window

Page 5: Final Matlab Report

Department of Electronics & communication Page 5

exist Check existence of variable, function, directory, or Java class dir Directory listing. what List MATLAB files in current directory. Operations in MATLAB:

+ Addition

+ Unary plus - Subtraction - Unary minus * Matrix multiplication

^ Matrix power \ Backslash or left matrix divides / Slash or right matrix divide ' Transpose .' Nonconjugated transpose .* Array multiplication (element-wise) .^ Array power (element-wise) .\ Left array divide (element-wise) ./ Right array divide (element-wise)

Data Analysis commands in MATLAB

Ø mean:- mean (A) returns the mean values of the elements along different dimensions of an array.

Ø median:- returns the median values of the elements along different dimensions of an array.

Ø conv:- conv(u,v) convolves vectors u and v. Algebraically, convolution is the same operation as multiplying the polynomials whose coefficients are the elements of u and v.

Ø convn:- convn(A,B) computes the N-dimensional convolution of the arrays A and B. The size of the result is size(A)+size(B)-1.

Page 6: Final Matlab Report

Department of Electronics & communication Page 6

Ø deconv:- deconv(v,u) deconvolves vector u out of vector v, using long division. The quotient is returned in vector q and the remainder in vector r such that v = conv(u,q)+r .

Ø poly:- poly(r) where r is a vector returns a row vector whose elements are the coefficients of the polynomial whose roots are the elements of r.

Ø polyval:- polyval(p,x) returns the value of a polynomial of degree n evaluated at x.

Ø polyvalm:- polyvalm(p,X) evaluates a polynomial in a matrix sense. This is the same as substituting matrix X in the polynomial p.

Ø roots:- returns a column vector whose elements are the roots of the polynomial c.

Ø fft:-Discrete Fourier transform. Ø fft2:-2-D discrete Fourier transform. Ø fftn:-N-D discrete Fourier transform. Ø ifft:-Inverse discrete Fourier transform. Ø ifft2:-2-D inverse discrete Fourier transform. Ø ifftn:-N-D inverse discrete Fourier transform.

Graphics commands:

Ø plot:- plot(Y) plots the columns of Y versus their index if Y is a real number.

Page 7: Final Matlab Report

Department of Electronics & communication Page 7

Ø plot3:- The plot3 function displays a three-dimensional plot of a set of data points.

Ø subplot:- divides the current figure into rectangular panes that are numbered row wise. Each pane contains an axes object. Subsequent plots are output to the current pane.

Ø stem: - Plot discrete sequence data.

3-D Visualization Commands:- Ø surf:- surf(Z) creates a three-dimensional shaded surface from the z

components in matrix Z.

Ø mesh:- mesh, meshc, and meshz create wireframe parametric surfaces specified by X, Y, and Z, with color specified by C.

Ø colormap:- A colormap is an m-by-3 matrix of real numbers between 0.0 and 1.0. Each row is an RGB vector that defines one color.

Ø zoom:-Turn zooming on or off or magnify by factor. Ø rotate3d:- Rotate 3-D view using mouse. Ø xlabel, ylabel, zlabel:-Label x-, y-, and z-axis. Ø contour:-Contour plot of matrix. Ø contour3:- 3-D contour plot. Ø hist:-Histogram plot. Ø sphere:-Generate sphere.

Page 8: Final Matlab Report

Department of Electronics & communication Page 8

4. MATLAB Toolboxes: Toolboxes are collections of functions etc. for tackling particular classes of problems. They have to be purchased separately to use with MATLAB to increase its capabilities. MATLAB contains the following Toolboxes: Ø SIMULINK Toolbox Ø Image Processing Toolbox Ø Control Systems Toolbox Ø Signal Processing Toolbox Ø Fuzzy Logic Toolbox Ø Neural Networks Toolbox Ø Optimisation Toolbox Ø Statistics Toolbox Ø Symbolic maths Toolbox Ø Mapping Toolbox Ø Wavelet and many more.

Here we will discuss few of the toolboxes that we have used during training.

5. Image processing toolbox:

Image: Ø An image may defined as a 2-dimensional function, f (x,y), where x & y are

spatial (plane) coordinates, & the amplitude of f at any pair of coordinates (x,y) is called the intensity or gray level of the image at that point. When x,y & the amplitude values of f are all finite, discrete quantities, we call the image as Digital image.

Ø A digital image differs from a photo in that the values are all discrete.

Ø A digital image can be considered as a large array of discrete dots, each of

which has a brightness associated with it. These dots are called picture elements, or more simply pixels.

Page 9: Final Matlab Report

Department of Electronics & communication Page 9

Image processing

Ø An image processing refers to processing images by means of various algorithms. Ø A digital image processing refers to the processing of digital images by means of

digital computer. Ø MATLAB has several image formats:

– BMP (Microsoft Windows Bitmap)

– GIF (Graphics Interchange Files)

– HDF (Hierarchical Data Format)

– JPEG (Joint Photographic Experts Group)

– PCX (Paintbrush)

– PNG (Portable Network Graphics)

– TIFF (Tagged Image File Format)

– XWD (X Window Dump)

– raw-data and other types of image data

Ø Data types in MATLAB

– Double (64-bit double-precision floating point)

– Single (32-bit single-precision floating point)

– Int32 (32-bit signed integer)

– Int16 (16-bit signed integer)

– Int8 (8-bit signed integer)

– Uint32 (32-bit unsigned integer)

– Uint16 (16-bit unsigned integer)

– Uint8 (8-bit unsigned integer)

Page 10: Final Matlab Report

Department of Electronics & communication Page 10

Image co-ordinate systems:

Ø Pixel coordinates: In this coordinate system, the image is treated as a grid of discrete elements, ordered from top to bottom and left to right.

Ø Spatial Coordinates: In this spatial coordinate system, locations in an image are positions on a plane, and they are described in terms of x and y (not r and c as in the pixel coordinate system).

Types of Digital Images

Ø Binary: Each pixel is just black or white. Since there are only two possible values for each pixel (0,1), we only need one bit per pixel.

Page 11: Final Matlab Report

Department of Electronics & communication Page 11

Ø Grayscale: Each pixel is a shade of gray, normally from 0 (black) to 255 (white). This range means that each pixel can be represented by eight bits, or exactly one byte. Other grey scale ranges are used,but generally they are a power of 2.

Ø True Colour, or RGB: Each pixel has a particular color; that color is described

by the amount of red, green and blue in it. If each of these components has a range 0–255, this gives a total of 2563 different possible colors. Such an image is a “stack” of three matrices; representing the red, green and blue values for each pixel. This means that for every pixel there correspond 3 values.

Page 12: Final Matlab Report

Department of Electronics & communication Page 12

Image conversion:

Basic functions in image processing toolbox: The various basic functions used in the processing of an image are listed below according to their category:-

Ø Image Display and Exploration

1. colorbar:-Display color bar. 2. immovie:-Make movie from multiframe image.

3. implay:-Play movies, videos, or image sequences.

4. imshow:-Display image.

5. imtool:-Image Tool.

6. subimage:-Display multiple images in single figure.

Ø Image Types and Type Conversions

1. double:-Convert data to double precision. 2. gray2ind:-Convert grayscale or binary image to indexed image.

Page 13: Final Matlab Report

Department of Electronics & communication Page 13

3. im2bw:-Convert image to binary image, based on threshold. 4. im2double:-Convert image to double precision. 5. im2int16:-Convert image to 16-bit signed integers. 6. im2single:-Convert image to single precision. 7. im2uint16:-Convert image to 16-bit unsigned integers. 8. im2uint8:-Convert image to 8-bit unsigned integers. 9. ind2gray:-Convert indexed image to grayscale image. 10. ind2rgb:-Convert indexed image to RGB image. 11. mat2gray:-Convert matrix to grayscale image. 12. rgb2gray:-Convert RGB image or colormap to grayscale. 13. rgb2ind:-Convert RGB image to indexed image. 14. uint16:-Convert data to unsigned 16-bit integers. 15. uint8:-Convert data to unsigned 8-bit integers.

Ø Modular Interactive Tools:

1. imageinfo:-Image Information tool.

2. imcontrast:-Adjust Contrast tool.

3. imdisplayrange:-Display Range tool.

4. imdistline:-Distance tool.

Page 14: Final Matlab Report

Department of Electronics & communication Page 14

5. impixelinfo:-Pixel Information tool.

6. impixelregion:-Pixel Region tool.

7. impixelregionpanel:-Pixel Region tool panel.

Ø Image Arithmetic

1. imabsdiff:-Absolute difference of two images.

2. imadd:-Add two images or add constant to image.

3. imcomplement:-Complement image.

4. imdivide:-Divide one image into another or divide image by constant.

5. imlincomb:-Linear combination of images.

6. immultiply:-Multiply two images or multiply image by constant.

7. imsubtract:-Subtract one image from another or subtract constant from image.

Ø Intensity and Binary Images

1. imbothat:-Bottom-hat filtering. 2. imclearborder:-Suppress light structures connected to image border.

3. imclose:-Morphologically close image.

4. imdilate:-Dilate image.

5. imerode:-Erode image.

Page 15: Final Matlab Report

Department of Electronics & communication Page 15

6. imfill:-Fill image regions and holes.

7. imopen:-Morphologically open image.

8. imtophat:-Top-hat filtering.

9. strel:-Create morphological structuring element (STREL).

Ø Image Enhancement

1. histeq:-Enhance contrast using histogram equalization.

2. imadjust:-Adjust image intensity values or colormap.

Ø Pixel Values and Statistics

1. imcontour:-Create contour plot of image data. 2. imhist:-Display histogram of image data. 3. impixel:-Pixel color values.

6. GRAPHICAL USER INTERFACE (GUI) : A graphical user interface (GUI) is a graphical display that contains devices, or components, that enable a user to perform interactive tasks. To perform these tasks, the user of the GUI does not have to create a script or type commands at the command line. Often, the user does not have to know the details of the task at hand.

The GUI components can be menus, toolbars, push buttons, radio buttons, list boxes, and sliders—just to name a few. In MATLAB, a GUI can also display data in tabular form or as plots, and can group related components.

Page 16: Final Matlab Report

Department of Electronics & communication Page 16

Working of GUI

Each component, and the GUI itself, is associated with one or more user-written routines known as callbacks. The execution of each callback is triggered by a particular user action such as a button push, mouse click, selection of a menu item, or the cursor passing over a component. The creator of the GUI provides these callbacks.

In the GUI, the user selects a data set from the pop-up menu, then clicks one of the plot type buttons. Clicking the button triggers the execution of a callback that plots the selected data in the axes. This kind of programming is often referred to as event-driven programming.

The event in the example is a button click. In event-driven programming, callback execution is asynchronous, controlled by events external to the software.

In the case of MATLAB GUIs, these events usually take the form of user interactions with the GUI.

Techniques used to implement the GUI

There are following two techniques to implement the GUIs in the MATLAB:-

1. Creating a Simple GUI with GUIDE. 2. Creating a Simple GUI Programmatically.

GUIDE: A Brief Introduction: GUIDE, the MATLAB graphical user interface development environment, provides a set of tools for creating graphical user interfaces (GUIs). These tools simplify the process of laying out and programming GUIs. The GUIDE Layout Editor enables you to populate a GUI by clicking and dragging GUI components — such as buttons, text fields, sliders, axes, and so on — into the layout area. It also enables you to create menus and context menus for the GUI.

Page 17: Final Matlab Report

Department of Electronics & communication Page 17

GUIDE Tools Summary

Page 18: Final Matlab Report

Department of Electronics & communication Page 18

Programming a GUI

When you save your GUI layout, GUIDE automatically generates an M-file that you can use to control how the GUI works. This M-file provides code to initialize the GUI and contains a framework for the GUI callbacks—the routines that execute in response to user-generated events such as a mouse click. Using the M-file editor, you can add code to the callbacks to perform the functions you want. Programming a Simple GUI shows you what code to add to the example M-file to make the GUI work.

Starting a GUIDE: There are many ways to start GUIDE. We can start GUIDE from the:

1. Command line by typing guide 2. Start menu by selecting MATLAB > GUIDE (GUI Builder) 3. MATLAB File menu by selecting New > GUI 4. MATLAB toolbar by clicking the GUIDE button

However we start GUIDE, it displays the GUIDE Quick Start dialog box shown in the following figure.

Page 19: Final Matlab Report

Department of Electronics & communication Page 19

Blank GUI

The blank GUI template displayed in the Layout Editor is shown in the following figure.

Alignment Tool

Page 20: Final Matlab Report

Department of Electronics & communication Page 20

Creating GUIs Programmatically

1. Callback Execution:- Callback execution is event driven and callbacks from different GUIs share the same event queue. In general, callbacks are triggered by user events such as a mouse click or key press. Because of this, you cannot predict, when a callback is requested, whether or not another callback is executing or, if one is, which callback it is.

Various functions used in creating GUIs programmatically are given below:-

Ø Predefined Dialog Boxes

1. errordlg:- Create and open error dialog box.

2. uigetfile:-Open standard dialog box for retrieving files.

3. uiopen:-Open file selection dialog box with appropriate file filters.

4. uisave:-Open standard dialog box for saving workspace variables.

5. msgbox:-Create and open message box.

6. warndlg:-Open warning dialog box.

7. questdlg:-Create and open question dialog box.

8. uiputfile:-Open standard dialog box for saving files.

Page 21: Final Matlab Report

Department of Electronics & communication Page 21

Ø Deploying User Interfaces

1. guidata:-Store or retrieve GUI data.

2. guihandles:-Create structure of handles.

3. movegui:-Move GUI figure to specified location on screen.

4. openfig:-Open new copy or raise existing copy of saved figure.

Ø User Interface Objects

1. menu:-Generate menu of choices for user input.

2. uibuttongroup:-Create container object to exclusively manage radio buttons and toggle buttons.

3. uicontextmenu:-Create context menu.

4. uicontrol:-Create user interface control object.

5. uimenu:-Create menus on figure windows.

6. uipanel:-Create panel container object.

7. uipushtool:-Create push button on toolbar.

8. uitoggletool:-Create toggle button on toolbar.

9. uitoolbar:-Create toolbar on figure.

Page 22: Final Matlab Report

Department of Electronics & communication Page 22

7. Adder using GUI : We have made a simple adder using GUI which can be deployed and can be used for adding purposes. By using the same GUI we can adder other functions also like subtract, divide, multiply etc. but we are only making adder here for learning purpose.

Fig. Final GUI

Steps for making the Adder :

1. Type guide in command prompt:

2. This window will appear:

Page 23: Final Matlab Report

Department of Electronics & communication Page 23

3. Select Blank GUI and press ok.

4. Take 3 edit boxes,1 static box and put it as shown in fig.

5. Go on property inspector and change the name.

Page 24: Final Matlab Report

Department of Electronics & communication Page 24

6. Similarly do it for edit box and push button.

7. For programming part right click the button and press view callback then write the code in it.

Code:

function input1_editText_Callback(hObject, eventdata, handles) % hObject handle to input1_editText (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hints: get(hObject,'String') returns contents of input1_editText as text % str2double(get(hObject,'String')) returns contents of input1_editText as a double %store the contents of input1_editText as a string. if the string %is not a number then input will be empty input = str2num(get(hObject,'String')); %checks to see if input is empty. if so, default input1_editText to zero if (isempty(input)) set(hObject,'String','0') end guidata(hObject, handles);

% --- Executes on button press in add_pushbutton. function add_pushbutton_Callback(hObject, eventdata, handles) % hObject handle to add_pushbutton (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)

a = get(handles.input1_editText,'String'); b = get(handles.input2_editText,'String'); % a and b are variables of Strings type, and need to be converted % to variables of Number type before they can be added together total = str2num(a) + str2num(b); c = num2str(total); % need to convert the answer back into String type to display it set(handles.answer_staticText,'String',c); guidata(hObject, handles);

8. Now click the green arrow or click run from the options or either write name i.e myadder in command window.

Page 25: Final Matlab Report

Department of Electronics & communication Page 25

9. The below screen will appear, put the values and get the answer. Here we have put 4 and 20 ad we get 24 as result.

8. Conclusion: During the training period we have studied that matlab can be used for variety of applications like computation and mathematical operations, Algorithm development, Data acquisition, Application development, including graphical user interface building, building graphs and plots. process digital images and many more.

9. References:

MATLAB primer R2012a. MATLAB GUI by reza sepas yar. http://www.imc.tue.nl/IMC-main/IMC-main-node8.html#Ch1.6 http://www.mathworks.in/products/ http://en.wikipedia.org/wiki/MATLAB