Download presentation
Presentation is loading. Please wait.
Published byColleen Henry Modified over 9 years ago
1
Introduction to Matlab By E. Noura Semary
2
Contents MATLAB Environment Command window, Workspace, Path window, Editor window,and Figure window) Basic Functions: clc, clear, save, load, who, whos … Variables in MATLAB Arrays Control flow (for – end, while – end, if – else – elseif – end, switch M-files Plotting Introduction to GUI Introduction to Image processing Toolbox
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
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
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
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
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
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
Basic Functions Function nameFunction mean clcClear the command window clearClear workspace whoShow workspace whosShow workspace in details helpShow information about any function lookforSearch for a word in MATLAB files saveSaving workspace loadLoading saved workspace
10
Variables MATLAB variables are arrays Variable must be one word myvariableaccepted my variable not accepted Variable length is up to 31 character Variables must begin by a character MATLAB variables are case sensitive There are preserved variable names:
11
Variables ansDefault function output piPi = 3.14 epsVery small value infVery large value = ∞ as the result of (1/0) NaNWhen the result = 0/0 realminMinimum real number = 10 -308 × 2.2251 realmaxMaximum real number = 10 308 × 1.7977 narginThe number of input parameters nargoutThe number of output parameters
12
Arrays : review Vector Array : (1 × n), (m ×1) Matrix : (m × n) Identity array 5 4 0 7 057057 1 4 4 5 12 8 9 15 1 0 0 0 1 0 0 0 1 15 3 4 0 (2×4) ->(3×2) -> (1×4) ->(3×1) ->
13
Arrays : review Assume You have A = B= A+B=A’ = 2A = = 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 15 3 4 0 2 7 abcdefabcdef xyxy ax+by cx+dy ex+fy (m×k)(k×n)=(m×n)
14
Arrays : Marlab vision m=4m=[4] a=3*6m=[18] a=[1,4,6,8,9]a=[1 4 6 8 9] M=[2 3 5 9 1 ; 0 3 6 4 5 ; 1 10 8 6 4] M(1) 2 M(6) 10 M(3, 4) 6 M(3, :) 1 10 8 6 4 M(:, 2) 3 3 10 M(1, 2:4) 3 5 9 M(2, 1:2:5) 0 6 5 M= 2 3 5 9 1 0 3 6 4 5 1 10 8 6 4
15
Standard arrays zeros zeros(3)= zeros(2,3)= ones ones(3)= ones(2,3)= eye eye(3)= eye(2,3)= magic magic(3) = randrand(3,4) = linspacelinspace (0,4,5)= [0 1 2 3 4] linspace (1,2,3)= [1 1.5 2] logspace logspace (0,4,5)= [1 10 100 1000 10000] logspace (1,2,3)= [10 31.6228 100] 0 0 0 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
16
Array functions size size(N)=[3 3] sum sum(N) = [14 9 16] length length(N)=[3] max max(N) = [9 7 8] det det(N)=-119 min min(N) = [1 0 3] diag diag(N)= flipud flipud(N)= invfliplr fliplr(N)= transpose (´ ) transpose(N)= rot90 rot90(N)= find find (N ==7 ) = 5 find (N == 10 ) = [] N= 1 0 3 4 7 5 9 2 8 178178 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
17
Relational and Logical Operators Relational Operators Operator nameSymbolComment eq= equal ne~=not equal lt<less than gt>greater than le<=less than or equal ge>=greater than or equal Logical Operators and&logical and or|logical or not~logical not xorlogical exclusive or
18
Control Flow for – end for variable= expression statements end example: for i=1:10 for j=1:2:10 x(i) = i*2; y=jend
19
Control Flow while – end x=0; while (x<5) x=x+s; disp(x); end
20
Control Flow if – else – elseif – end a=6;% a=7; % a=10; if (rem(a,3)==0) a=a*3; elseif (rem(a,2)==0) a=a*2; else a=a*10; end disp (a);
21
Control Flow switch – case – otherwise – end x= input (‘The value of x:’); units= input (“Enter the unit of x: (Please Enter the unit between ‘ ‘) ’); switch units case (‘inch’,’in’) y=x*2.54 ; case (‘feet’,’ft’) y=x*2.54*12 ; case (‘meter’,’m’) y=x*100 ; case (‘millimeter’,’mm’) y=x/10 ; otherwise disp (‘Unknown unit’); end
22
Control Flow break for i=1:10 if (i>5) break; else a=a*4.5; end disp (a);
23
M-files 1. Scripts : Deal with the work space directly without inputs or outputs 2. Functions: Needs inputs and only the outputs are saved in the workspace. examples: function [output1,output2…]=function_name(input1,input2,…) function function_name(input1,input2,…)
24
M-files Function or script calling is done by typing the file name. The function file name must be the same as function name Function name must be one word Has a maximum of 31character size Starting by a character
25
M-files Assume a function: function [a,b,c]=my_function(x,y) is called as [b]=my_function(x,y,z); [a,b,c]=my_function(u); then b = a value x= u value
26
Math functions Basic functions triangular functions Approximation functions complex functionsconversion functions abs cos fix absdec2hex sqrt sin round angledec2bin mean tan floor conjdec2base power acos ceil imaghex2dec log asin rem realbin2dec log10 atanfloorbase2dec exp cscceilrad2deg max secremdeg2rad min cotcart2sph sort acsccart2pol asecpol2cart acotsph2cart cosh sinh tanh
27
Exercise 1. write a matlab program that sum a series begins by 2 and ends by 100 Use both for and while loops in separated m-files 2. write a switch – case program such that: if 35>=S>20 display ‘Hot Day’ if 20>=S>10 display ‘Nice Day’ if 10>=S>=0 display ‘Cold Day ‘ otherwise display ‘Out of Range’ That S is the sum of the array M, and M is: Random matrix of size 3×6 and has values between 0 and 2 A square matrix of size 3 and values between 1 and 9 A vector of equal elements =5 and size of 7 Identity matrix of size 10×10
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.