Upcoming SlideShare
×

# Learn Matlab

2,113 views

Published on

Published in: Education, Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
2,113
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
104
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Learn Matlab

1. 1. Introduction to Matlab By Abd El Kareem Ahmed
2. 2. Contents <ul><li>MATLAB Environment </li></ul><ul><ul><li>Command window, Workspace, Path window, Editor window,and Figure window) </li></ul></ul><ul><li>Basic Functions: clc, clear, save, load, who, whos … </li></ul><ul><li>Variables in MATLAB </li></ul><ul><ul><li>Arrays </li></ul></ul><ul><li>Control flow (for – end, while – end, if – else – elseif – end, switch </li></ul><ul><li>M-files </li></ul><ul><li>Plotting </li></ul><ul><li>Introduction to GUI </li></ul><ul><li>Introduction to Image processing Toolbox </li></ul>
3. 3. MATLAB Environment Command window Use Menus as an alternative to typing some commands Use of Toolbar for easy access to popular operations Status bar Command window Status of Caps, Num, and Scroll locks New M-file Open file Undo New Simulink Model Help Cut Paste Copy Workspace Browser Path Browser
4. 4. MATLAB Environment Workspace Open the selected matrix Delete the selected matrix from the workspace Displays the total number of elements in the workspace and total size of them. Close the workspace browser size of the matrix Number of bytes in the matrix Name of the matrix Type of the matrix
5. 5. MATLAB Environment Workspace Name of the matrix Current dimension of the matrix: User can change the dimensions of the matrix by simply type the dimensions he/she want Current Cell Current values: User can change the values o the matrix’s element by editing in the cell.
6. 6. MATLAB Environment Path browser The current Directory The directories in the path The Files in the selected path. Add a new path Remove the selected path
7. 7. MATLAB Environment Editor window Current line number Automatic Indenting Automatic Color Highlighting to Distinguish Different Elements The toolbar Names of the opened files in the editor New file Save file Copy Print Set/Clear break point Step in Continue List if functions in call stack Open file Cut Paste Help Clear all break points Single step Quit debugging
8. 8. MATLAB Environment Figure window New figure Open an exciting figure Save the current figure Print Zoom the plot Rotate the plot Start / End the plot editor mode Draw an arrow in the plot Type a text in the plot Draw a line in the plot
9. 9. Basic Functions load save lookfor help whos who clear clc Function name Loading saved workspace Saving workspace Search for a word in MATLAB files Show information about any function Show workspace in details Show workspace Clear workspace Clear the command window Function mean
10. 10. Variables <ul><li>MATLAB variables are arrays </li></ul><ul><li>Variable must be one word </li></ul><ul><ul><li>myvariable accepted </li></ul></ul><ul><ul><li>my variable not accepted </li></ul></ul><ul><li>Variable length is up to 31 character </li></ul><ul><li>Variables must begin by a character </li></ul><ul><li>MATLAB variables are case sensitive </li></ul><ul><li>There are preserved variable names: </li></ul>
11. 11. Variables The number of output parameters nargout The number of input parameters nargin Maximum real number = 10 308 × 1.7977 realmax Minimum real number = 10 -308 × 2.2251 realmin When the result = 0/0 NaN Very large value = ∞ as the result of (1/0) inf Very small value eps Pi = 3.14 pi Default function output ans
12. 12. Arrays : review <ul><li>Vector Array : (1 × n), (m ×1) </li></ul><ul><li>Matrix : (m × n) </li></ul><ul><li>Identity array </li></ul>5 4 0 7 0 5 7 1 4 4 5 12 8 9 15 1 0 0 0 1 0 0 0 1 <ul><li>5 </li></ul><ul><li>3 4 </li></ul><ul><li>0 0 </li></ul>(2×4) -> (3×2) -> (1×4) -> (3×1) ->
13. 13. Arrays : review <ul><li>Assume You have A = B= </li></ul><ul><li>A+B= A’ = </li></ul><ul><li>2A = = </li></ul>1 3 0 2 5 4 0 7 0 1 4 3 7 4 9 8 1 4 4 5 12 8 9 15 2 6 0 4 10 8 0 14 <ul><li>5 </li></ul><ul><li>3 4 </li></ul><ul><li>0 0 </li></ul><ul><li>2 7 </li></ul>ax+by cx+dy ex+fy (m×k)(k×n)=(m×n) a b c d e f x y
14. 14. Arrays : Marlab vision 2 3 5 9 1 0 3 6 4 5 1 10 8 6 4 M= <ul><li>M=[2 3 5 9 1 ; 0 3 6 4 5 ; 1 10 8 6 4] </li></ul><ul><ul><li>M(1)  2 </li></ul></ul><ul><ul><li>M(6)  10 </li></ul></ul><ul><ul><li>M(3, 4)  6 </li></ul></ul><ul><ul><li>M(3, :)  1 10 8 6 4 </li></ul></ul><ul><ul><li>M(:, 2)  3 </li></ul></ul><ul><ul><li>3 </li></ul></ul><ul><ul><li>10 </li></ul></ul><ul><ul><li>M(1, 2:4)  3 5 9 </li></ul></ul><ul><ul><li>M(2, 1:2:5)  0 6 5 </li></ul></ul>a=[1 4 6 8 9] a=[1,4,6,8,9] m=[18] a=3*6 m=[4] m=4
15. 15. 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 1 0 0 0 1 1 0 0 0 1 0 8 1 6 3 5 7 4 9 2 0.1389 0.6038 0.0153 0.9318 0.2028 0.2722 0.7468 0.4660 0.1987 0.1988 0.4451 0.4186 magic(3) = magic logspace (0,4,5)= [1 10 100 1000 10000] logspace (1,2,3)= [10 31.6228 100] logspace linspace ( 0,4,5 )= [0 1 2 3 4] linspace ( 1,2,3 )= [1 1.5 2] linspace rand ( 3,4) = rand eye(3)= eye(2,3)= eye ones(3)= ones(2,3)= ones zeros(3)= zeros(2,3)= zeros Standard arrays
16. 16. N= 1 0 3 4 7 5 9 2 8 1 7 8 9 2 8 4 7 5 1 0 3 3 0 1 5 7 4 8 2 9 3 5 8 0 7 2 1 4 9 -0.3866 -0.0504 0.1765 -0.1092 0.1597 -0.0588 0.4622 0.0168 -0.0588 1 4 9 0 7 2 3 5 8 find (N ==7 ) = 5 find (N == 10 ) = [] find rot90(N)= fliplr ( N)= flipud ( N)= min(N) = [1 0 3] max(N) = [9 7 8] sum(N) = [14 9 16] rot90 fliplr flipud min max sum transpose ( ´ ) inv diag det length size transpose ( N)= diag(N)= det(N)=-119 length(N)=[3] size(N)=[3 3] Array functions
17. 17. Relational and Logical Operators logical exclusive or xor logical not ~ not logical or | or logical and & and Logical Operators greater than or equal >= ge less than or equal <= le greater than > gt less than < lt not equal ~= ne equal = = eq Comment Symbol Operator name Relational Operators
18. 18. Control Flow <ul><li>for – end </li></ul><ul><ul><ul><ul><li>for variable= expression </li></ul></ul></ul></ul><ul><ul><ul><ul><li>statements </li></ul></ul></ul></ul><ul><ul><ul><ul><li>end </li></ul></ul></ul></ul><ul><li>example: </li></ul><ul><ul><ul><ul><li>for i=1:10 for j=1:2:10 </li></ul></ul></ul></ul><ul><ul><ul><ul><li>x(i) = i*2; y=j </li></ul></ul></ul></ul><ul><ul><ul><ul><li>end end </li></ul></ul></ul></ul>
19. 19. Control Flow <ul><li>while – end </li></ul><ul><ul><ul><ul><li>x=0; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>while (x<5) </li></ul></ul></ul></ul><ul><ul><ul><ul><li>x=x+s; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>disp (x); </li></ul></ul></ul></ul><ul><ul><ul><ul><li>end </li></ul></ul></ul></ul>
20. 20. Control Flow <ul><li>if – else – elseif – end </li></ul><ul><ul><ul><ul><li>a=6; % a=7; % a=10 ; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>if (rem(a,3)==0) </li></ul></ul></ul></ul><ul><ul><ul><ul><li>a=a*3; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>elseif (rem(a,2)==0) </li></ul></ul></ul></ul><ul><ul><ul><ul><li>a=a*2; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>else </li></ul></ul></ul></ul><ul><ul><ul><ul><li>a=a*10; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>end </li></ul></ul></ul></ul><ul><ul><ul><ul><li>disp (a); </li></ul></ul></ul></ul>
21. 21. Control Flow <ul><li>switch – case – otherwise – end </li></ul><ul><ul><ul><ul><li>x= input (‘The value of x:’); </li></ul></ul></ul></ul><ul><ul><ul><ul><li>units= input (“Enter the unit of x: (Please Enter the unit between ‘ ‘) ’); </li></ul></ul></ul></ul><ul><ul><ul><ul><li>switch units </li></ul></ul></ul></ul><ul><ul><ul><ul><li>case (‘inch’,’in’) </li></ul></ul></ul></ul><ul><ul><ul><ul><li>y=x*2.54 ; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>case (‘feet’,’ft’) </li></ul></ul></ul></ul><ul><ul><ul><ul><li>y=x*2.54*12 ; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>case (‘meter’,’m’) </li></ul></ul></ul></ul><ul><ul><ul><ul><li>y=x*100 ; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>case (‘millimeter’,’mm’) </li></ul></ul></ul></ul><ul><ul><ul><ul><li>y=x/10 ; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>otherwise </li></ul></ul></ul></ul><ul><ul><ul><ul><li>disp (‘Unknown unit’); </li></ul></ul></ul></ul><ul><ul><ul><ul><li>end </li></ul></ul></ul></ul>
22. 22. Control Flow <ul><li>break </li></ul><ul><ul><ul><li>for i=1:10 </li></ul></ul></ul><ul><ul><ul><li>if (i>5) </li></ul></ul></ul><ul><ul><ul><li>break ; </li></ul></ul></ul><ul><ul><ul><li>else </li></ul></ul></ul><ul><ul><ul><li>a=a*4.5; </li></ul></ul></ul><ul><ul><ul><li>end </li></ul></ul></ul><ul><ul><ul><li>end </li></ul></ul></ul><ul><ul><ul><li>disp (a); </li></ul></ul></ul>
23. 23. M-files <ul><li>Scripts : Deal with the work space directly without inputs or outputs </li></ul><ul><li>Functions: Needs inputs and only the outputs are saved in the workspace. </li></ul><ul><li>examples: </li></ul><ul><ul><li>function [output1,output2…]=function_name(input1,input2,…) </li></ul></ul><ul><ul><li>function function_name(input1,input2,…) </li></ul></ul>
24. 24. M-files <ul><li>Function or script calling is done by typing the file name. </li></ul><ul><li>The function file name must be the same as function name </li></ul><ul><li>Function name must be one word </li></ul><ul><li>Has a maximum of 31character size </li></ul><ul><li>Starting by a character </li></ul>
25. 25. M-files <ul><li>Assume a function: </li></ul><ul><ul><ul><li>function [a,b,c]=my_function(x,y) </li></ul></ul></ul><ul><li>is called as </li></ul><ul><ul><ul><li>[b]=my_function(x,y,z); </li></ul></ul></ul><ul><ul><ul><li>[a,b,c]=my_function(u); </li></ul></ul></ul><ul><li>then </li></ul><ul><ul><ul><li>b = a value </li></ul></ul></ul><ul><ul><ul><li>x= u value </li></ul></ul></ul>
26. 26. Math functions rem ceil floor round fix Approximation functions sph2cart deg2rad rad2deg base2dec pol2cart cart2pol cart2sph bin2dec hex2dec dec2base dec2bin dec2hex conversion functions tanh sinh cosh acot asec rem ceil floor real imag conj angle abs cos abs complex functions acsc sort cot min sec max csc exp atan log10 asin log acos power tan mean sin sqrt triangular functions Basic functions
27. 27. Exercise <ul><li>write a matlab program that sum a series begins by 2 and ends by 100 </li></ul><ul><ul><ul><li>Use both for and while loops in separated m-files </li></ul></ul></ul><ul><li>write a switch – case program such that: </li></ul><ul><ul><ul><li>if 35>=S>20 display ‘ Hot Day’ </li></ul></ul></ul><ul><ul><ul><li>if 20>=S>10 display ‘ Nice Day’ </li></ul></ul></ul><ul><ul><ul><li>if 10>=S>=0 display ‘ Cold Day ‘ </li></ul></ul></ul><ul><ul><ul><li>otherwise display ‘ Out of Range’ </li></ul></ul></ul><ul><ul><li>That S is the sum of the array M, and M is: </li></ul></ul><ul><ul><ul><li>Random matrix of size 3×6 and has values between 0 and 2 </li></ul></ul></ul><ul><ul><ul><li>A square matrix of size 3 and values between 1 and 9 </li></ul></ul></ul><ul><ul><ul><li>A vector of equal elements =5 and size of 7 </li></ul></ul></ul><ul><ul><ul><li>Identity matrix of size 10×10 </li></ul></ul></ul>