basics of matlab 2- programming in matlab
DESCRIPTION
Basics of MATLAB 2- Programming in MATLAB. By DR. Wafaa Shabana Email: [email protected]. Table of Contents:. 1. Introduction 2. MATLAB m-files. 3. Flow control in MATLAB. 4 . Image Processing using MATLAB 5 . Questions? 6 . References. 1. Introduction:. Recall that: - PowerPoint PPT PresentationTRANSCRIPT
![Page 2: Basics of MATLAB 2- Programming in MATLAB](https://reader035.vdocuments.us/reader035/viewer/2022081417/56814fbf550346895dbd79e6/html5/thumbnails/2.jpg)
Table of Contents:
1. Introduction2. MATLAB m-files.3. Flow control in MATLAB.4. Image Processing using MATLAB5. Questions?6. References
![Page 3: Basics of MATLAB 2- Programming in MATLAB](https://reader035.vdocuments.us/reader035/viewer/2022081417/56814fbf550346895dbd79e6/html5/thumbnails/3.jpg)
1. Introduction:
• Recall that:• Everything in MATLAB is a matrix !• The MATLAB environment is command oriented somewhat
like UNIX. • If a statement is terminated with a semicolon ( ; ), no results
will be displayed. Otherwise results will appear before the next prompt.• ans Default variable name for results• % start comment which ends at end of line
![Page 4: Basics of MATLAB 2- Programming in MATLAB](https://reader035.vdocuments.us/reader035/viewer/2022081417/56814fbf550346895dbd79e6/html5/thumbnails/4.jpg)
The colon operator ( : ) to specify range •MATLAB supports six relational operators.
Less Than <Less Than or Equal <=Greater Than >Greater Than or Equal >=Equal To ==Not Equal To ~=
![Page 5: Basics of MATLAB 2- Programming in MATLAB](https://reader035.vdocuments.us/reader035/viewer/2022081417/56814fbf550346895dbd79e6/html5/thumbnails/5.jpg)
• MATLAB supports three logical operators.
not ~ % highest precedenceand & % equal precedence with oror | % equal precedence with and
![Page 6: Basics of MATLAB 2- Programming in MATLAB](https://reader035.vdocuments.us/reader035/viewer/2022081417/56814fbf550346895dbd79e6/html5/thumbnails/6.jpg)
• MATLAB is case-sensitive Programming language.• Variable names can contain up to 63 characters (as of
MATLAB 6.5 and newer)• Variable names must start with a letter followed by letters,
digits, and underscores• Examples: my_name , day2 % valid variable names Invalid variable name: my-name %minus not allowed 2day %must start with letter %x % not allowed character @room %not allowed character
![Page 7: Basics of MATLAB 2- Programming in MATLAB](https://reader035.vdocuments.us/reader035/viewer/2022081417/56814fbf550346895dbd79e6/html5/thumbnails/7.jpg)
Some of useful MATLAB commands•who List known variables•whos List known variables plus their size•help >> help sqrt Help on using sqrt•lookfor >> lookfor sqrt Search for
keyword sqrt in m-files•what >> what a: List MATLAB files in a:•clear Clear all variables from work space•clear x y Clear variables x and y from work space•clc Clear the command window (clear the screen)•save saves the workspace load loads a saved workspace or data file
![Page 8: Basics of MATLAB 2- Programming in MATLAB](https://reader035.vdocuments.us/reader035/viewer/2022081417/56814fbf550346895dbd79e6/html5/thumbnails/8.jpg)
2. MATLAB m-files:
•There is no doubt that you can do a lot of work in the command window. However, when the amount of instructions needed to complete a task increases or you need to re-execute a block of instructions several times, the command window is a poor choice. The best choice is to write a MATLAB program.
![Page 9: Basics of MATLAB 2- Programming in MATLAB](https://reader035.vdocuments.us/reader035/viewer/2022081417/56814fbf550346895dbd79e6/html5/thumbnails/9.jpg)
•MATLAB programs are stored as text files. •The files can be loaded into the MATLAB command environment where they are then interpreted and executed.
MATLAB supports two types of programs: 1- script files and2- function files.
![Page 10: Basics of MATLAB 2- Programming in MATLAB](https://reader035.vdocuments.us/reader035/viewer/2022081417/56814fbf550346895dbd79e6/html5/thumbnails/10.jpg)
Script files consist of sequences of instructions stored in text files. The files are commonly referred to as m-files because of the .m extension used.•An m-file can be created by any text editor or word processor. •If a word processor is used, make sure to save the file as a text file.• MATLAB has its own built-in text editor. •To launch it, type the command edit in the command window
![Page 11: Basics of MATLAB 2- Programming in MATLAB](https://reader035.vdocuments.us/reader035/viewer/2022081417/56814fbf550346895dbd79e6/html5/thumbnails/11.jpg)
Example (1):%this is our first m.file%saved as first.m%plot a graph of a cos functioncleart = 0: 0.001:10;y = cos (2*pi*t);plot(t,y)title(‘My first graph’)xlabel(‘time, sec’)ylabel(‘values of y’)
![Page 12: Basics of MATLAB 2- Programming in MATLAB](https://reader035.vdocuments.us/reader035/viewer/2022081417/56814fbf550346895dbd79e6/html5/thumbnails/12.jpg)
Remark:1- Before trying to run your program, make sure that its file is on the MATLAB path. 2- Use the which function to see if your program is on the path:
which first3-You have the choice of operating the debugger from the Editor window that displays your program, from the MATLAB command line, or both.
![Page 13: Basics of MATLAB 2- Programming in MATLAB](https://reader035.vdocuments.us/reader035/viewer/2022081417/56814fbf550346895dbd79e6/html5/thumbnails/13.jpg)
Function files:• The main difference between a script and a function is that a function
accepts input from and returns output to its caller, whereas scripts do not.
• You define MATLAB functions in a file that begins with a line containing the function key word.
• Question:Can we define a function within a script file or at the command window?
![Page 14: Basics of MATLAB 2- Programming in MATLAB](https://reader035.vdocuments.us/reader035/viewer/2022081417/56814fbf550346895dbd79e6/html5/thumbnails/14.jpg)
* Functions always begin with a function definition line for example function y = average(x)
• The function ends either with the first matching end statement, the occurrence of another function definition line, or the end of the file, whichever comes first.
![Page 15: Basics of MATLAB 2- Programming in MATLAB](https://reader035.vdocuments.us/reader035/viewer/2022081417/56814fbf550346895dbd79e6/html5/thumbnails/15.jpg)
Example:
• function y = area(x1,x2)• %this function computes the area of a rectangle• %input: length x1 and width x2• %output: area y• x1 =input('Enter the width:');• x2 =input('Enter the length:');• y = x1 * x2;• disp(‘The area is %d’, y)
![Page 16: Basics of MATLAB 2- Programming in MATLAB](https://reader035.vdocuments.us/reader035/viewer/2022081417/56814fbf550346895dbd79e6/html5/thumbnails/16.jpg)
inputRequest user input
SyntaxevalResponse = input(prompt)strResponse = input(prompt, 's')
dispDisplay text or array
Syntaxdisp(X)
![Page 17: Basics of MATLAB 2- Programming in MATLAB](https://reader035.vdocuments.us/reader035/viewer/2022081417/56814fbf550346895dbd79e6/html5/thumbnails/17.jpg)
• If the function is supposed to return more than one value, the syntax is:
function [output_1,output_2,…]= function_name(input_1, input_2,…)
Question:What is the difference between MATLAB functions and C-
functions?Note that:• The use of the editor is pretty straight-forward. The
advantage of using MATLAB’s editor is that color coding and formatting is built in.
![Page 18: Basics of MATLAB 2- Programming in MATLAB](https://reader035.vdocuments.us/reader035/viewer/2022081417/56814fbf550346895dbd79e6/html5/thumbnails/18.jpg)
3. Control flow in MATLAB:
Decision in MATLAB:1- if statement:if (logical statement) block of code to execute if logical statement is trueend2- if …………else statement:if (logical statement) block of code to execute if logical statement is trueelse block of code to execute if logical statement is falseend
![Page 19: Basics of MATLAB 2- Programming in MATLAB](https://reader035.vdocuments.us/reader035/viewer/2022081417/56814fbf550346895dbd79e6/html5/thumbnails/19.jpg)
3- Nested if ------else statement:if (logical statement 1)
block of code to execute if logical statement 1 is trueelseif (logical statement 2)
block of code to execute if logical statement 2 is trueelseif (logical statement n)
block of code to execute if logical statement n is trueelse
block of code to execute if all logical statements are false
end
![Page 20: Basics of MATLAB 2- Programming in MATLAB](https://reader035.vdocuments.us/reader035/viewer/2022081417/56814fbf550346895dbd79e6/html5/thumbnails/20.jpg)
4- switch statement:switch evaluative expressioncase value_1
block of code to execute if the evaluative expression is value_1case value_2
block of code to execute if the evaluative expression is value_2case value_n
block of code to execute if the evaluative expression is value_notherwise
block of code to execute if the evaluative expression is not any of the valuesend
![Page 21: Basics of MATLAB 2- Programming in MATLAB](https://reader035.vdocuments.us/reader035/viewer/2022081417/56814fbf550346895dbd79e6/html5/thumbnails/21.jpg)
2- Iteration in MATLAB:
1- for statement:for i = 1:10 % execute these commandsend
2- while statement:while x <= 10
% execute these commandsend
![Page 22: Basics of MATLAB 2- Programming in MATLAB](https://reader035.vdocuments.us/reader035/viewer/2022081417/56814fbf550346895dbd79e6/html5/thumbnails/22.jpg)
break statement:Terminate execution of for or while loop
Syntaxbreak
return statement:Return to invoking function
Syntaxreturn
continue statement:Pass control to next iteration of for or while loop
Syntaxcontinue
![Page 23: Basics of MATLAB 2- Programming in MATLAB](https://reader035.vdocuments.us/reader035/viewer/2022081417/56814fbf550346895dbd79e6/html5/thumbnails/23.jpg)
4. Image Processing in MATLAB:Image processing toolbox is one of the most useful toolboxes in MATLAB.This toolbox supports a wide range of image processing operations.There are five types of images in MATLAB: 1- Binary image : {0, 1}{black white}
2- Gray-scale image: [0,255] {shades of gray}3- True color image: m x n x 34- Intensity image: [0, 1] or uint85- Indexed images: m x 3 color map
![Page 24: Basics of MATLAB 2- Programming in MATLAB](https://reader035.vdocuments.us/reader035/viewer/2022081417/56814fbf550346895dbd79e6/html5/thumbnails/24.jpg)
1- Reading an image: I = imread(‘image name’)It reads an image from an image file and store it In a matrix I
2- displaying an image:imshow(‘image_name’)
3- image size
size(‘image_name’)
![Page 25: Basics of MATLAB 2- Programming in MATLAB](https://reader035.vdocuments.us/reader035/viewer/2022081417/56814fbf550346895dbd79e6/html5/thumbnails/25.jpg)
Check the Image in Memory
< Name, Size, Bytes, Class >whos
Name Size Bytes Class ans 291x240 69840 uint8 arrayGrand total is 69840 elements using 69840 bytes
•uint8 [0, 255]•uint16 [0, 65535]•double [0, 1]
![Page 26: Basics of MATLAB 2- Programming in MATLAB](https://reader035.vdocuments.us/reader035/viewer/2022081417/56814fbf550346895dbd79e6/html5/thumbnails/26.jpg)
Resizing Images
• B = imresize(A, scale)• B = imresize(A, [mrows ncols])
![Page 27: Basics of MATLAB 2- Programming in MATLAB](https://reader035.vdocuments.us/reader035/viewer/2022081417/56814fbf550346895dbd79e6/html5/thumbnails/27.jpg)
For example:
clearclose ally= imread('pic2.jpg');I1 = imresize(y, 0.5);I2 = imresize(y,[256 256]);figureimshow(y)figureimshow(I1)figureimshow(I2)
![Page 28: Basics of MATLAB 2- Programming in MATLAB](https://reader035.vdocuments.us/reader035/viewer/2022081417/56814fbf550346895dbd79e6/html5/thumbnails/28.jpg)
im2bwConvert image to binary image, based on threshold
SyntaxBW = im2bw(I, level)BW = im2bw(X, map, level)BW = im2bw(RGB, level)
rgb2grayConvert RGB image or colormap to grayscale
Some useful commands:
![Page 29: Basics of MATLAB 2- Programming in MATLAB](https://reader035.vdocuments.us/reader035/viewer/2022081417/56814fbf550346895dbd79e6/html5/thumbnails/29.jpg)
imageDisplay image object
imwriteWrite image to graphics file
Syntaximwrite(A,filename,fmt)
e.g. imwrite(I, ‘wafaa’, ‘gif’)
![Page 30: Basics of MATLAB 2- Programming in MATLAB](https://reader035.vdocuments.us/reader035/viewer/2022081417/56814fbf550346895dbd79e6/html5/thumbnails/30.jpg)
5. Questions?(Mini project in MATLAB):
1- Encoding step:Write a m-function that hides some information (e.g. : your name) into a given image.(watermarked image)
2- Decoding Step:Write an m-function that receives the watermarked image and extract the water mark from it.
![Page 31: Basics of MATLAB 2- Programming in MATLAB](https://reader035.vdocuments.us/reader035/viewer/2022081417/56814fbf550346895dbd79e6/html5/thumbnails/31.jpg)
6. References:You can read the following references:
•1- “The Basics of MATLAB” Jeffrey O. Bauer•2- www.mathworks.com•3. “MATLAB tutorial for beginners” Jyotirmay Gadewadikar http://arri.uta.edu/acs/
Need further help? Contact me
![Page 32: Basics of MATLAB 2- Programming in MATLAB](https://reader035.vdocuments.us/reader035/viewer/2022081417/56814fbf550346895dbd79e6/html5/thumbnails/32.jpg)
• Thank you for your attendance and hope you have not wasted your time.