UNIVERSITY OF UTAH ELECTRICAL AND COMPUTER ENGINEERING DEPARTMENT ECE MATLABª TUTORIAL Èdiary on Èhelp path PATH Get/set search path. PATH, by itself, prettyprints MATLAB's current search path. The initial search path list is set by PATHDEF, and is perhaps individualized by STARTUP. P = PATH returns a string containing the path in P. PATH(P) changes the path to P. PATH(PATH) forces the path cache to be rebuilt. PATH(P1,P2) changes the path to the concatenation of the two path strings P1 and P2. Thus PATH(PATH,P) appends a new directory to the current path and PATH(P,PATH) prepends a new path. If P1 or P2 are already on the path, they are not added. For example, the following statements add another directory to MATLAB's search path on various operating systems: Unix: path(path,'/home/myfriend/goodstuff') VMS: path(path,'DISKS1:[MYFRIEND.GOODSTUFF]') DOS: path(path,'TOOLS\GOODSTUFF') Mac: path(path,'HardDisk:Tools:GoodStuff:') See also WHAT, CD, DIR, ADDPATH, RMPATH. Èclear all È% for help, type help at the prompt È% A comment is anything after a % sign. È% Matlab does simple math. È2+2 ans = 4 È% Matlab does simple math and remembers the result. Èa = 2 + 2 a = 4 È% To continue a command on the next line, use 3 periods. Èa = 2 ... + 2 a = 4 È% Matlab is somewhat like C but with simpler syntax. Èfor ind = 1 : 2 : 5 È ind Èend ind = 1 ind = 3 ind = 5 Èif a > 3 È a = a + 4 Èend a = 8 È% Use a semicolon to supress output. Èa = a + 5; È% To see the value of a variable, type its name. Èa a = 13 È% Matlab is specialized for handling arrays and matrices. Èavec = [1 3 a] avec = 1 3 13 È% To make an array vertical, take its transpose using .' Èbvec = avec.' bvec = 1 3 13 È% Use single quotes to define a string. Èwierd = 'hi' wierd = hi È% Matlab can expand an existing array. Èavec = [avec 12 6] avec = 1 3 13 12 6 È% An array may be empty. Èzerovec = [] zerovec = [] È% To enter a matrix, place a semicolon at the end of each row. ÈAmat = [avec; 2 2 4 3 1] Amat = 1 3 13 12 6 2 2 4 3 1 È% You may build matrices from vertical arrays, too. ÈBmat = [avec.' [2 2 4 3 1].'] Bmat = 1 2 3 2 13 4 12 3 6 1 È% Just use * to multiply matrices. Èprodmat = Amat * Bmat prodmat = 359 102 102 34 È% Matlab handles complex numbers with ease. Èacomplex = 1.4e-2 + i * 0.089 acomplex = 0.0140 + 0.0890i Èacomplex = 1.5e-2 + 0.086i acomplex = 0.0150 + 0.0860i È% Use length to find the size of an array. Èlength(avec) ans = 5 È% Use size to find the size of a matrix. Èsize(avec) ans = 1 5 Èlength(avec.') ans = 5 È% Use an index in parentheses to extract one element of an array. Èavec(3) ans = 13 È% Note that indexes start at 1, NOT 0. Èavec(0) ??? Index exceeds matrix dimensions. Èavec(length(avec)) ans = 6 È% The colon operator means make a list by adding one until È% 2nd number exceeded. È3 : 8 ans = 3 4 5 6 7 8 È8 : 3 ans = Empty matrix: 1-by-0 È3.4 : 6.7 ans = 3.4000 4.4000 5.4000 6.4000 È% Use a second colon and the middle number is the increment. È8 : -1 : 3 ans = 8 7 6 5 4 3 Èlistvec = 1.1 : 0.1 : 1.4 listvec = 1.1000 1.2000 1.3000 1.4000 È% A colon as an index means the entire row or column. ÈAmat Amat = 1 3 13 12 6 2 2 4 3 1 ÈAmat(:,2) ans = 3 2 ÈAmat(2,:) ans = 2 2 4 3 1 È% The zeros() and ones() functions are useful. ÈCmat = [ones(4,1) [1:4].'] Cmat = 1 1 1 2 1 3 1 4 ÈDvec = zeros(1,5) Dvec = 0 0 0 0 0 È% Basic plotting is simple. Èplot(avec) È% This plot command will create a 'Figure 1' window containing È% a line plot of the values in avec. È% Use files with .m as the extension for scripts (a list of È% Matlab commands just like you would type at the prompt), È% and functions (user-defined functions you can call from È% the command line). È% .* and ./ do element-by-element * and / È[1 2] .* [3 4] ans = 3 8 È[1 2] ./ [3 4] ans = 0.3333 0.5000 È% .^ does element-by-element ^ È[2 3 5] .^ [0 2 3] ans = 1 9 125 Èdiary off